whatsapp

whatsApp

Have any Questions? Enquiry here!
☎ +91-9972364704 LOGIN BLOG
× Home Careers Contact
Back
Stock Price Prediction – Machine Learning Project in Python
Stock Price Prediction – Machine Learning Project in Python

Introduction

Predicting how the stock market will perform is one of the most difficult things to do. There are so many factors involved in the prediction – physical factors vs. psychological, rational and irrational behavior, etc. All these aspects combine to make share prices volatile and very difficult to predict with a high degree of accuracy.

 

Problem Statement

Fundamental Analysis involves analyzing the company’s future profitability on the basis of its current business environment and financial performance.

Technical Analysis, on the other hand, includes reading the charts and using statistical figures to identify the trends in the stock market.

 

 

Machine learning has significant applications in the stock price prediction. In this machine learning project, we will be talking about predicting the returns on stocks. This is a very complex task and has uncertainties. We will develop this project on LSTM  neural network

 

we will learn how to predict stock price using the LSTM neural network.

Datasets

To build the stock price prediction model, we will use the NSE TATA GLOBAL dataset. This is a dataset of Tata Beverages from Tata Global Beverages Limited, National Stock Exchange of India: Tata Global Dataset

 

1. Imports:

 

import pandas as pd

import numpy as np

 

import matplotlib.pyplot as plt

%matplotlib inline

 

from matplotlib.pylab import rcParams

rcParams['figure.figsize']=20,10

from keras.models import Sequential

from keras.layers import LSTM,Dropout,Dense

 

 

from sklearn.preprocessing import MinMaxScaler  

2. Read the dataset:

 

df=pd.read_csv("NSE-TATA.csv")

df.head()

3. Analyze the closing prices from dataframe:

 

df["Date"]=pd.to_datetime(df.Date,format="%Y-%m-%d")

df.index=df['Date']

 

plt.figure(figsize=(16,8))

plt.plot(df["Close"],label='Close Price history')

 

 

4. Sort the dataset on date time and filter “Date” and “Close” columns:

 

data=df.sort_index(ascending=True,axis=0)

new_dataset=pd.DataFrame(index=range(0,len(df)),columns=['Date','Close'])

 

for i in range(0,len(data)):

 new_dataset["Date"][i]=data['Date'][i]

 new_dataset["Close"][i]=data["Close"][i]

 

5. Normalize the new filtered dataset:

 

scaler=MinMaxScaler(feature_range=(0,1))

final_dataset=new_dataset.values

 

train_data=final_dataset[0:987,:]

valid_data=final_dataset[987:,:]

 

new_dataset.index=new_dataset.Date

new_dataset.drop("Date",axis=1,inplace=True)

scaler=MinMaxScaler(feature_range=(0,1))

scaled_data=scaler.fit_transform(final_dataset)

 

x_train_data,y_train_data=[],[]

 

for i in range(60,len(train_data)):

 x_train_data.append(scaled_data[i-60:i,0])

 y_train_data.append(scaled_data[i,0])

 

x_train_data,y_train_data=np.array(x_train_data),np.array(y_train_data)

 

x_train_data=np.reshape(x_train_data,(x_train_data.shape[0],x_train_data.shape[1],1))

 

6. Build and train the LSTM model:

 

 

lstm_model=Sequential()

lstm_model.add(LSTM(units=50,return_sequences=True,input_shape=(x_train_data.shape[1],1)))

lstm_model.add(LSTM(units=50))

lstm_model.add(Dense(1))

 

inputs_data=new_dataset[len(new_dataset)-len(valid_data)-60:].values

inputs_data=inputs_data.reshape(-1,1)

inputs_data=scaler.transform(inputs_data)

 

lstm_model.compile(loss='mean_squared_error',optimizer='adam')

lstm_model.fit(x_train_data,y_train_data,epochs=1,batch_size=1,verbose=2)

 

 

7. Take a sample of a dataset to make stock price predictions using the LSTM model:

 

X_test=[]

for i in range(60,inputs_data.shape[0]):

 X_test.append(inputs_data[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))

predicted_closing_price=lstm_model.predict(X_test)

predicted_closing_price=scaler.inverse_transform(predicted_closing_price)

 

8. Save the LSTM model:

 

lstm_model.save("saved_model.h5")

 

9. Visualize the predicted stock costs with actual stock costs:

 

train_data=new_dataset[:987]

valid_data=new_dataset[987:]

valid_data['Predictions']=predicted_closing_price

plt.plot(train_data["Close"])

plt.plot(valid_data[['Close',"Predictions"]])

 results

Note : Find the best solution for electronics components and technical projects ideas
keep in touch with our social media links as mentioned below
Mifratech websites : https://www.mifratech.com/public/
Mifratech facebook : https://www.facebook.com/mifratech.lab
mifratech instagram : https://www.instagram.com/mifratech/
mifratech twitter account : https://twitter.com/mifratech

latest engineering projects on data science

 

engineering projects on machine learning

latest engineering projects on data science

engineering projects on machine learning

 

best engineering projects on machine learning

best engineering projects on machine learning

best projects on machine learning

best projects in deep learning

best machine learning projects for resume

best machine learning projects for final year

best machine learning projects for beginners

best machine learning projects for portfolio

best machine learning projects for jobs

best machine learning projects github

best projects in machine learning

best machine learning projects with source code

best deep learning projects for resume

best deep learning projects github

best deep learning research projects

best machine learning project ideas

best machine learning projects

best ml projects for resume

top 5 machine learning projects for beginners

top 10 machine learning projects for beginners

best ai projects for beginners

 

best ml projects for final year students

best engineering projects on machine learning

best projects on machine learning

best projects in deep learning

best machine learning projects for resume

best machine learning projects for final year

best machine learning projects for beginners

best machine learning projects for portfolio

best machine learning projects for jobs

best machine learning projects github

best projects in machine learning

best machine learning projects with source code

best deep learning projects for resume

best deep learning projects github

best deep learning research projects

best machine learning project ideas

best machine learning projects

best ml projects for resume

top 5 machine learning projects for beginners

top 10 machine learning projects for beginners

best ai projects for beginners

best ml projects for final year students

best project for machine learning

best ml projects for beginners

best machine learning tutorial for beginners

best machine learning course with projects

best machine learning projects in python

best machine learning projects on github

best machine learning programs online mifratech

top 10 machine learning projects for beginners in python mifratech

easy machine learning projects for beginners mifratech

 

Popular Coures