python - 当我们有多指标时如何用实际值绘制预测值

标签 python pandas matplotlib time-series pandas-groupby

我有时间序列数据,我试图预测第二天的现货价格。我的数据看起来像:enter image description here

我在 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)

我得到了这张图。 enter image description here

我想要的是在 X 轴上有日期,但由于多索引,我无法这样做。我该怎么做或删除多索引?我试图通过 reset_index 删除多索引,但它在我的情况下不起作用谢谢

最佳答案

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

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

关于python - 当我们有多指标时如何用实际值绘制预测值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53188871/

相关文章:

python - 在 groupby 中的每条记录上显示组

python - 将图例添加到散点图 (PCA)

Python:如何在矩阵和数据帧之间映射值?

python - python 中将列转换为行

Python 错误 : null byte in input prompt

python - requests.get() (Python,请求模块)是否暂停脚本直到响应到达?

python - 包含字符串和 float 的 Pandas Dataframe 列

Python "TypeError: unhashable type: ' slice'"用于编码分类数据

python - 在 NetworkX 中无法将图形保存为 jpg 或 png 文件

Python TimedRotatingFileHandler - 日志丢失