mystock / app.py
Shrikrishna's picture
Update app.py
d14d4db
raw
history blame
3.01 kB
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as yf
yf.pdr_override()
from pandas_datareader import data as pdr
from sklearn.preprocessing import MinMaxScaler
from keras.models import load_model
start = '2005-01-01'
end = '2022-12-31'
st.title("Stock Market Trend Predictor")
user_input = st.text_input("Enter the stock ticker", "TATAPOWER.NS")
df = pdr.get_data_yahoo(user_input, start, end)
st.subheader("Data from year 2005 to 2022:")
st.write(df.describe())
st.subheader("Closing Price VS Time Chart:")
fig = plt.figure(figsize=(12,6))
plt.plot(df.Close, label="Closing Price")
plt.legend()
st.pyplot(fig)
moving_avg_100 = df.Close.rolling(100).mean()
st.subheader("Closing Price VS Time Chart With 100Moving Average:")
fig = plt.figure(figsize=(12,6))
plt.plot(df.Close, label="Closing Price")
plt.plot(moving_avg_100,'red', label="100 Moving Average")
plt.legend()
st.pyplot(fig)
moving_avg_200 = df.Close.rolling(200).mean()
st.subheader("Closing Price VS Time Chart With 100Moving Average and 200Moving Average:")
fig = plt.figure(figsize=(12,6))
plt.plot(df.Close, label="Closing Price")
plt.plot(moving_avg_100,'red', label="100 Moving Average")
plt.plot(moving_avg_200,'green', label="200 Moving Average")
plt.legend()
st.pyplot(fig)
#Spliting Data in Training and Testing Data
data_training = pd.DataFrame(df["Close"][0:int(len(df)*0.70)])
data_testing = pd.DataFrame(df["Close"][int(len(df)*0.70):int(len(df))])
#Scaling
scaler = MinMaxScaler(feature_range=(0,1))
data_training_arr = scaler.fit_transform(data_training)
#Split data in x_train and y_train
x_train = []
y_train = []
for i in range(100, data_training_arr.shape[0]):
x_train.append(data_training_arr[i-100: i])
y_train.append(data_training_arr[i, 0])
x_train, y_train = np.array(x_train), np.array(y_train)
#Load the model
model = load_model("keras_model.h5")
past_100_days = data_training.tail(100)
final_test_df = past_100_days._append(data_testing, ignore_index=True)
print("Final_test_df")
print(final_test_df)
input_data = scaler.fit_transform(final_test_df)
print("input_data")
print(input_data.shape)
print(input_data)
#Split data in x_test and y_test
x_test = []
y_test = []
for i in range(100, input_data.shape[0]):
x_test.append(input_data[i-100: i])
y_test.append(input_data[i, 0])
x_test, y_test = np.array(x_test), np.array(y_test)
print(x_test.shape)
print(y_test.shape)
print("y_test")
print(y_test)
y_predicted = model.predict(x_test)
print("Predicted")
print(y_predicted)
sc = scaler.scale_
print(sc)
scale_factor = 1/sc[0]
y_predicted = y_predicted * scale_factor
print(y_predicted)
y_test = y_test * scale_factor
print(y_test)
st.subheader("Original Stock Price Vs Predicted Stock Price:")
fig2 = plt.figure(figsize=(12,6))
plt.plot(y_test, 'blue', label="Original Stock Price")
plt.plot(y_predicted, 'red', label="Predicted Stock Price")
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend()
st.pyplot(fig2)