Spaces:
Runtime error
Runtime error
Commit
·
44e298c
1
Parent(s):
4e25940
Upload 3 files
Browse files- app (2).py +91 -0
- requirements.txt +11 -0
- stock_model.h5 +3 -0
app (2).py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pip install --upgrade pip
|
| 2 |
+
pip install -r requirements.txt
|
| 3 |
+
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import pandas_datareader as data
|
| 9 |
+
import yfinance as yf
|
| 10 |
+
from keras.models import load_model
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
st.title('Stock Trend predictor')
|
| 15 |
+
|
| 16 |
+
user_input = st.text_input('Enter Stock Ticker','AAPL' )
|
| 17 |
+
script = yf.download(tickers=user_input,period='15y',interval='1d')
|
| 18 |
+
|
| 19 |
+
#describing data
|
| 20 |
+
st.subheader('Data of last 15 years')
|
| 21 |
+
st.write(script.describe())
|
| 22 |
+
|
| 23 |
+
#visualization
|
| 24 |
+
st.subheader('closing price vs time chart')
|
| 25 |
+
fig = plt.figure(figsize=(17,7))
|
| 26 |
+
plt.plot(script.Close, 'b')
|
| 27 |
+
st.pyplot(fig)
|
| 28 |
+
|
| 29 |
+
st.subheader('Closing price vs time chart with 100MA')#100 ma
|
| 30 |
+
ma100 = script.Close.rolling(100).mean()
|
| 31 |
+
fig = plt.figure(figsize=(17,7))
|
| 32 |
+
plt.plot(ma100, 'g')
|
| 33 |
+
plt.plot(script.Close, 'b')
|
| 34 |
+
st.pyplot(fig)
|
| 35 |
+
|
| 36 |
+
st.subheader('Closing price vs time chart with 100MA and 200MA')#200&100 ma
|
| 37 |
+
ma200 = script.Close.rolling(200).mean()
|
| 38 |
+
#ma100 = script.Close.rolling(100).mean()
|
| 39 |
+
fig = plt.figure(figsize=(17,7))
|
| 40 |
+
plt.plot(ma200, 'r')
|
| 41 |
+
plt.plot(ma100, 'g')
|
| 42 |
+
plt.plot(script.Close, 'b')
|
| 43 |
+
st.pyplot(fig)
|
| 44 |
+
|
| 45 |
+
# splitting data into training and testing
|
| 46 |
+
|
| 47 |
+
data_training = pd.DataFrame(script['Close'][0:int(len(script)*0.70)])
|
| 48 |
+
data_testing = pd.DataFrame(script['Close'][int(len(script)*0.70): int(len(script))])
|
| 49 |
+
|
| 50 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 51 |
+
scaler = MinMaxScaler(feature_range=(0,1))
|
| 52 |
+
|
| 53 |
+
data_training_array = scaler.fit_transform(data_training)
|
| 54 |
+
|
| 55 |
+
#load my model
|
| 56 |
+
model = load_model('stock_model.h5')
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
#testing part
|
| 61 |
+
past_100_days = data_training.tail(100)
|
| 62 |
+
final_script = pd.concat([past_100_days, data_testing], ignore_index=True)
|
| 63 |
+
input_data = scaler.fit_transform(final_script)
|
| 64 |
+
|
| 65 |
+
x_test = []
|
| 66 |
+
y_test = []
|
| 67 |
+
|
| 68 |
+
for i in range(100, input_data.shape[0]):
|
| 69 |
+
x_test.append(input_data[i-100: i])
|
| 70 |
+
y_test.append(input_data[i, 0])
|
| 71 |
+
|
| 72 |
+
x_test, y_test = np.array(x_test), np.array(y_test)
|
| 73 |
+
|
| 74 |
+
# making prediction
|
| 75 |
+
|
| 76 |
+
y_predicted = model.predict(x_test)
|
| 77 |
+
scaler.scale_
|
| 78 |
+
|
| 79 |
+
scale_factor = 1/scaler.scale_
|
| 80 |
+
y_predicted = y_predicted * scale_factor
|
| 81 |
+
y_test = y_test * scale_factor
|
| 82 |
+
#final graph
|
| 83 |
+
|
| 84 |
+
st.subheader('Prediction vs original')
|
| 85 |
+
fig2 = plt.figure(figsize=(17,7))
|
| 86 |
+
plt.plot(y_test, 'b', label = 'Original Price')
|
| 87 |
+
plt.plot(y_predicted, 'r', label = 'Predicted Price')
|
| 88 |
+
plt.xlabel('Time')
|
| 89 |
+
plt.ylabel('Price')
|
| 90 |
+
plt.legend()
|
| 91 |
+
st.pyplot(fig2)
|
requirements.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
numpy
|
| 3 |
+
pandas
|
| 4 |
+
matplotlib
|
| 5 |
+
pandas_datareader
|
| 6 |
+
yfinance
|
| 7 |
+
keras.models
|
| 8 |
+
tensorflow
|
| 9 |
+
sklearn
|
| 10 |
+
|
| 11 |
+
|
stock_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6a544bf19f84d80f99017ddde84028ebb0508375f9baa55c222a5f6a7a3ddf76
|
| 3 |
+
size 2214648
|