<jupyter_start><jupyter_text>Load the dataset - CAC40 from Yahoo<jupyter_code>startDate = pd.to_datetime('2005-01-01')
endDate = pd.to_datetime('2020-11-15')
df = web.DataReader("^FCHI", data_source='yahoo', start = startDate,end = endDate)
df.tail()
data = df["Close"]
last_15_days = data.tail(15)
print(last_15_days)
print(data.head())
data = data.reset_index()
print(len(data))
separating_date = pd.to_datetime('2017-01-01')
training_data = data[data['Date'] < separating_date].copy()
test_data = data[data['Date'] >= separating_date].copy()
print("Training data : " + str(len(training_data)) + ", Testing data : " + str(len(test_data)))
training_data = training_data.set_index('Date')
test_data = test_data.set_index('Date')
plt.figure(figsize=(14,4))
plt.plot(training_data['Close'])
plt.plot(test_data['Close'])
plt.ylabel("Price")
plt.xlabel("Date")
plt.legend(["Training Set", "Test Set"])
plt.title("CAC40 Close Price")<jupyter_output><empty_output><jupyter_text>Feature scaling<jupyter_code>from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0,1))
training_data_scaled = scaler.fit_transform(training_data)<jupyter_output><empty_output><jupyter_text>Creating data with timesteps<jupyter_code>X_train=[]
y_train=[]

for i in range(60, len(training_data_scaled)):
    temp_data = training_data_scaled[i-60:i, 0]
    X_train.append(temp_data)
    #y_train.append(np.mean(temp_data))
    y_train.append(training_data_scaled[i, 0])
X_train, y_train = np.array(X_train), np.array(y_train)
print(X_train.shape, y_train.shape)
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1],1))
print(X_train.shape, y_train.shape)<jupyter_output>(3007, 60, 1) (3007,)<jupyter_text>Building the LSTMTo build the LSTM we need to import a few modules from keras:    Sequential for initializing the neural network     Dense for adding a densely connected network layer     LSTM for adding the Long-Short-Term Memory layer     Dropout for adding dropout layers that prevent overfitting After making the predictions we use inverse_transform to get back the stock prices in normal readable format<jupyter_code>from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers import Dropout
from keras import optimizers
regressor = Sequential()
regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_train.shape[1], 1)))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units = 50, return_sequences = True))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units = 50))
regressor.add(Dropout(0.5))

regressor.add(Dense(units = 1))
optimizer = optimizers.Adam(clipvalue=0.5)
regressor.compile(optimizer = optimizer, loss = 'mean_squared_error')
regressor.fit(X_train, y_train,epochs = 20, batch_size = 200)
original_test_data = test_data.copy()
test_data_for_prediction = training_data[-60:].append(original_test_data)
test_data_scaled = scaler.fit_transform(test_data_for_prediction)
print(test_data_scaled.shape, test_data.shape)
X_test = []
y_test = []

for i in range(60, len(test_data_scaled)):
    X_test.append(test_data_scaled[i-60:i,0])
    y_test.append(test_data_scaled[i,0])
X_test, y_test = np.array(X_test), np.array(y_test)
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1],1))
predicted_price = regressor.predict(X_test)
print(predicted_price.shape, y_test.shape)
y_test = np.reshape(y_test, (y_test.shape[0], 1))
predicted_price = scaler.inverse_transform(predicted_price)
y_test = scaler.inverse_transform(y_test)
predicted_price = pd.DataFrame(predicted_price)
predicted_price.rename(columns = {0:'CAC40_predicted'}, inplace = True)
predicted_price = predicted_price.round(decimals=0)
predicted_price.index = test_data.index
print(max(y_test))
print(predicted_price['CAC40_predicted'].max(), predicted_price['CAC40_predicted'].min())<jupyter_output>[6111.24023438]
5811.0 4188.0<jupyter_text>Plotting the results<jupyter_code>from sklearn.metrics import mean_squared_error
plt.figure(figsize = (14,5))
mse = mean_squared_error(y_test, predicted_price)
plt.plot(predicted_price['CAC40_predicted'], color = 'red', label = 'Predicted CAC40 closing price')
plt.plot(test_data['Close'], color = 'green', label = 'Actual CAC40 closing price')
plt.title("CAC40 closing Price prediction- with MSE {:10.4f}".format(mse))
plt.xlabel('Time')
plt.ylabel('Price (EUR)')
plt.legend()<jupyter_output><empty_output><jupyter_text>Forecasting future stock<jupyter_code>n = test_data_scaled.shape[0]
X_forecast = test_data_scaled[n-60:n+1,0]
X_forecast = np.array(X_forecast)
preds = []
n_days_ahead = 5
for i in range(0,n_days_ahead):
    X_forecast = np.reshape(X_forecast, (1, X_forecast.shape[0], 1))
    y_pred_scaled = regressor.predict(X_forecast)
    y_pred = scaler.inverse_transform(y_pred_scaled)
    y_pred = np.reshape(y_pred, (np.shape(y_pred)[0]))
    print(y_pred)
    y_pred_scaled = np.reshape(y_pred_scaled, (np.shape(y_pred_scaled)[0]))
    preds.append(y_pred)
    X_forecast = np.reshape(X_forecast, (X_forecast.shape[1]))
    X_forecast = np.concatenate((X_forecast, y_pred_scaled), axis=0)
    X_forecast = X_forecast[-60:]
    n = X_forecast.shape[0]
prediction = np.concatenate([predicted_price, preds], axis=0)
prediction.shape
#plt.figure(figsize = (14,5))
#plt.title("MSE {:10.4f}".format(mse))
#mse = mean_squared_error(y_test, predicted_price)
#plt.plot(predicted_price["CAC40_predicted"], color = 'red', label = 'Predicted CAC40 closing price')
#plt.plot(prediction, color = 'blue', label = 'Forecast for the future')
#plt.plot(test_data["Close"], color = 'green', label = 'Actual CAC40 closing price')
#plt.title("CAC40 closing Price prediction- with MSE {:10.4f}".format(mse))
#plt.xlabel('Time')
#plt.ylabel('Price (EUR)')
#plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1410386965376782, 'fraction_numerical': 0.03004073319755601, 'mean_word_length': 7.323446327683616, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '254858', 'license': 'MIT', 'n_tokens_mistral': 2360, 'n_tokens_neox': 2185, 'n_words': 387, 'path': 'LSTM/LSTM_Vanilla-Final.ipynb', 'repo_name': 'HusseinLezzaik/Stock-Market-Prediction', 'size': '5892'}