Spaces:
Runtime error
Runtime error
Commit
·
3b24ace
1
Parent(s):
1a126b0
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,109 +1,120 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
-
import matplotlib.pyplot as plt
|
|
|
|
| 5 |
import yfinance as yf
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
from
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
start = '
|
| 12 |
-
end = '
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
st.write(df.describe())
|
| 23 |
-
|
| 24 |
-
st.subheader("Closing Price VS Time Chart:")
|
| 25 |
-
fig = plt.figure(figsize=(12,6))
|
| 26 |
-
plt.plot(df.Close, label="Closing Price")
|
| 27 |
-
plt.legend()
|
| 28 |
-
st.pyplot(fig)
|
| 29 |
-
|
| 30 |
-
moving_avg_100 = df.Close.rolling(100).mean()
|
| 31 |
-
st.subheader("Closing Price VS Time Chart With 100Moving Average:")
|
| 32 |
-
fig = plt.figure(figsize=(12,6))
|
| 33 |
-
plt.plot(df.Close, label="Closing Price")
|
| 34 |
-
plt.plot(moving_avg_100,'red', label="100 Moving Average")
|
| 35 |
-
plt.legend()
|
| 36 |
-
st.pyplot(fig)
|
| 37 |
-
|
| 38 |
-
moving_avg_200 = df.Close.rolling(200).mean()
|
| 39 |
-
st.subheader("Closing Price VS Time Chart With 100Moving Average and 200Moving Average:")
|
| 40 |
-
fig = plt.figure(figsize=(12,6))
|
| 41 |
-
plt.plot(df.Close, label="Closing Price")
|
| 42 |
-
plt.plot(moving_avg_100,'red', label="100 Moving Average")
|
| 43 |
-
plt.plot(moving_avg_200,'green', label="200 Moving Average")
|
| 44 |
-
plt.legend()
|
| 45 |
-
st.pyplot(fig)
|
| 46 |
-
|
| 47 |
-
#Spliting Data in Training and Testing Data
|
| 48 |
-
data_training = pd.DataFrame(df["Close"][0:int(len(df)*0.70)])
|
| 49 |
-
data_testing = pd.DataFrame(df["Close"][int(len(df)*0.70):int(len(df))])
|
| 50 |
-
|
| 51 |
-
#Scaling
|
| 52 |
-
scaler = MinMaxScaler(feature_range=(0,1))
|
| 53 |
-
data_training_arr = scaler.fit_transform(data_training)
|
| 54 |
-
|
| 55 |
-
#Split data in x_train and y_train
|
| 56 |
-
x_train = []
|
| 57 |
-
y_train = []
|
| 58 |
-
for i in range(100, data_training_arr.shape[0]):
|
| 59 |
-
x_train.append(data_training_arr[i-100: i])
|
| 60 |
-
y_train.append(data_training_arr[i, 0])
|
| 61 |
-
x_train, y_train = np.array(x_train), np.array(y_train)
|
| 62 |
-
|
| 63 |
-
#Load the model
|
| 64 |
-
model = load_model("keras_model1.h5")
|
| 65 |
-
|
| 66 |
-
past_100_days = data_training.tail(100)
|
| 67 |
-
final_test_df=pd.concat([past_100_days,data_testing],ignore_index=True)
|
| 68 |
-
print("Final_test_df")
|
| 69 |
-
print(final_test_df)
|
| 70 |
-
input_data = scaler.fit_transform(final_test_df)
|
| 71 |
-
print("input_data")
|
| 72 |
-
print(input_data.shape)
|
| 73 |
-
print(input_data)
|
| 74 |
-
|
| 75 |
-
#Split data in x_test and y_test
|
| 76 |
-
x_test = []
|
| 77 |
-
y_test = []
|
| 78 |
-
|
| 79 |
-
for i in range(100, input_data.shape[0]):
|
| 80 |
-
x_test.append(input_data[i-100: i])
|
| 81 |
-
y_test.append(input_data[i, 0])
|
| 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 |
-
plt.
|
| 108 |
-
plt.
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import pandas_datareader as data
|
| 5 |
import yfinance as yf
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
|
| 8 |
+
from keras.models import load_model
|
| 9 |
+
import streamlit as st
|
| 10 |
+
|
| 11 |
+
start = '2010-01-01'
|
| 12 |
+
end = '2023-7-30'
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
st.title('Stock Future Predicter')
|
| 16 |
+
|
| 17 |
+
use_input = st.text_input('Enter stock Ticker', 'AAPL')##############
|
| 18 |
+
|
| 19 |
+
if st.button('Predict'):
|
| 20 |
+
df = yf.download(use_input, start ,end )
|
| 21 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
#describing data
|
| 24 |
+
st.subheader('Data From 2010-2023')
|
| 25 |
+
st.write(df.describe())
|
| 26 |
+
|
| 27 |
+
#maps
|
| 28 |
+
|
| 29 |
+
st.subheader('closing Price VS Time Chart ')
|
| 30 |
+
fig = plt.figure(figsize=(10,5))
|
| 31 |
+
plt.plot(df.Close , color = 'yellow')
|
| 32 |
+
plt.legend()
|
| 33 |
+
st.pyplot(fig)
|
| 34 |
+
|
| 35 |
+
st.subheader('closing Price VS Time Chart with 100 moving Average ')
|
| 36 |
+
ma100= df.Close.rolling(100).mean()
|
| 37 |
+
fig = plt.figure(figsize=(10,5))
|
| 38 |
+
plt.plot(ma100, color = 'red')
|
| 39 |
+
plt.plot(df.Close , color = 'yellow')
|
| 40 |
+
plt.legend()
|
| 41 |
+
st.pyplot(fig)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
st.subheader('closing Price VS Time Chart with 100 & 200 moving Average ')
|
| 45 |
+
ma100= df.Close.rolling(100).mean()
|
| 46 |
+
ma200= df.Close.rolling(200).mean()
|
| 47 |
+
fig = plt.figure(figsize=(10,5))
|
| 48 |
+
plt.plot(ma100 , color = 'red')
|
| 49 |
+
plt.plot(ma200, color = 'green')
|
| 50 |
+
plt.plot(df.Close , color = 'yellow')
|
| 51 |
+
plt.legend()
|
| 52 |
+
st.pyplot(fig)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
#spltting data into train test
|
| 56 |
+
data_training = pd.DataFrame(df['Close'][0:int(len(df)*0.70)])
|
| 57 |
+
data_testing = pd.DataFrame(df['Close'][int(len(df)*0.70):int(len(df))])
|
| 58 |
+
|
| 59 |
+
print(' taining ', data_training.shape)
|
| 60 |
+
print(' testing ', data_testing.shape)
|
| 61 |
+
|
| 62 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 63 |
+
scaler = MinMaxScaler(feature_range = (0,1))
|
| 64 |
+
|
| 65 |
+
data_training_array = scaler.fit_transform(data_training)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
#load Model
|
| 71 |
+
|
| 72 |
+
model = load_model('model.h5')
|
| 73 |
+
|
| 74 |
+
#testing past
|
| 75 |
+
pass_100_days = data_training.tail(100)
|
| 76 |
+
|
| 77 |
+
final_df = pd.concat([pass_100_days, data_testing], ignore_index=True)
|
| 78 |
+
|
| 79 |
+
input_data = scaler.fit_transform(final_df)
|
| 80 |
+
|
| 81 |
+
x_test = []
|
| 82 |
+
y_test = []
|
| 83 |
+
|
| 84 |
+
for i in range(100 , input_data.shape[0]):
|
| 85 |
+
x_test.append(input_data[i-100:i])
|
| 86 |
+
y_test.append(input_data[i,0])
|
| 87 |
+
x_test, y_test = np.array(x_test), np.array(y_test)
|
| 88 |
+
|
| 89 |
+
y_predicted = model.predict(x_test)
|
| 90 |
+
|
| 91 |
+
scaler = scaler.scale_
|
| 92 |
+
scale_factor = 1/scaler[0]
|
| 93 |
+
y_predicted = y_predicted*scale_factor
|
| 94 |
+
y_test = y_test*scale_factor
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
#final graph
|
| 98 |
+
def plot_transparent_graph():
|
| 99 |
+
st.subheader('prediction vs Original')
|
| 100 |
+
fig2 = plt.figure(figsize= (12,6))
|
| 101 |
+
plt.plot(y_test , 'b', label = 'Original Price')
|
| 102 |
+
plt.plot(y_predicted , 'r', label = 'prdicted Price')
|
| 103 |
+
plt.style.use('dark_background')
|
| 104 |
+
plt.xlabel('time')
|
| 105 |
+
plt.ylabel('price')
|
| 106 |
+
plt.legend()
|
| 107 |
+
st.pyplot(fig2)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def main():
|
| 111 |
+
st.title('Stock Price Predicted Analysis')
|
| 112 |
+
|
| 113 |
+
# Call the function to plot the transparent graph
|
| 114 |
+
plot_transparent_graph()
|
| 115 |
+
|
| 116 |
+
# Other interactive elements and text can be added here as needed
|
| 117 |
+
# ...
|
| 118 |
+
|
| 119 |
+
if __name__ == "__main__":
|
| 120 |
+
main()
|