Я пробовал использовать линейную или полиномиальную регрессию , RandomForestRegresor и GradientBoostingRegresor. Эти модели возвращают очень точные результаты на основе предоставленных данных, но когда я пытаюсь использовать их за пределами этого диапазона, результат либо такой же, как последнее значение, либо невозможный результат (иногда я получаю отрицательные цены).
Кто-нибудь знает, как это сделать? Стоит ли мне использовать другой метод?
Например, я попробовал GradientBoosting цены на бензин в период с 01.1992 по 05.2024:
Код: Выделить всё
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from sklearn.model_selection import train_test_split
from sklearn.ensemble import GradientBoostingRegressor
data = pd.read_csv("essence2.csv", sep=";").iloc[::-1]
x = data["date"].values.reshape(-1, 1)
y = data["price"].values
x_train, x_test, y_train, y_test = train_test_split(
x,
y,
test_size=0.01,
random_state=2,
)
model = GradientBoostingRegressor(
n_estimators=200,
learning_rate=1,
random_state=2,
)
model.fit(x_train, y_train)
y_predictions = np.round(model.predict(x), 4)
residues = y_predictions - y
fig, ax = plt.subplots(2)
ax[0].plot(x, y, color="blue", label="Prix réel")
ax[0].plot(x, y_predictions, color="orange", linestyle="--", label="Gradient Boosting Regressor")
ax[0].xaxis.set_major_locator(MaxNLocator(8))
ax[0].tick_params(axis="x", rotation=45)
ax[0].legend()
ax[1].plot(x, residues, color="orange", label="Résidues")
ax[1].xaxis.set_major_locator(MaxNLocator(8))
ax[1].tick_params(axis="x", rotation=45)
ax[1].legend()
plt.tight_layout()
plt.show()
Как спрогнозировать цену на 06/2024?
Подробнее здесь: https://stackoverflow.com/questions/787 ... ing-python