我有时间序列数据,我试图预测第二天的现货价格。我的数据看起来像:
我在 f_area 上做了 groupby,最后得到了 multiindex。现在我正在尝试使用 RandomForestRegressor 进行预测。
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
y = area3['y'].values
X = area3[['f_price', 'day_of_week', 'day_of_month']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)
model = RandomForestRegressor()
model = model.fit(X_train, y_train)
y_pred = model.predict(X_test)
现在,当我尝试绘制 y_test(实际值)和 y_pred(预测值)时
fig, ax = plt.subplots()
ax.plot(y_test)
ax.plot(y_pred)
我想要的是在 X 轴上有日期,但由于多索引,我无法这样做。我该怎么做或删除多索引?我试图通过 reset_index 删除多索引,但它在我的情况下不起作用谢谢
最佳答案
首先,您获取一个变量并将日期列存储为列表形式
注意:确保您的列表c
长度和y_pred
、y-test
大小相同。
fig=plt.figure()
c=area3['f_date'].to_list()
plt.plot(c,y_test)
plt.plot(c,y_pred)
plt.show()
关于python - 当我们有多指标时如何用实际值绘制预测值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53188871/