import numpy as np import matplotlib.pyplot as plt import pandas as pd
training_set = pd.read_csv('Google_Stock_Price_Train.csv') training_set = training_set.iloc[:, 1:2].values
from sklearn.preprocessing import MinMaxScaler sc = MinMaxScaler() training_set = sc.fit_transform(training_set)
#get input and output X_train = training_set[0:1257] y_train = training_set[1:1258]
#reshaping X_train = np.reshape(X_train, (1257,1, 1))
from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM
regressor = Sequential() #add lstm layer regressor.add(LSTM(units = 4, activation = 'sigmoid', input_shape = (None, 1)))
#add output layer regressor.add(Dense(units = 1))
#compiling regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')
#fitting rnn to training set regressor.fit(X_train, y_train, batch_size = 32, epochs= 200)
#get the real price 2017 test_set = pd.read_csv('Google_Stock_Price_Test.csv') real_stock_price = test_set.iloc[:, 1:2].values
#getting prediction inputs = real_stock_price inputs = sc.transform(inputs) inputs = np.reshape(inputs, (20,1, 1)) predicted_stock_price = regressor.predict(inputs) predicted_stock_price = sc.inverse_transform(predicted_stock_price)
#visulaising stock price plt.plot(real_stock_price, color = 'red', label = 'Real Google Stock Price') plt.plot(predicted_stock_price, color = 'blue', label = 'predicted Google Stock Price') plt.title('Google Stock Price Prediction') plt.xlabel('Time') plt.ylabel('Google Stock Price') plt.legend() plt.show()
#homework real_stock_price_train = pd.read_csv('Google_Stock_Price_Train.csv') real_stock_price_train = real_stock_price_train.iloc[:, 1:2].values
predicted_stock_price_train = regressor.predict(X_train) predicted_stock_price_train = sc.inverse_transform(predicted_stock_price_train)
#visulaising stock price plt.plot(real_stock_price_train, color = 'red', label = 'Real Google Stock Price') plt.plot(predicted_stock_price_train, color = 'blue', label = 'predicted Google Stock Price') plt.title('Google Stock Price Prediction') plt.xlabel('Time') plt.ylabel('Google Stock Price') plt.legend() plt.show()
#evaluating import math from sklearn.metrics import mean_squared_error rmse = math.sqrt(mean_squared_error(real_stock_price, predicted_stock_price))