Stock Price Prediction using RNN

Deep-learning

Objective

Predict future stock prices (or any time-series data) based on past prices using a Recurrent Neural Network.

 

Import Libraries

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.preprocessing import MinMaxScaler

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import SimpleRNN, Dense

 

Load or Create Sample Data

If you have real stock price data (like from Yahoo Finance), you can load it.
Here, we’ll generate a synthetic sine wave dataset that mimics stock price trends.

# Generate synthetic stock price data

np.random.seed(42)

time = np.arange(0, 100, 0.1)

price = np.sin(time) + np.random.normal(0, 0.1, len(time))  # sine wave + noise

 

# Convert to DataFrame

data = pd.DataFrame(price, columns=[‘Close’])

plt.plot(data[‘Close’])

plt.title(“Synthetic Stock Price Data”)

plt.show()

 

 

 

Data Preprocessing

We need to:

  1. Normalize data (RNNs learn better on scaled data)
  2. Create sequences (e.g., use past 10 prices → predict next price)

scaler = MinMaxScaler(feature_range=(0, 1))

scaled_data = scaler.fit_transform(data)

 

# Create sequences

sequence_length = 10

X, y = [], []

 

for i in range(len(scaled_data) – sequence_length):

X.append(scaled_data[i:i + sequence_length])

y.append(scaled_data[i + sequence_length])

 

X, y = np.array(X), np.array(y)

 

print(“X shape:”, X.shape)

print(“y shape:”, y.shape)

 

Output Example:

X shape: (990, 10, 1)

y shape: (990, 1)

 

Split into Training and Testing Sets

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:]

 

Build the RNN Model

Deep-learning Mid

We’ll use a simple RNN with 50 neurons and one dense output.

model = Sequential()

model.add(SimpleRNN(50, activation=’tanh’, input_shape=(X_train.shape[1], X_train.shape[2])))

model.add(Dense(1))

 

model.compile(optimizer=’adam’, loss=’mean_squared_error’)

model.summary()

Explanation:

  • SimpleRNN(50) — learns temporal dependencies from the past 10 stock prices.
  • Dense(1) — predicts a single future value (next stock price).
  • Loss: Mean Squared Error (MSE) is suitable for regression tasks.

 

Train the Model

history = model.fit(X_train, y_train, epochs=50, batch_size=16, validation_data=(X_test, y_test), verbose=1)

 

Visualize Training Results

plt.plot(history.history[‘loss’], label=’Train Loss’)

plt.plot(history.history[‘val_loss’], label=’Validation Loss’)

plt.legend()

plt.title(“Model Training Progress”)

plt.show()

 

Make Predictions

predicted = model.predict(X_test)

predicted_price = scaler.inverse_transform(predicted)

actual_price = scaler.inverse_transform(y_test)

 

plt.figure(figsize=(10,6))

plt.plot(actual_price, color=’red’, label=’Actual Price’)

plt.plot(predicted_price, color=’blue’, label=’Predicted Price’)

plt.title(‘Stock Price Prediction using RNN’)

plt.xlabel(‘Time’)

plt.ylabel(‘Price’)

plt.legend()

plt.show()

The closer the blue (predicted) and red (actual) lines are, the better your model performs.

 

Evaluate Model Performance

from sklearn.metrics import mean_squared_error

import math

 

rmse = math.sqrt(mean_squared_error(actual_price, predicted_price))

print(“Root Mean Squared Error:”, rmse)

 

Predict Next Future Price

# Take the last 10 prices from test data

last_sequence = X_test[-1]

last_sequence = np.expand_dims(last_sequence, axis=0)

 

# Predict next value

next_price = model.predict(last_sequence)

next_price = scaler.inverse_transform(next_price)

print(“Predicted next stock price:”, next_price[0][0])

 

Understanding How It Works

Step Concept What Happens
1 Time Series Data Sequential data (e.g., stock prices)
2 Sequence Creation Split into past 10 → next 1
3 Normalization Scale between 0 and 1
4 RNN Model Learns temporal dependencies
5 Training Adjust weights using backpropagation through time
6 Prediction Generates next step values

 

Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *