Skip to content

ManikaNagpal/AWS-Project-for-Stock-Price-Prediction-using-LSTM-Model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stock Price Prediction with LSTM using AWS SageMaker

This project demonstrates how to train an LSTM model to predict stock prices using historical data. The model is trained on Google Colab and deployed using AWS SageMaker. It will demonstrate how to deploy an LSTM model for stock price prediction using AWS SageMaker. It includes preprocessing a CSV file (test_input.csv), scaling it, and invoking a SageMaker endpoint for inference.

Project Structure

.
├── SageMaker.py             # Loads test_input.csv, scales & reshapes it, and invokes endpoint
├── predict.py               # Used inside SageMaker container for inference
├── scaler.pkl               # Fitted scaler for transforming input features
├── test_input.csv           # New input data (e.g., Open, High, Low columns)
└── lstm_model_deploy.tar.gz # Trained LSTM model to be deployed on SageMaker

Overview

  • Model Type: LSTM (Long Short-Term Memory)
  • Framework: TensorFlow/Keras
  • Deployment: AWS SageMaker Endpoint
  • Input Format: CSV file containing columns like Open, High, and Low
  • Output: Predicted stock prices (scaled or unscaled based on implementation)

How It Works

1. predict.py

This script runs inside the SageMaker container and:

  • Loads the LSTM model from /opt/ml/model
  • Accepts preprocessed input via JSON
  • Returns predictions as a JSON response

2. SageMaker.py

This is run on your SageMaker Notebook Instance, and it:

  • Reads test_input.csv
  • Uses scaler.pkl to transform the data
  • Builds time-step input for the LSTM
  • Sends the preprocessed data to the deployed endpoint
  • Receives predictions and optionally inverse-scales them

3. Input File: test_input.csv

Your CSV file should contain at least the following columns:

Open,High,Low
100,105,98
102,108,101
...

⚠️ At least 60 rows are required to form one LSTM input sample if your time step is set to 60.

Run Deployment & Prediction

1. Upload to S3

Upload the following files to your S3 bucket:

  • lstm_model_deploy.tar.gz
  • predict.py (used in the model container)
  • test_input.csv
  • scaler.pkl

You can upload using AWS CLI:

aws s3 cp lstm_model_deploy.tar.gz s3://your-bucket/your-folder/
aws s3 cp scaler.pkl s3://your-bucket/your-folder/
aws s3 cp predict.py s3://your-bucket/your-folder/
aws s3 cp test_input.csv s3://your-bucket/your-folder/

Or use Python boto3:

import boto3

s3 = boto3.client('s3')
bucket = 'your-bucket'
prefix = 'your-folder/'

s3.upload_file('lstm_model_deploy.tar.gz', bucket, prefix + 'lstm_model_deploy.tar.gz')
s3.upload_file('scaler.pkl', bucket, prefix + 'scaler.pkl')
s3.upload_file('predict.py', bucket, prefix + 'predict.py')
s3.upload_file('test_input.csv', bucket, prefix + 'test_input.csv')

2. SageMaker Notebook

On SageMaker Notebook, run:

python SageMaker.py

This will:

  • Deploy the model to a real-time SageMaker endpoint
  • Send test_input.csv (after scaling) as a request
  • Print the predicted values

Dependencies

  • Python 3.8+
  • boto3
  • sagemaker
  • pandas, numpy, tensorflow, scikit-learn

Notes

  • Ensure your LSTM model expects input shape (samples, 60, 3) or adjust accordingly.
  • Input preprocessing, including scaling and reshaping, is handled in SageMaker.py before the request is sent to the endpoint. The predict.py file inside the SageMaker model container only performs inference on already-preprocessed input.

Next Steps

  • Add visualization for predicted vs. actual prices
  • Automate daily prediction pipeline
  • Deploy with API Gateway for public access

About

In this AWS project, you will learn how to use an LSTM model for Stock Price Prediction and deploy the trained model using Amazon Web Services.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages