rasa0704's picture
mdified with inversed scaled data
b48b5bf
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import pandas_datareader.data as web
import sklearn as sk
import datetime as dt
from datetime import date
import yfinance as yf
yf.pdr_override()
from keras.models import load_model
import streamlit as st
# defining the dates here
st_dt1 = dt.datetime(2010,1,1)
end_dt1 = dt.datetime(2019,12,31)
st_dt2 = dt.datetime(2021,1,1)
end_dt2 = date.today()
st.title('Stock Trend Prediction')
user_input = st.text_input('Enter Stock Tiker','AAPL')
df_stocks1 = web.get_data_yahoo( user_input, st_dt1, end_dt1 )
df_stocks2 = web.get_data_yahoo(user_input, st_dt2, end_dt2 )
df_stocks = pd.concat([df_stocks1,df_stocks2])
df = df_stocks.reset_index()
# Desribing the data
stk_des = "Data for "+str(user_input)+" from 2010 to 2023 exluding the Covid Year"
st.subheader(stk_des)
st.write(df.describe())
# visualization
st.subheader('The Closing price vs Time Chart')
fig = plt.figure(figsize=(12,6))
plt.plot(df.Close)
st.pyplot(fig)
# with Moving Averages
st.subheader('The Closing price vs Time Chart with MA')
ma100 = df.Close.rolling(100).mean()
ma200 = df.Close.rolling(200).mean()
fig = plt.figure(figsize=(12,6))
plt.plot(df.Close,'b',label='Original Price')
plt.plot(ma100,'r',label='MA100')
plt.plot(ma200,'g',label='MA200')
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend()
st.pyplot(fig)
# train-test split
df_train = pd.DataFrame(df['Close'][0:int(df.shape[0]*0.7)])
df_test = pd.DataFrame(df['Close'][int(df.shape[0]*0.7):int(df.shape[0])])
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0,1))
df_train_array = scaler.fit_transform(df_train)
# need to fix the step size of 100
x_train = []
y_train = []
for i in range(100,df_train_array.shape[0]):
x_train.append(df_train_array[i-100:i])
y_train.append(df_train_array[i,0])
x_train,y_train = np.array(x_train), np.array(y_train)
model = load_model("keras_model.h5")
past_100_days = df_train.tail(100)
df_final = pd.concat([past_100_days,df_test],ignore_index=True)
df_final = scaler.fit_transform(df_final)
x_test = []
y_test = []
for i in range(100,(int(df_final.shape[0])+1) ):
x_test.append(df_final[i-100:i])
for i in range(100,df_final.shape[0]):
# x_test.append(df_final[i-100:i])
y_test.append(df_final[i])
x_test, y_test = np.array(x_test), np.array(y_test)
y_pred = model.predict(x_test)
y_pred_tmp = y_pred
y_test_tmp = y_test
y_pred_tmp = scaler.inverse_transform(y_pred_tmp)
y_test_tmp = np.reshape(y_test_tmp,(-1,1))
y_test_tmp = scaler.inverse_transform(y_test_tmp)
scaler_val = scaler.scale_
scale_factor = 1/scaler_val
y_pred = y_pred*scale_factor
y_test = y_test*scale_factor
#plotting
st.subheader("Forecasted Trends vs Original Prie")
fig2 = plt.figure(figsize=(12,6))
plt.plot(y_test,'b',label='Original Price')
plt.plot(y_pred,'r',label='Predicted Price')
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend()
st.pyplot(fig2)
# also printing the next day closing price
last_predicted_price = round(float(y_pred_tmp[-1]),2)
actual_closing_price = round(float(y_test_tmp[-1]),2)
# ls = "Predicted Upcoming Trading Day Closing Price: "
st.subheader("Predicted Upcoming Trading Day Closing Price: ")
st.write(last_predicted_price)
st.subheader("Last Closing Price: ")
st.write(actual_closing_price)