Stock Price Prediction using ML algorithm

Introduction :-

In this tutorial we are going to implement a stock price prediction model using a machine learning algorithm. stock price prediction model as the name suggests, It predicts the price of the stock based on the different parameters like Open, High, Low, Close, etc.

So, I have trained this model using a Multi-Linear Regression model. It’s a simple Linear Regression model and it gave me a 5% RMSE (Root Mean Squared Error).


About dataset :-

The dataset is taken from NASDAQ, AAPL - Apple Inc company. Apple Inc. and its wholly-owned subsidiaries (collectively "Apple" or the  "Company") design, manufacture, and market personal computers, portable digital  music players, and mobile communication devices and sells a variety of related  software, services, peripherals, and networking solutions. The Company sells its  products worldwide through its online stores, its retail stores, its direct  sales force, and third-party wholesalers, resellers, and value-added resellers.

Let's start :-

First of all, import all the required libraries like pandas, numpy, matplotlib, seaborn for data visualisation and EDA.

Then load an APPL data set using the read_csv method of pandas and display the top five rows of the APPL data set.




Perform the exploratory data analysis. In EDA, Firstly check that there are Null values present or not in the data set using the isnull().sum() function. 
Now check the shape of the data set. As you can see in the below image that 1090 rows and 15 columns are present.


Then check the correlation of the function using the corr() method but by using the corr() method , it displays only the data frame, which is difficult to understand.


So to visualize the correlation using the heatmap() method of seaborn.


Then visualize each and every column of the data set using a hist() method.


Now create two new features HL_PCT and PCT_change from the existing features or domain knowledge.Then display the top 5 rows of the dataset after creating new features.

Now create a new data set with several features which are used to train the stock price prediction model on the APPL data set and again display the to 5 rows.



Then again check that there are null values present or not in the data set.

Now it's time to prepare the data set. divide the data set into the dependent and independent features. X contains all the independent features and Y contains our target or dependent feature. 

Now display the top 5 rows of X and Y data using the head() method.



Now split the data set into the training and testing using the train_test_split() method which takes some parameters like X, Y, test_size, random_state, etc. and returns 4 data sets: X_train , X_test , y_train , y_test.

And now check the shape of all the 4 data sets.

Now import the algorithm Linear Regression which is used to train the stock price prediction model and also load the Standard Scaler Which is used to normalise the data. It means that it converts the value into a particular range(0-1).

Now define the StandardScalar() and apply on the X_ train and X_test data set.

Now define the LinearRegression() model and train the model using the X_train and Y_train data set.



Now test the model using the test data set (X_test) and  compare the output with the actual output (y_test).


Now create a new data frame for comparing the actual and predicted data. 

Load the Evaluation Matrix and check the mean absolute error, mean squared error and root mean squared error. As You can see the output in the below image.



Source Code :-
  1. Go to my GitHub and fork or download the repo: Stock Price Prediction
  2. Then open .ipnyb file in Jupyter Notebook or Google Colab.

Video Tutorial :-



Thank you guys, I hope you like the post. If you like the post then comment and share with your friends.


If you have any doubts, Please let me know

Post a Comment (0)
Previous Post Next Post