0% found this document useful (0 votes)
11 views

Stock Prediction

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Stock Prediction

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Create a program to predict stock prices using machine

learning or me series analysis techniques.

Crea ng a program to predict stock prices typically involves me series analysis and machine
learning techniques. A common approach is to use historical stock data and models like Long
Short-Term Memory (LSTM), ARIMA, or Facebook Prophet.
I'll outline a basic LSTM-based solu on using Python and TensorFlow/Keras to predict stock
prices. You'll need a stock price dataset, which you can get from Yahoo Finance or a similar
pla orm.

Here’s an overview of the process:

Steps:
1. Impor ng libraries
2. Stock Data Download Using yFinance
3. Data Preprocessing
4. Training and Tes ng Data Spli ng
5. Crea ng Sequences for LSTM Input
6. Reshaping Data
7. Building the LSTM Model
8. Compiling and Training the Model
9. Making Predic ons
10. Visualiza on of Results
11. Error Handling and Connec on Management

1|Page
1. Impor ng Libraries

 NumPy: A library for numerical compu ng, used for handling arrays and matrices.
 Pandas: A powerful data manipula on library, used to handle tabular data such as
stock prices.
 Matplotlib: A plo ng library used to visualize the stock data and predic ons.
 MinMaxScaler (from Scikit-Learn): A tool for normalizing data, scaling values to a range
(typically 0 to 1) to make the data suitable for training machine learning models.
 TensorFlow and Keras: Libraries used for building and training neural network models,
specifically LSTM (Long Short-Term Memory) networks for me series data.
 yFinance: A library used to download historical stock price data from Yahoo Finance.

2. Stock Data Download Using yFinance

 yFinance: Downloads stock data for a specified cker symbol over a specified date
range. This data includes columns such as 'Open', 'High', 'Low', 'Close', 'Adj Close', and
'Volume'.
 Purpose: This sec on fetches the historical data that will be used for training and
tes ng the machine learning model. The data consists of the stock’s price movements
over me.

3. Data Preprocessing

 Selec ng the 'Close' Prices: The code isolates the 'Close' prices because stock closing
prices are o en used as a target variable for predic on in me series modeling.
 Normaliza on: Using MinMaxScaler, the data is normalized to a range between 0 and
1. This is cri cal for LSTM models since neural networks perform be er when input
values are within a similar range.

2|Page
4. Training and Tes ng Data Spli ng

 Train-Test Split: The data is split into training (80%) and tes ng (20%) sets. The training
set is used to train the LSTM model, while the tes ng set is used to evaluate its
performance.

5. Crea ng Sequences for LSTM Input

 Crea ng Time Series Sequences: LSTM models require sequence data. The func on
create_sequences generates sequences of past stock prices (for example, the past 60
days of stock prices) to predict the next day's stock price.
o x_train: Input sequences for the model (e.g., the past 60 days of closing prices).
o y_train: The actual closing prices corresponding to the target day.

6. Reshaping Data

 Reshaping for LSTM: The input data is reshaped into a 3D array to meet the input
shape requirements of the LSTM model: (samples, mesteps, features). Here, each
sequence is treated as one sample, with each sample having 60 mesteps (days), and
each mestep containing one feature (closing price).

7. Building the LSTM Model

 Sequen al Model: This defines the neural network as a sequence of layers.


 LSTM Layers:

3|Page
o The first LSTM layer has 50 units and returns sequences
(return_sequences=True), meaning that the full output sequence is passed to
the next LSTM layer.
o The second LSTM layer also has 50 units but doesn't return sequences
(return_sequences=False), meaning only the final output is passed to the
subsequent layers.
 Dropout Layers: These layers prevent overfi ng by randomly se ng a frac on of the
input units to 0 during training.
 Dense Layers: The Dense layers are fully connected layers. The final Dense layer
outputs a single value (the predicted stock price).

8. Compiling and Training the Model

 Op mizer: The Adam op mizer is used to adjust the weights during training.
 Loss Func on: The model uses Mean Squared Error (MSE) as the loss func on, which is
standard for regression tasks like predic ng stock prices.
 Training: The model is trained on the training data for 25 epochs, with a batch size of
32 (meaning that the model updates weights every 32 samples).

