Spaces:
Sleeping
Sleeping
Commit
·
bde8b9e
1
Parent(s):
b3068d0
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import numpy as np
|
2 |
import pandas as pd
|
3 |
import matplotlib.pyplot as plt
|
@@ -6,7 +7,7 @@ yf.pdr_override()
|
|
6 |
from pandas_datareader import data as pdr
|
7 |
import tensorflow as tf
|
8 |
from keras.models import load_model
|
9 |
-
|
10 |
|
11 |
# Start and the End dates and the stock ticker
|
12 |
start = '2005-01-01'
|
@@ -17,91 +18,78 @@ st.title("Stock Market Trend Predictor")
|
|
17 |
use_input = st.text_input('Enter stock Ticker', stock_ticker)
|
18 |
if st.button('Analyze'):
|
19 |
df = pdr.get_data_yahoo(use_input, start)
|
20 |
-
sorted_df = df.sort_index(ascending=False)
|
21 |
|
22 |
-
#
|
23 |
st.subheader("Data from year 2005 to till date:")
|
24 |
-
st.dataframe(
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
plt.plot(df.Close , color = 'yellow')
|
31 |
plt.legend()
|
32 |
st.pyplot(fig)
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
plt.
|
38 |
-
plt.plot(df.Close
|
|
|
39 |
plt.legend()
|
40 |
st.pyplot(fig)
|
41 |
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
fig = plt.figure(figsize=(10,5))
|
47 |
-
plt.plot(
|
48 |
-
plt.plot(
|
49 |
-
plt.plot(
|
50 |
plt.legend()
|
51 |
st.pyplot(fig)
|
52 |
|
53 |
-
|
54 |
-
#spltting data into train test
|
55 |
data_training = pd.DataFrame(df['Close'][0:int(len(df)*0.70)])
|
56 |
data_testing = pd.DataFrame(df['Close'][int(len(df)*0.70):int(len(df))])
|
57 |
|
58 |
-
|
59 |
-
print(' testing ', data_testing.shape)
|
60 |
-
|
61 |
-
from sklearn.preprocessing import MinMaxScaler
|
62 |
scaler = MinMaxScaler(feature_range = (0,1))
|
63 |
-
|
64 |
data_training_array = scaler.fit_transform(data_training)
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
#load Model
|
70 |
-
|
71 |
model = load_model('model.h5')
|
72 |
|
73 |
-
#
|
74 |
-
|
75 |
-
|
76 |
final_df = pd.concat([pass_100_days, data_testing], ignore_index=True)
|
77 |
-
|
78 |
-
input_data = scaler.fit_transform(final_df)
|
79 |
|
80 |
x_test = []
|
81 |
y_test = []
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
y_test.append(input_data[i,0])
|
86 |
x_test, y_test = np.array(x_test), np.array(y_test)
|
87 |
|
|
|
88 |
y_predicted = model.predict(x_test)
|
89 |
|
|
|
90 |
scaler = scaler.scale_
|
91 |
scale_factor = 1/scaler[0]
|
92 |
y_predicted = y_predicted*scale_factor
|
93 |
y_test = y_test*scale_factor
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
st.subheader('prediction vs Original')
|
99 |
fig2 = plt.figure(figsize= (12,6))
|
100 |
-
plt.plot(y_test
|
101 |
-
plt.plot(y_predicted
|
102 |
-
plt.
|
103 |
-
plt.
|
104 |
-
plt.ylabel('price')
|
105 |
plt.legend()
|
106 |
st.pyplot(fig2)
|
107 |
|
@@ -109,11 +97,9 @@ if st.button('Analyze'):
|
|
109 |
def main():
|
110 |
st.title('Stock Price Predicted Analysis')
|
111 |
|
112 |
-
#
|
113 |
-
|
114 |
|
115 |
-
# Other interactive elements and text can be added here as needed
|
116 |
-
# ...
|
117 |
|
118 |
if __name__ == "__main__":
|
119 |
main()
|
|
|
1 |
+
import streamlit as st
|
2 |
import numpy as np
|
3 |
import pandas as pd
|
4 |
import matplotlib.pyplot as plt
|
|
|
7 |
from pandas_datareader import data as pdr
|
8 |
import tensorflow as tf
|
9 |
from keras.models import load_model
|
10 |
+
from sklearn.preprocessing import MinMaxScaler
|
11 |
|
12 |
# Start and the End dates and the stock ticker
|
13 |
start = '2005-01-01'
|
|
|
18 |
use_input = st.text_input('Enter stock Ticker', stock_ticker)
|
19 |
if st.button('Analyze'):
|
20 |
df = pdr.get_data_yahoo(use_input, start)
|
|
|
21 |
|
22 |
+
#View Data
|
23 |
st.subheader("Data from year 2005 to till date:")
|
24 |
+
st.dataframe(df.sort_index(ascending=False),use_container_width=True)
|
25 |
|
26 |
+
#Plot Graph for Closing Price Vs the Time
|
27 |
+
st.subheader("Closing Price VS Time Chart:")
|
28 |
+
fig = plt.figure(figsize=(12,6))
|
29 |
+
plt.plot(df.Close,label="Closing Price")
|
|
|
30 |
plt.legend()
|
31 |
st.pyplot(fig)
|
32 |
|
33 |
+
#Plot Graph for Closing Price Vs the Time with 100 Moving Average
|
34 |
+
moving_avg_100 = df.Close.rolling(100).mean()
|
35 |
+
st.subheader("Closing Price VS Time Chart With 100Moving Average:")
|
36 |
+
fig = plt.figure(figsize=(12,6))
|
37 |
+
plt.plot(df.Close, label="Closing Price")
|
38 |
+
plt.plot(moving_avg_100,'red', label="100 Moving Average")
|
39 |
plt.legend()
|
40 |
st.pyplot(fig)
|
41 |
|
42 |
|
43 |
+
#Plot Graph for Closing Price Vs the Time with 100 moving Average and 200 Moving Average
|
44 |
+
moving_avg_200 = df.Close.rolling(200).mean()
|
45 |
+
st.subheader("Closing Price VS Time Chart With 100Moving Average and 200Moving Average:")
|
46 |
fig = plt.figure(figsize=(10,5))
|
47 |
+
plt.plot(df.Close, label="Closing Price")
|
48 |
+
plt.plot(moving_avg_100,'red', label="100 Moving Average")
|
49 |
+
plt.plot(moving_avg_200,'green', label="200 Moving Average")
|
50 |
plt.legend()
|
51 |
st.pyplot(fig)
|
52 |
|
53 |
+
#Spliting Data in Training and Testing Data
|
|
|
54 |
data_training = pd.DataFrame(df['Close'][0:int(len(df)*0.70)])
|
55 |
data_testing = pd.DataFrame(df['Close'][int(len(df)*0.70):int(len(df))])
|
56 |
|
57 |
+
#Scale the training data between 0 and 1
|
|
|
|
|
|
|
58 |
scaler = MinMaxScaler(feature_range = (0,1))
|
|
|
59 |
data_training_array = scaler.fit_transform(data_training)
|
60 |
|
61 |
+
#Load the pre-trained model
|
|
|
|
|
|
|
|
|
62 |
model = load_model('model.h5')
|
63 |
|
64 |
+
#Testing Past
|
65 |
+
past_100_days = data_training.tail(100)
|
|
|
66 |
final_df = pd.concat([pass_100_days, data_testing], ignore_index=True)
|
67 |
+
input_test_data = scaler.fit_transform(final_df)
|
|
|
68 |
|
69 |
x_test = []
|
70 |
y_test = []
|
71 |
+
for i in range(100 , input_test_data.shape[0]):
|
72 |
+
x_test.append(input_test_data[i-100:i])
|
73 |
+
y_test.append(input_test_data[i,0])
|
|
|
74 |
x_test, y_test = np.array(x_test), np.array(y_test)
|
75 |
|
76 |
+
#Make Predictions
|
77 |
y_predicted = model.predict(x_test)
|
78 |
|
79 |
+
#Get the scale factor from the scaler and get the original value from the scaled values
|
80 |
scaler = scaler.scale_
|
81 |
scale_factor = 1/scaler[0]
|
82 |
y_predicted = y_predicted*scale_factor
|
83 |
y_test = y_test*scale_factor
|
84 |
|
85 |
+
#Plot Final Graph
|
86 |
+
def plot_final_graph():
|
87 |
+
st.subheader("Original Stock Price Vs Predicted Stock Price:")
|
|
|
88 |
fig2 = plt.figure(figsize= (12,6))
|
89 |
+
plt.plot(y_test, 'blue', label="Original Stock Price")
|
90 |
+
plt.plot(y_predicted, 'red', label="Predicted Stock Price")
|
91 |
+
plt.xlabel('Time')
|
92 |
+
plt.ylabel('Price')
|
|
|
93 |
plt.legend()
|
94 |
st.pyplot(fig2)
|
95 |
|
|
|
97 |
def main():
|
98 |
st.title('Stock Price Predicted Analysis')
|
99 |
|
100 |
+
#Call the function to plot the final graph
|
101 |
+
plot_final_graph()
|
102 |
|
|
|
|
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
main()
|