这个循环使用np来向预测数据集添加未来日期:
# 未来预测,在这里添加你想要预测的日期dates = ["2021-12-23", "2022-12-24", "2023-12-25", "2024-12-26", "2025-12-27",]#转换为时间戳for dt in dates: datetime_object = datetime.strptime(dt, "%Y-%m-%d") timestamp = datetime.timestamp(datetime_object) # 加入到数组X print(int(timestamp)) np.append(X, int(timestamp))
它正确地返回这些值:
16402140001671836400170345880017351676001766790000
问题是代码没有将这5个时间戳值添加到数组X中(假设是e+09 – 科学记数法的问题 – 但不知道如何解决)。
数组X的结构是:
array([[1.5383520e+09], [1.5384384e+09], [1.5385248e+09], (...) [1.6339968e+09], [1.6340832e+09], [1.6341696e+09]])
在将这些时间戳值添加到X后,预测代码出现错误:
# 未来预测,在这里添加你想要预测的日期from datetime import datetimeimport numpy as npfrom matplotlib import pyplot as pltfrom sklearn.metrics import mean_squared_error# 定义模型model = DecisionTreeRegressor()# 拟合模型model.fit(X_train, Y_train)# 预测predictions = model.predict(X)print(mean_squared_error(Y, predictions))
错误:
ValueError: Found input variables with inconsistent numbers of samples: [766, 771]
在最后一行
错误的原因是X和Y的值不同:
ValueError: x and y must have same first dimension, but have shapes (771, 1) and (766, 1)
但这5个来自Y的值应该已经预测过了。
回答:
在循环中使用这个:
X = np.append(X, int(timestamp))