kailash2506 / rnn-stock-price-prediction

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stock Price Prediction

AIM

To develop a Recurrent Neural Network model for stock price prediction.

Problem Statement and Dataset

DESIGN STEPS

STEP 1:

Import the necessary tensorflow modules

STEP 2:

Load the stock dataset.

STEP 3:

Fit the model and then predict.

PROGRAM

Name: Kailash Kumar S

Register number: 212223220041

Importing modules

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from keras import layers
from keras.models import Sequential

Loading the training dataset

dataset_train = pd.read_csv('trainset.csv')

Reading only columns

dataset_train.columns

Displaying the first five rows of the dataset

dataset_train.head()

Selecting all rows and the column with index 1

train_set = dataset_train.iloc[:,1:2].values

Displaying the type of the training dataset

type(train_set)

Displaying the shape of the training dataset

train_set.shape

Scaling the dataset using MinMaxScaler

sc = MinMaxScaler(feature_range=(0,1))
training_set_scaled = sc.fit_transform(train_set)

Displaying the shape of the scaled training data set

training_set_scaled.shape
X_train_array = []
y_train_array = []
for i in range(60, 1259):
  X_train_array.append(training_set_scaled[i-60:i,0])
  y_train_array.append(training_set_scaled[i,0])
X_train, y_train = np.array(X_train_array), np.array(y_train_array)
X_train1 = X_train.reshape((X_train.shape[0], X_train.shape[1],1))

X_train.shape
length = 60
n_features = 1

Creating the model

model = Sequential()
model.add(layers.SimpleRNN(50, input_shape=(length, n_features)))
model.add(layers.Dense(1))

Compiling the model

model.compile(optimizer='adam', loss='mse')

Printing the summary of the model

model.summary()

Fitting the model

model.fit(X_train1,y_train,epochs=100, batch_size=32)

Reading the testing dataset

dataset_test = pd.read_csv('testset.csv')

Selecting all rows and the column with index 1

test_set = dataset_test.iloc[:,1:2].values

Displaying the shape of the testing data

test_set.shape

Concatenating the 'Open' columns from testing dataset and training dataset

dataset_total = pd.concat((dataset_train['Open'],dataset_test['Open']),axis=0)
inputs = dataset_total.values
inputs = inputs.reshape(-1,1)

Transforming the inputs

inputs_scaled=sc.transform(inputs)
X_test = []
for i in range(60,1384):
  X_test.append(inputs_scaled[i-60:i,0])
X_test = np.array(X_test)
X_test = np.reshape(X_test,(X_test.shape[0], X_test.shape[1],1))
X_test.shape
predicted_stock_price_scaled = model.predict(X_test)
predicted_stock_price = sc.inverse_transform(predicted_stock_price_scaled)

Plotting the graph between True Stock Price, Predicted Stock Price vs time

plt.plot(np.arange(0,1384),inputs, color='red', label = 'Test(Real) Google stock price')
plt.plot(np.arange(60,1384),predicted_stock_price, color='blue', label = 'Predicted Google stock price')
plt.title('Kailash Kumar S\n212223220041\nGoogle Stock Price Prediction')
plt.xlabel('Time')
plt.ylabel('Google Stock Price')
plt.legend()
plt.show()

OUTPUT

True Stock Price, Predicted Stock Price vs time

image

Mean Square Error

image

RESULT

Thus a Recurrent Neural Network model for stock price prediction is developed and implemented successfully.

About

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Jupyter Notebook 100.0%