mirror of
https://github.com/newnius/YAO-optimizer.git
synced 2025-12-13 08:26:43 +00:00
update
This commit is contained in:
25
train.py
25
train.py
@@ -12,6 +12,11 @@ from math import sqrt
|
||||
import numpy
|
||||
|
||||
|
||||
# date-time parsing function for loading the dataset
|
||||
def parser(x):
|
||||
return datetime.strptime('190' + x, '%Y-%m')
|
||||
|
||||
|
||||
# frame a sequence as a supervised learning problem
|
||||
def timeseries_to_supervised(data, lag=1):
|
||||
df = DataFrame(data)
|
||||
@@ -76,13 +81,11 @@ def fit_lstm(train, batch_size, nb_epoch, neurons):
|
||||
|
||||
# make a one-step forecast
|
||||
def forecast_lstm(model, batch_size, X):
|
||||
X = X.reshape(X.shape[0], X.shape[1], len(X))
|
||||
X = X.reshape(1, 1, len(X))
|
||||
yhat = model.predict(X, batch_size=batch_size)
|
||||
return yhat[0, 0]
|
||||
|
||||
|
||||
batch_size = 12
|
||||
|
||||
# load dataset
|
||||
series = read_csv('data.csv', header=0, index_col=0, squeeze=True)
|
||||
|
||||
@@ -101,25 +104,19 @@ train, test = supervised_values[0:-12], supervised_values[-12:]
|
||||
scaler, train_scaled, test_scaled = scale(train, test)
|
||||
|
||||
# fit the model
|
||||
lstm_model = fit_lstm(train_scaled, batch_size, 30, 4)
|
||||
lstm_model = fit_lstm(train_scaled, 1, 30, 4)
|
||||
# forecast the entire training dataset to build up state for forecasting
|
||||
train_reshaped = train_scaled[:, 0].reshape(len(train_scaled), 1, 1)
|
||||
lstm_model.predict(train_reshaped, batch_size=batch_size)
|
||||
|
||||
print(raw_values)
|
||||
print(diff_values)
|
||||
print(supervised_values)
|
||||
print(train, test)
|
||||
print(train_scaled, test_scaled)
|
||||
lstm_model.predict(train_reshaped, batch_size=1)
|
||||
|
||||
# walk-forward validation on the test data
|
||||
predictions = list()
|
||||
for j in range(len(test_scaled)):
|
||||
# make one-step forecast
|
||||
#X, y = test_scaled[j, 0:-1], test_scaled[j, -1]
|
||||
yhat = forecast_lstm(lstm_model, batch_size, test_scaled)
|
||||
X, y = test_scaled[j, 0:-1], test_scaled[j, -1]
|
||||
yhat = forecast_lstm(lstm_model, 1, X)
|
||||
# invert scaling
|
||||
yhat = invert_scale(scaler, test_scaled, yhat)
|
||||
yhat = invert_scale(scaler, X, yhat)
|
||||
# invert differencing
|
||||
yhat = inverse_difference(raw_values, yhat, len(test_scaled) + 1 - j)
|
||||
# store forecast
|
||||
|
||||
Reference in New Issue
Block a user