当我们有多个索引时,如何绘制预测值和实际值的图表

3

我有一些时间序列数据,想要预测明天的现货价格。我的数据长这样:enter image description here

我对f_area进行了分组,得到了多级索引。现在我正在尝试使用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)

我得到了这张图表。 enter image description here 我想要的是在X轴上显示日期,但由于有多重索引,我无法实现。该怎么办才能添加日期或者移除多重索引呢?我尝试使用reset_index来移除多重索引,但在我的情况下无法生效。谢谢。
1个回答

0

首先,您需要创建一个变量并将日期列存储为列表形式

注意:请确保您的列表 c 长度与y_predy_test大小相同。

fig=plt.figure()
c=area3['f_date'].to_list()
plt.plot(c,y_test)
plt.plot(c,y_pred)
plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接