0% found this document useful (0 votes)
8 views2 pages

Stock Market Prediction (1111)

The document outlines a Python script that predicts stock prices using LSTM neural networks. It allows users to input a CSV file containing stock market data, select a stock symbol, and specify a future date for prediction. The model is trained on historical stock prices and outputs a predicted price for the selected stock on the given date.

Uploaded by

swaranthib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Stock Market Prediction (1111)

The document outlines a Python script that predicts stock prices using LSTM neural networks. It allows users to input a CSV file containing stock market data, select a stock symbol, and specify a future date for prediction. The model is trained on historical stock prices and outputs a predicted price for the selected stock on the given date.

Uploaded by

swaranthib
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SOURCE CODE:

import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
file_path = input("Enter the path to the stock market CSV file: ")
data = pd.read_csv(file_path)
data.columns = data.columns.str.strip()
available_columns = data.columns.tolist()[1:]
print("Available stock symbols in dataset:", available_columns)
selected_column = input("Enter the stock symbol you want to analyze (e.g., 'AMZN', 'DPZ', 'BTC'): ")
if selected_column not in data.columns:
raise KeyError(f"Selected stock '{selected_column}' not found in dataset. Available stocks:
{available_columns}")
data['Date'] = pd.to_datetime(data['Date'])
date_input = input("Enter the future date to predict (YYYY-MM-DD): ")
selected_date = pd.to_datetime(date_input, errors='coerce')
prices = data[selected_column].values.reshape(-1, 1)
print(f"Using column '{selected_column}' for stock prices.")
scaler = MinMaxScaler(feature_range=(0,1))
prices_scaled = scaler.fit_transform(prices)
def create_sequences(data, seq_length):
sequences, labels = [], []
for i in range(len(data) - seq_length):
sequences.append(data[i:i+seq_length])
labels.append(data[i+seq_length])
return np.array(sequences), np.array(labels)
seq_length = 50 # User-defined sequence length
X, y = create_sequences(prices_scaled, seq_length)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = Sequential([
LSTM(50, return_sequences=True, input_shape=(seq_length, 1)),
LSTM(50, return_sequences=False),
Dense(25, activation='relu'),
Dense(1)
])
model.compile(optimizer='adam', loss='mean_squared_error')
model.fit(X_train, y_train, epochs=10, batch_size=16, verbose=1)
last_sequence = prices_scaled[-seq_length:].reshape(1, seq_length, 1)
predicted_price = scaler.inverse_transform(model.predict(last_sequence))
print(f"Predicted stock price for {selected_column} on {date_input}: {predicted_price[0][0]}")
OUTPUT

Enter the path to the stock market CSV file: /portfolio_data.csv


Available stock symbols in dataset: ['AMZN', 'DPZ', 'BTC', 'NFLX']
Enter the stock symbol you want to analyze (e.g., 'AMZN', 'DPZ', 'BTC'): AMZN
Enter the future date to predict (YYYY-MM-DD): 2025-03-26
Using column 'AMZN' for stock prices.
Epoch 1/10
74/74 ━━━━━━━━━━━━━━━━━━━━ 4s 23ms/step - loss: 0.0396
Epoch 2/10
74/74 ━━━━━━━━━━━━━━━━━━━━ 3s 30ms/step - loss: 6.7705e-04
Epoch 3/10
74/74 ━━━━━━━━━━━━━━━━━━━━ 2s 22ms/step - loss: 5.9535e-04
Epoch 4/10
74/74 ━━━━━━━━━━━━━━━━━━━━ 3s 23ms/step - loss: 6.3809e-04
Epoch 5/10
74/74 ━━━━━━━━━━━━━━━━━━━━ 3s 22ms/step - loss: 6.6923e-04
Epoch 6/10
74/74 ━━━━━━━━━━━━━━━━━━━━ 2s 22ms/step - loss: 5.3682e-04
Epoch 7/10
74/74 ━━━━━━━━━━━━━━━━━━━━ 2s 26ms/step - loss: 5.1026e-04
Epoch 8/10
74/74 ━━━━━━━━━━━━━━━━━━━━ 2s 22ms/step - loss: 6.3585e-04
Epoch 9/10
74/74 ━━━━━━━━━━━━━━━━━━━━ 2s 23ms/step - loss: 6.2295e-04
Epoch 10/10
74/74 ━━━━━━━━━━━━━━━━━━━━ 2s 23ms/step - loss: 4.2156e-04
1/1 ━━━━━━━━━━━━━━━━━━━━ 0s 217ms/step
Predicted stock price for AMZN on 2025-03-26: 1837.160400390625

You might also like