![]() |
VOOZH | about |
Time series data such as stock prices are sequence that exhibits patterns such as trends and seasonality. Each data point in a time series is linked to a timestamp which shows the exact time when the data was observed or recorded. Many fields including finance, economics, weather forecasting and machine learning use this type of data. Due to these characteristics we can use Recurrent Neural Networks (RNN) for prediction as they work fine on sequential data.
To demonstrate the same we're going to use stock price data the most popular type of time series data.
We will import some libraries such as numpy, pandas, matplotlib, yfinance, scikit learn and tensorflow.
We are fetching data from yahoo finance using its API yfinance.
Normalizing data between 0 and 1 prevents large values from dominating smaller ones making training more efficient, improving model stability and convergence especially for deep learning models.
We will create a function to create a dataset for the training of our RNN model.
We will split the data into training and testing part into the ratio of 80% Training and 20% Testing data.
The model which we are using here is a Recurrent Neural Network (RNN). It s used for sequential data modeling such as time series forecasting.
The model is trained on historical data and used for making predictions.
The model’s performance is evaluated using standard error metrics. These metrics provide insights into how well the model's predictions align with actual values.
Output:
Mean Squared Error (MSE): 44.628821529273395
Root Mean Squared Error (RMSE): 6.680480636097481
Mean Absolute Error (MAE): 5.322556813557941
These values suggest that while the model is making reasonable predictions we can further fine tune like refining data preprocessing or using more advanced architectures like LSTM or GRU for better accuracy.
This visualization compares the actual stock prices with the predicted stock prices helping to assess the model’s performance visually.
Output:
The graph shows the performance of a RNN model in predicting Apple (AAPL) stock prices. The blue line represents the actual stock prices from the test data while the red line shows the predicted values. The RNN model successfully captures the underlying trends in the stock price with the predicted values closely following the actual prices. Although minor deviations are observed the overall prediction shows that model effectively learned the temporal patterns in stock data.