This script sets up a stock price prediction system using multiple machine learning models in Python. It begins by importing essential libraries, including yfinance
for fetching stock data, pandas
and numpy
for data manipulation, scikit-learn
for machine learning models and evaluation, and tensorflow
for building neural network models. matplotlib
is used for data visualization.
The data collection step involves defining a function, fetch_data
, which retrieves historical stock prices for a specified symbol over a specified date range using the Yahoo Finance API. The data preprocessing step involves cleaning the data by removing missing values, calculating daily returns, creating directional indicators, and generating lagged features for the adjusted close prices.
The data is then split into training and testing sets. In the model building step, three models are created: a linear regression model, a decision tree model, and a neural network model. Each model is trained using the training data.
In the model evaluation step, the script predicts stock prices using the testing data and evaluates the models’ performance using mean squared error (MSE) and mean absolute error (MAE). These metrics help assess the accuracy of the predictions made by each model.
Finally, the script visualizes the actual and predicted stock prices using matplotlib
. The plot includes lines representing the actual prices, linear regression predictions, decision tree predictions, and neural network predictions, providing a clear comparison of the models’ performance.
This setup allows for comprehensive data collection, preprocessing, model training, evaluation, and visualization, making it a robust tool for stock price prediction using multiple machine learning approaches.