Stock Market Prediction using Machine Learning
Stock Market Prediction using Machine Learning
“With a good perspective on history, we can have a better understanding of the past and present, and thus a clear vision of the Future.”
— Carlos Slim Helu
Photo by Aditya Vyas on Unsplash
Stock market analysis and predictions are very difficult tasks to complete. There are numerous variables that influence the value of stock prices including, but not limited to, media publications, insider buying, notice of dividends, stock splits, quarterly financial reports and financial forecasts by management. These variables make it extremely difficult for any stock market professional to predict the rise and fall of stock prices with accuracy.
With the introduction of Machine Learning and its many advanced algorithms, the most recent market research and stock market prediction advancements have begun to include machine learning approaches in analyzing stock market data and predicting future stock prices. The successful prediction of a stock’s future price could yield a significant return on investment.
Disclaimer: this article is not intended to provide trading and investment advice. No warranties are made regarding the accuracy of the model. Readers are advised to due their own research before making any investment decisions.
The remainder of this article will present a program that will attempt to predict future stock prices based on previous closing prices using machine learning. We will use the linear regression model. The following steps will be performed using Python.
3. Import the dataset into a dataframe.
4. Analyze the data.
5. Restructure the data.
6. Split the data into training and test data sets.
7. Train the model on the training data.
8. Make predictions on the test data.
9. Visualize predictions for stock prices.
The objective of this program is to predict future stock prices based on previous closing prices for a company stock. In this case we will use Tesla.
We will start by installing the required packages and importing the libraries.
Install the Packages
Import the Libraries
Import the Data Set
We will use Yahoo Finance Market Downloader to get stock data for Tesla for the time period from 1/1/2021 thru 12/5/2021.
Let’s take a look at the data and show some information about the data elements.
<class 'pandas.core.frame.DataFrame'> DatetimeIndex: 233 entries, 2021-01-04 to 2021-12-03 Data columns (total 1 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 Close 233 non-null float64 dtypes: float64(1) memory usage: 3.6 KB
There are 233 stock records for Tesla within the specified time frame. There is a Date column (index) and a column named Close which has a float data type. The data set contains no null values.
Restructure the Data
We need to transform the data so we can feed the data into the machine learning model. We will restructure our data so that each row contains the actual stock price for one day followed be each of the closing stock prices for the previous 10 days. For example, beginning with the 11th row, we will get the previous 10 closing prices and use them for the features. The Pandas shift function will be used to get the close values from each of the previous 10 rows of data. This process will be repeated for each row of data.
Listed below is an image of the dataset after the data has been restructured.
Split Data into Training and Test Data Sets
Train the model and make predictions on the test data
We will use the Linear Regression model for training and to make predictions on the test data. Linear Regression is a supervised machine learning model in which the model finds the best fit linear line between the independent and dependent variables.
Visualize the Actual and Predicted Stock Prices
Let’s visualize and compare the actual stock prices and the predicted stock prices over the specified time period.
Conclusion: In general, the predicted stock prices and the actual stock prices show similar trends in direction. Many times the predicted stock prices are below the actual stock prices. On 11-23-2021 the actual stock price went down and the predicted stock price went up. On 12/03/2021 there was a significant drop in the actual stock price and a moderate decrease in the predicted stock price.
Thanks so much for reading my article! If you have any comments or feedback please let me know.
If you enjoy reading stories like these and want to support me as a writer, consider signing up to become a Medium member. Membership gives you unlimited access to stories on Medium. You can sign up using this link https://medium.com/@dniggl/membership