首页 > 解决方案 > 我想显示从 500 天前到 2020 年 12 月 31 日的预测图,但这是错误的。我的论文真的需要帮助

问题描述

 # The date from which on the date is displayed
 display_start_date = pd.Timestamp(2020,12,31) - timedelta(days=500)

 # Add the date column
data_filtered_sub = data_filtered.copy()
data_filtered_sub['Date'] = date_index

 # Add the difference between the valid and predicted prices
train = data_filtered_sub[:train_data_len + 1]
valid = data_filtered_sub[train_data_len:]
valid.insert(1, "Prediction", y_pred.ravel(), True)
valid.insert(1, "Difference", valid["Prediction"] - valid["PX_LAST"], True)

 # Zoom in to a closer timeframe
valid = valid[valid['Date'] > display_start_date]
train = train[train['Date'] > display_start_date]

 # Visualize the data
fig, ax1 = plt.subplots(figsize=(22, 10), sharex=True)
xt = train['Date']; yt = train[["PX_LAST"]]
xv = valid['Date']; yv = valid[["PX_LAST", "Prediction"]]
plt.title("Predictions vs Actual Values", fontsize=20)
plt.ylabel(stockname, fontsize=18)
plt.plot(xt, yt, color="#039dfc", linewidth=2.0)
plt.plot(xv, yv["Prediction"], color="#E91D9E", linewidth=2.0)
plt.plot(xv, yv["PX_LAST"], color="black", linewidth=2.0)
plt.legend(["Train", "Test Predictions", "Actual Values"], loc="upper left")

 # Create the bar plot with the differences
x = valid['PX_LAST']
y = valid["Difference"]

 # Create custom color range for positive and negative differences
valid.loc[y >= 0, 'diff_color'] = "#2BC97A"
valid.loc[y < 0, 'diff_color'] = "#C92B2B"

plt.bar(x, y, width=0.8, color=valid['diff_color'])
plt.grid()
plt.show()

当我运行代码时,错误消息如下

TypeError                                 Traceback (most recent call last)
<ipython-input-88-3342eef4afe1> in <module>
     13 
     14 # Zoom in to a closer timeframe
     ---> 15 valid = valid[valid['Date'] > display_start_date]
     16 train = train[train['Date'] > display_start_date]
     17 

 ~\Anaconda3\lib\site-packages\pandas\core\ops\__init__.py in wrapper(self, other, axis)
   1227 
   1228             with np.errstate(all="ignore"):
 -> 1229                 res = na_op(values, other)
   1230             if is_scalar(res):
   1231                 raise TypeError(

~\Anaconda3\lib\site-packages\pandas\core\ops\__init__.py in na_op(x, y)
   1115                     result = method(y)
   1116                 if result is NotImplemented:
   -> 1117                     return invalid_comparison(x, y, op)
   1118             else:
   1119                 result = op(x, y)

~\Anaconda3\lib\site-packages\pandas\core\ops\__init__.py in invalid_comparison(left, right, 
op)
    495         raise TypeError(
    496             "Invalid comparison between dtype={dtype} and {typ}".format(
    --> 497                 dtype=left.dtype, typ=type(right).__name__
    498             )
    499         )

TypeError: Invalid comparison between dtype=int64 and Timestamp

标签: pythontime-seriesprediction

解决方案


推荐阅读