Add Timestamp dates to X for machine learning

Viewed 306

This loop use np for adding future dates to prediction dataset:

# 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))

it's return this values properly:

1640214000
1671836400
1703458800
1735167600
1766790000

The problem the code doesn't append these 5 timestamp values to array X (the hypothesis is that e+09 - notation - but don't have an idea how to make it work).

The structure of array X is:

array([[1.5383520e+09],
       [1.5384384e+09],
       [1.5385248e+09],
       (...)
       [1.6339968e+09],
       [1.6340832e+09],
       [1.6341696e+09]])

After add those timestamp values to X get the error in predicting code:

# 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))

error:

ValueError: Found input variables with inconsistent numbers of samples: [766, 771]

in the last line

There is error - cause the X and Y values are different:

ValueError: x and y must have same first dimension, but have shapes (771, 1) and (766, 1)

but already those 5 values from Y should be predicted.

1 Answers

Inside loop use this:

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