为机器学习添加时间戳日期到X



这个循环使用np向预测数据集添加未来日期:

# Future prediction, add dates here for which you want to predict
dates = ["2021-12-23", "2022-12-24", "2023-12-25", "2024-12-26", "2025-12-27",]
#convert to time stamp
for dt in dates:
datetime_object = datetime.strptime(dt, "%Y-%m-%d")
timestamp = datetime.timestamp(datetime_object)
# to array X
print(int(timestamp))
np.append(X, int(timestamp))

它会正确返回这些值:

1640214000
1671836400
1703458800
1735167600
1766790000

问题代码没有将这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后得到预测代码的错误:

# Future prediction, add dates here for which you want to predict
from datetime import datetime
import numpy as np
from matplotlib import pyplot as plt
from sklearn.metrics import mean_squared_error
# Define model
model = DecisionTreeRegressor()
# Fit to model
model.fit(X_train, Y_train)
# predict
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)

但是Y的5个值已经可以预测了

内部循环使用this:

X = np.append(X, int(timestamp))

最新更新