【问题标题】:How to plot predicted values with actual values when we have multi-index当我们有多索引时如何用实际值绘制预测值
【发布时间】:2019-04-10 20:20:50
【问题描述】:

我正在尝试预测第二天的现货价格的时间序列数据。我的数据看起来像:

我在 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 删除多索引,但在我的情况下它不起作用谢谢

【问题讨论】:

    标签: python pandas matplotlib time-series pandas-groupby


    【解决方案1】:

    首先取一个变量并将日期列存储为列表形式

    注意:确保您的列表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()
    

    【讨论】:

      猜你喜欢
      • 2014-12-21
      • 1970-01-01
      • 2018-01-13
      • 2021-11-11
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-26
      相关资源
      最近更新 更多