File size: 3,729 Bytes
d09cb6c
47d147b
 
 
 
 
 
85b11fb
7a5f7b9
47d147b
 
 
 
 
 
 
 
cd9604c
 
47d147b
 
 
e7ce3ee
 
 
016e17f
 
3aad77e
 
 
 
 
016e17f
 
 
3aad77e
 
 
 
 
016e17f
 
 
 
85b11fb
 
 
 
 
 
3f61e0e
 
 
 
 
 
 
 
 
 
 
 
4b965fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f61e0e
4b965fc
3f61e0e
 
 
45e7455
 
3f61e0e
45e7455
 
 
3f61e0e
 
 
 
 
 
 
 
 
 
45e7455
 
d14d4db
 
3f61e0e
 
3579bf6
 
3f61e0e
 
8ed6527
3f61e0e
 
 
b0cdab4
3f61e0e
b0cdab4
3f61e0e
8ed6527
7a5f7b9
3f61e0e
 
 
 
 
7a5f7b9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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)
df = df.reset_index()
df = df.drop(["Date","Adj Close"], axis=1)

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)

from keras.layers import Dense, Dropout, LSTM
from keras.models import Sequential

model = Sequential()
model.add(LSTM(units=50, activation="linear", return_sequences=True, input_shape = (x_train.shape[1],1)))
model.add(Dropout(0.2))

model.add(LSTM(units=60, activation="linear", return_sequences=True))
model.add(Dropout(0.3))

model.add(LSTM(units=80, activation="linear", return_sequences=True))
model.add(Dropout(0.4))

model.add(LSTM(units=120, activation="linear"))
model.add(Dropout(0.5))

model.add(Dense(units=1))

model.compile(optimizer="adam", loss="mean_squared_error")
model.fit(x_train, y_train, epochs=50)

model.save('keras_model1.h5')

#Load the model
#model = load_model("keras_model1.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)