In the financial investment field, predicting stock price movements has always been a coveted ability for investors, and Python, with its rich data analytics and machine learning libraries, provides us with powerful support for building stock price prediction models. Today, I’d like to share with you a practical example of using Python to build a stock price prediction model.
We select the historical stock price data of a well-known listed company as the object of study. The data covers the daily open price, close price, high price, low price, and volume over a period of time. The data can be obtained through the API of a financial data service provider or a publicly available financial data website. In this case, we assume that the acquired data has been stored as a CSV file containing columns such as Date Date Open Close Close High High Low and Volume.
First, import the necessary Python libraries such as Pandas and NumPy.
import pandas as pd import numpy as np read stock price data stock data pd read csv stock price csv view the first few rows of the data stock data head
Data preprocessing is a critical step in building an accurate model. We need to perform the following operations
Check for missing values in the data and process accordingly.
Check for missing values print stock data isnull sum If there are missing values, you can choose to delete or fill them For example, delete rows containing missing values stock data stock data dropna
Creating some new features may help to improve the predictive power of the model. For example, calculate the range of daily price fluctuations.
Calculate the price range stock data Price Range stock data High stock data Low Calculate the moving average, in this case the 5-day moving average stock data MA5 stock data Close rolling window 5 mean
Normalize data so that different features have the same scale.
from sklearn preprocessing import StandardScaler Select the column of features to be standardized features Open Close High Low Volume Price Range MA5 scaler StandardScaler stock data features scaler fit transform stock data features
In this case, we use the Long Short Term Memory Network LSTM to construct a stock price prediction model because LSTM has excellent performance in handling time series data.
from keras models import Sequential from keras layers import LSTM Dense Prepare the data for training Divide the data into input feature X and target variable y Here we use the data from the previous 60 days to predict the closing price on day 61 X y time steps 60 for i in range len stock data time steps X append stock data features iloc i i time steps values y append stock data Close iloc i time steps X np array X y np array y Constructing the LSTM model model Sequential model add LSTM units 50 return sequences True input shape time steps len features model add LSTM units 50 model add Dense units 1 compile model model compile optimizer adam loss mean squared error train model model fit X y epochs 100 batch size 32
After training, we need to evaluate the model and use it for stock price prediction.
Evaluate the performance of the model on the training set loss model evaluate X y print f Model loss loss Make a prediction First get the last time steps days of data as input last data stock data features iloc time steps values last data last data reshape 1 time steps len features predicted price model predict last data inverse normalize the predicted results to get the actual price range predicted price scaler inverse transform 0 len features 1 predicted price 0 0 0 1 print f Predicted closing price predicted price
It is important to note that the stock market is extremely complex and is affected by many factors, and while the models we build can make predictions based on historical data to a certain extent, they are not guaranteed to be absolutely accurate. This example is intended to demonstrate Python’s capabilities in financial data analysis and machine learning, and to help you understand how to build a basic stock price prediction model. In actual investment, more factors and expertise need to be taken into account.
In this case, we can see the power of Python in processing complex financial data, building machine learning models, and making data predictions. Whether you are a financial professional or a hobbyist interested in data analysis, you can use Python to explore the mysteries of the financial market and uncover the value behind the data.
Leave a Reply