9. Making Predic ons

 Predic ng: A er the model is trained, it makes predic ons on the tes ng data.
 Inverse Scaling: The predic ons are rescaled back to their original value range using
scaler.inverse_transform(), since the data was normalized before training.

10. Visualiza on of Results

4|Page
 Visualizing Predic ons: The plot shows how the predicted stock prices compare to the
actual prices. This allows you to visually assess the model’s performance.

11. Error Handling and Connec on Management

 Error Handling: This ensures that if an error occurs while processing one stock (e.g.,
connec on issues), the program won’t terminate but will print an error message and
con nue with the next stock.
 Sleep: The me.sleep(5) func on ensures that there is a 5-second delay between API
requests, preven ng overloading or exceeding rate limits.

Summary:

 Stock Data is downloaded using yFinance, preprocessed, and fed into an LSTM model.
 The LSTM Model is used to predict future stock prices based on past closing prices.
 Normaliza on is applied to the stock prices to improve model performance.
 The train-test split allows the model to be evaluated on unseen data.
 Visualiza ons are generated to compare the model's predic ons against actual stock
prices.

5|Page
CODE : -

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
import tensorflow as
from tensorflow.keras.models import Sequen al
from tensorflow.keras.layers import LSTM, Dense, Dropout
import yfinance as yf
import me

# List of stock symbols to analyze


stock_symbols = ['AAPL', 'GOOGL', 'MSFT', 'TSLA'] # Example stock symbols

# Func on to download and preprocess stock data, train LSTM, and visualize results
def predict_stock_price(stock_symbol):
print(f"Processing stock: {stock_symbol}")

try:
# 1. Load the dataset
data = yf.download(stock_symbol, start="2010-01-01", end="2023-01-01")

# If the data is empty, skip this stock


if data.empty:
print(f"No data found for {stock_symbol}")
return

6|Page
print(f"Data for {stock_symbol} downloaded successfully.")

# Displaying the stock data in the format you provided


print(data[['Low', 'Close', 'Adj Close', 'Volume']].head())

# 2. Data Preprocessing
closing_price = data['Close'].values.reshape(-1, 1)
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(closing_price)

training_size = int(len(scaled_data) * 0.8)


train_data = scaled_data[:training_size]
test_data = scaled_data[training_size:]

# Create training and test sequences


def create_sequences(data, seq_length):
x, y = [], []
for i in range(seq_length, len(data)):
x.append(data[i-seq_length:i, 0])
y.append(data[i, 0])
return np.array(x), np.array(y)

seq_length = 60
x_train, y_train = create_sequences(train_data, seq_length)
x_test, y_test = create_sequences(test_data, seq_length)

7|Page
x_train = x_train.reshape((x_train.shape[0], x_train.shape[1], 1))
x_test = x_test.reshape((x_test.shape[0], x_test.shape[1], 1))

# 3. Build and train the LSTM model


model = Sequen al([
LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1], 1)),
Dropout(0.2),
LSTM(units=50, return_sequences=False),
Dropout(0.2),
Dense(units=25),
Dense(units=1)
])

model.compile(op mizer='adam', loss='mean_squared_error')


model.fit(x_train, y_train, batch_size=32, epochs=25, verbose=1)

# 4. Make predic ons


predic ons = model.predict(x_test)
predic ons = scaler.inverse_transform(predic ons)

# Prepare the data for visualiza on


train = data[:training_size].copy()
valid = data[training_size:].copy()
valid = valid.iloc[seq_length:] # Skip first 60 rows of valid
valid['Predic ons'] = predic ons

# 5. Visualize the results

8|Page
plt.figure(figsize=(16,8))
plt. tle(f'Stock Price Predic on for {stock_symbol}')
plt.plot(train['Close'], label='Train')
plt.plot(valid['Close'], label='Actual')
plt.plot(valid['Predic ons'], label='Predicted')
plt.xlabel('Date')
plt.ylabel('Stock Price')
plt.legend(loc='lower right')
plt.show()

except Excep on as e:
print(f"Failed to process stock {stock_symbol}: {e}")

# Iterate through the stock symbols and predict prices for each stock
for stock in stock_symbols:
predict_stock_price(stock)
me.sleep(5) # Sleep for 5 seconds to avoid any connec on thro ling

9|Page
OUTPUT : -

10 | P a g e

You might also like