-
-
Save lukovkin/1aefa4509e066690b892 to your computer and use it in GitHub Desktop.
# Time Series Testing | |
import keras.callbacks | |
from keras.models import Sequential | |
from keras.layers.core import Dense, Activation, Dense, Dropout | |
from keras.layers.recurrent import LSTM | |
# Call back to capture losses | |
class LossHistory(keras.callbacks.Callback): | |
def on_train_begin(self, logs={}): | |
self.losses = [] | |
def on_batch_end(self, batch, logs={}): | |
self.losses.append(logs.get('loss')) | |
# You should get data frames with prices somewhere, e.g. on Quandl - implementation is up to you | |
# merge data frames | |
merged = df1.merge(df2, left_index=True, right_index=True, how='inner').dropna() | |
# data prep | |
# use 100 days of historical data to predict 10 days in the future | |
data = merged.values | |
examples = 100 | |
y_examples = 10 | |
nb_samples = len(data) - examples - y_examples | |
# input - 2 features | |
input_list = [np.expand_dims(np.atleast_2d(data[i:examples+i,:]), axis=0) for i in xrange(nb_samples)] | |
input_mat = np.concatenate(input_list, axis=0) | |
# target - the first column in merged dataframe | |
target_list = [np.atleast_2d(data[i+examples:examples+i+y_examples,0]) for i in xrange(nb_samples)] | |
target_mat = np.concatenate(target_list, axis=0) | |
# set up model | |
trials = input_mat.shape[0] | |
features = input_mat.shape[2] | |
hidden = 64 | |
model = Sequential() | |
model.add(LSTM(hidden, input_shape=(examples, features))) | |
model.add(Dropout(.2)) | |
model.add(Dense(y_examples)) | |
model.add(Activation('linear')) | |
model.compile(loss='mse', optimizer='rmsprop') | |
# Train | |
history = LossHistory() | |
model.fit(input_mat, target_mat, nb_epoch=100, batch_size=400, callbacks=[history]) |
Hi,
I am also looking on how to predict the future 10 days.
Any help on the same will be really helpful.
Thanks in advance.
Can you help me solve this issue?
I am new to deep learning and LSTM. I have a very simple question. I have taken a sample of demands for 50 time steps and I am trying to forecast the demand value for the next 10 time steps (up to 60 time steps) using the same 50 samples to train the model.
But unfortunately, the closest I came is splitting the sample demands into 67 training % and 33 testing % and my forecast is only forecasting for the 33% (35 - 50 time steps), but it never goes beyond 50 time steps. Can anybody help me with this issue?
I have attached my code below.
Thank you in advance.
import pandas
import matplotlib.pyplot as plt
dataset = pandas.read_csv('Dmd2ahr.csv')
plt.plot(dataset)
plt.show()
LSTM for international airline passengers problem with window regression framing
import numpy
import matplotlib.pyplot as plt
from pandas import read_csv
import math
from keras.models import Sequential
from keras.layers import Dropout
from keras.layers import Dense
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
dataX, dataY = [], []
for i in range(len(dataset)-look_back-1):
a = dataset[i:(i+look_back), 0]
dataX.append(a)
dataY.append(dataset[i + look_back, 0])
return numpy.array(dataX), numpy.array(dataY)
fix random seed for reproducibility
numpy.random.seed(7)
load the dataset
dataframe = read_csv('Dmd2ahr.csv')
dataset = dataframe.values
dataset = dataset.astype('float32')
normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)
split into train and test sets
train_size = int(len(dataset) * 0.7)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
print(len(train), len(test))
reshape into X=t and Y=t+1
look_back = 3
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)
reshape input to be [samples, time steps, features]
#trainX = numpy.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
#testX = numpy.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
reshape input to be [samples, time steps, features]
trainX = numpy.reshape(trainX, (trainX.shape[0], trainX.shape[1], 1))
testX = numpy.reshape(testX, (testX.shape[0], testX.shape[1], 1))
create and fit the LSTM network
model = Sequential()
model.add(LSTM(32, input_shape=(look_back, 1)))
model.add(Dropout(0.3))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam', metrics = ['accuracy'])
model.fit(trainX, trainY, epochs=300, batch_size=1, verbose=2)
make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
testY = scaler.inverse_transform([testY])
calculate root mean squared error
trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
print('Train Score: %.2f RMSE' % (trainScore))
testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
print('Test Score: %.2f RMSE' % (testScore))
shift train predictions for plotting
trainPredictPlot = numpy.empty_like(dataset)
trainPredictPlot[:, :] = numpy.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
shift test predictions for plotting
testPredictPlot = numpy.empty_like(dataset)
testPredictPlot[:, :] = numpy.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
plot baseline and predictions
plt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()
print (trainPredict)
print (testPredict)
Dear lukovkin, Suppose I have multiple time series as input and I need to predict all these time series at once for the next 10 days, how should I reshape the input and target datasets?
Regards
Hi,
Could you please tell me how to predict the next 10 days in future? (i.e.) the steps after model.fit and how to evaluate this model.
Also is it possible to do multiple sequences input with Stateful LSTM?Thanks in advance.
u need tu put a Dense layer as output with 10 units, and train your network with the same structure N-imputs and 10 outputs values
Hello!
Would you like to help me?
I am stuck on a problem, how to fit a multiple input CNN or LSTM model.
I have a time series data divided (split) into train, test, and validation
I want to give the same training data to two different CNN models and concatenate them. I have built the model as shown in the figure, input shape =168,23
My network is;
but I am not getting how to fit this model.
once I tried to fit the model I face the error
@sarahboufelja @bv123 @kascesar @Sudarsan9966 will be waiting for your kind reply.
Thank you
Hi,
Could you please tell me how to predict the next 10 days in future? (i.e.) the steps after model.fit and how to evaluate this model.
Also is it possible to do multiple sequences input with Stateful LSTM?
Thanks in advance.