import yfinance as yf
import pandas as pd
gme = yf.Ticker('GME')
gme = gme.history(period='max')
gme.to_csv('gme.csv')
gme = pd.read_csv('gme.csv')
gme
del gme['Dividends']
del gme['Stock Splits']
gme
gme['Close'].plot()
import pandas as pd
output_var = pd.DataFrame(gme['Close'])
features = ['Open', 'High', 'Low', 'Volume']
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
feature_transform = scaler.fit_transform(gme[features])
feature_transform = pd.DataFrame(columns=features, data=feature_transform, index=gme.index)
feature_transform
from sklearn.model_selection import TimeSeriesSplit
timesplit = TimeSeriesSplit(n_splits=10)
for train_index, test_index in timesplit.split(feature_transform):
x_train, x_test = feature_transform[:len(train_index)], feature_transform[len(train_index): (len(train_index)+len(test_index))]
y_train, y_test = output_var[:len(train_index)].values.ravel(), output_var[len(train_index): (len(train_index)+len(test_index))].values.ravel()
import numpy as np
train_x = np.array(x_train)
test_x = np.array(x_test)
x_train = train_x.reshape(x_train.shape[0], 1, x_train.shape[1])
x_test = test_x.reshape(x_test.shape[0], 1, x_test.shape[1])
from tensorflow.keras.optimizers import Adam
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.layers import LSTM
from keras.utils.vis_utils import plot_model
from sklearn.metrics import mean_squared_error, r2_score
model = Sequential()
model.add(LSTM(32, input_shape=(1, train_x.shape[1]), activation='relu', return_sequences=False))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train, y_train, epochs=30, batch_size=8, verbose=1, shuffle=False)
y_pred = model.predict(x_test)
import matplotlib.pyplot as plt
plt.plot(y_test, label='True Value')
plt.plot(y_pred, label='LSTM Value')
plt.title('Prediction by LSTM Value')
plt.xlabel('Time Scale')
plt.ylabel('Scaled USD')
plt.legend()
plt.show()