-
-
Save AlexanderNeutel/271dc9de86ef77dbc7ffb51654bf7b08 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from sklearn.preprocessing import StandardScaler, MinMaxScaler | |
from keras.models import Sequential | |
from keras.layers import LSTM, Dense, Dropout | |
from tensorflow.keras.optimizers import Adagrad | |
# load the data using pandas | |
data = pd.read_csv('S&P500 historical data.csv') | |
data = data.dropna() | |
# split the data into training and testing sets | |
train_size = int(len(data) * 0.7) | |
train_data = data.iloc[:train_size, :] | |
test_data = data.iloc[train_size:, :] | |
# normalize the data | |
scaler = StandardScaler() | |
train_data = scaler.fit_transform(train_data) | |
test_data = scaler.transform(test_data) | |
def create_sequences(data, sequence_length): | |
X = [] | |
y = [] | |
for ii in range(sequence_length, len(data)): | |
X.append(data[ii-sequence_length:ii, :]) | |
y.append(data[ii,0]) | |
return np.array(X), np.array(y) | |
sequence_length = 10 # number of time steps to look back | |
X_train, y_train = create_sequences(train_data, sequence_length) | |
X_test, y_test = create_sequences(test_data, sequence_length) | |
# reshape the data | |
y_train = np.reshape(y_train, (y_train.shape[0], 1)) | |
y_test = np.reshape(y_test, (y_test.shape[0], 1)) | |
# create the LSTM model | |
model = Sequential() | |
model.add(LSTM(150, input_shape=(sequence_length, X_train.shape[2]))) | |
model.add(Dense(y_train.shape[1])) | |
model.compile(loss='mse', optimizer=Adagrad(learning_rate=0.01)) | |
# train the model | |
model.fit(X_train, y_train, epochs=50, batch_size=16, verbose=0) | |
# evaluate the model on the test set | |
mse = model.evaluate(X_test, y_test, verbose=0) | |
print('MSE:', mse) | |
model.save('model.h5') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment