Stock Price Prediction using Machine Learning

Machine Learning Project for beginners on Stock Price Prediction.

Predicting the stock market is one of the most important applications of Machine Learning in finance. In this article, I will take you through a simple Data Science project on Stock Price Prediction using Machine Learning Python.

At the end of this article, you will learn how to predict stock prices by using the Linear Regression model by implementing the Python programming language.

Also, Read – Machine Learning Full Course for free.

Stock Price Prediction

Predicting the stock market has been the bane and goal of investors since its inception. Every day billions of dollars are traded on the stock exchange, and behind every dollar is an investor hoping to make a profit in one way or another.

Entire companies rise and fall daily depending on market behaviour. If an investor is able to accurately predict market movements, he offers a tantalizing promise of wealth and influence. 

Today, so many people are making money staying at home trading in the stock market. It is a plus point for you if you use your experience in the stock market and your machine learning skills for the task of stock price prediction.

Let’s see how to predict stock prices using Machine Learning and the python programming language. I will start this task by importing all the necessary python libraries that we need for this task:

Data Preparation

In the above section, I started the task of stock price prediction by importing the python libraries. Now I will write a function that will prepare the dataset so that we can fit it easily in the Linear Regression model:

You can easily understand the above function as I have narrated the functioning of every line step by step. Now the next thing to do is reading the data:

df = pd.read_csv("prices.csv")
df = df[df.symbol == "GOOG"]

Now we need to prepare three input variables as already prepared in the function created in the above section. We need to declare an input variable mentioning about which column we want to predict. The next variable we need to declare is how much far we want to predict.

And the last variable that we need to declare is how much should be the size of the test set. Now let’s declare all the variables:

forecast_col = 'close'
forecast_out = 5
test_size = 0.2

Applying Machine Learning for Stock Price Prediction

Now I will split the data and fit into the linear regression model:

X_train, X_test, Y_train, Y_test , X_lately =prepare_data(df,forecast_col,forecast_out,test_size); #calling the method were the cross validation and data preperation is in
learner = LinearRegression() #initializing linear regression model,Y_train) #training the linear regression model

Now let’s predict the output and have a look at the prices of the stock prices:

{‘test_score’: 0.9481024935723803, ‘forecast_set’: array([786.54352516, 788.13020371, 781.84159626, 779.65508615, 769.04187979])}

So this is how we can predict the stock prices with Machine Learning. I hope you liked this article on Stock Price prediction using Python with machine learning by implementing the Linear Regression Model. Feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply