Time Series Forecasting using Recurrent Neural Networks (RNN) in TensorFlow (original) (raw)

Last Updated : 8 Oct, 2025

Time series data such as stock prices are sequence that exhibits patterns such as trends and seasonality. Each data point in atime 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.

1. Importing required libraries

We will import some libraries such as numpy, pandas, matplotlib, yfinance, scikit learn and tensorflow.

Python `

import numpy as np import pandas as pd import matplotlib.pyplot as plt import yfinance as yf from sklearn.metrics import mean_squared_error, mean_absolute_error from sklearn.preprocessing import MinMaxScaler from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, SimpleRNN

`

2. Fetching Data from Yahoo Finance

We are fetching data from yahoo finance using its API yfinance.

ticker = 'AAPL'
data = yf.download(ticker, start="2020-01-01", end="2025-01-01") data = data['Close'].values.reshape(-1, 1)

`

3. Normalizing the Data

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.

scaler = MinMaxScaler(feature_range=(0, 1)) scaled_data = scaler.fit_transform(data)

`

4. Preparing the Data for Time Series

We will create a function to create a dataset for the training of our RNN model.

def create_dataset(data, time_step=60): X, y = [], [] for i in range(len(data) - time_step - 1): X.append(data[i:(i + time_step), 0]) y.append(data[i + time_step, 0]) return np.array(X), np.array(y)

X, y = create_dataset(scaled_data) X = X.reshape(X.shape[0], X.shape[1], 1)

`

5. Splitting the Data into Training and Testing Sets

We will split the data into training and testing part into the ratio of80% Training and 20% Testing data.

Python `

train_size = int(len(X) * 0.8) X_train, X_test = X[:train_size], X[train_size:] y_train, y_test = y[:train_size], y[train_size:]

`

6. Building the RNN Model

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.

model = Sequential() model.add(SimpleRNN(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1))) model.add(SimpleRNN(units=50, return_sequences=False)) model.add(Dense(units=1)) model.compile(optimizer='adam', loss='mean_squared_error')

`

7. Training the Model

The model is trained on historical data and used for making predictions.

model.fit(X_train, y_train, epochs=20, batch_size=64)

predictions = model.predict(X_test) predictions = scaler.inverse_transform(predictions)

`

8. Evaluating the model

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.

y_test_unscaled = scaler.inverse_transform(y_test.reshape(-1, 1))

mse = mean_squared_error(y_test_unscaled, predictions) rmse = np.sqrt(mse) mae = mean_absolute_error(y_test_unscaled, predictions)

print(f"Unscaled Mean Squared Error (MSE): {mse}") print(f"Unscaled Root Mean Squared Error (RMSE): {rmse}") print(f"Unscaled Mean Absolute Error (MAE): {mae}")

`

**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.

9. Visualizing the Results

This visualization compares the actual stock prices with the predicted stock prices helping to assess the model’s performance visually.

Python `

plt.figure(figsize=(10,6)) plt.plot(scaler.inverse_transform(y_test.reshape(-1, 1)), color='blue', label='Real Stock Price') plt.plot(predictions, color='red', label='Predicted Stock Price') plt.title(f'{ticker} Stock Price Prediction') plt.xlabel('Time') plt.ylabel('Stock Price') plt.legend() plt.show()

`

**Output:

download

Model Prediction

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.