forked from developer-subhajit/stock_price_prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
81 lines (58 loc) · 2.6 KB
/
app.py
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
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import streamlit as st
import yfinance as yf
from keras.models import load_model
from sklearn.preprocessing import MinMaxScaler
st.title("Stock Price Predictor App")
stock = st.text_input("Enter the Stock ID", "GOOG")
from datetime import datetime
end = datetime.now()
start = datetime(end.year - 20, end.month, end.day)
google_data = yf.download(stock, start, end)
model = load_model("Latest_stock_price_model.keras")
st.subheader("Stock Data")
st.write(google_data)
splitting_len = int(len(google_data) * 0.7)
x_test = pd.DataFrame(google_data.Close[splitting_len:])
def plot_graph(figsize, values, full_data, extra_data=0, extra_dataset=None):
fig = plt.figure(figsize=figsize)
plt.plot(values, "Orange")
plt.plot(full_data.Close, "b")
if extra_data:
plt.plot(extra_dataset)
return fig
st.subheader("Original Close Price and MA for 250 days")
google_data["MA_for_250_days"] = google_data.Close.rolling(250).mean()
st.pyplot(plot_graph((15, 6), google_data["MA_for_250_days"], google_data, 0))
st.subheader("Original Close Price and MA for 200 days")
google_data["MA_for_200_days"] = google_data.Close.rolling(200).mean()
st.pyplot(plot_graph((15, 6), google_data["MA_for_200_days"], google_data, 0))
st.subheader("Original Close Price and MA for 100 days")
google_data["MA_for_100_days"] = google_data.Close.rolling(100).mean()
st.pyplot(plot_graph((15, 6), google_data["MA_for_100_days"], google_data, 0))
st.subheader("Original Close Price and MA for 100 days and MA for 250 days")
st.pyplot(plot_graph((15, 6), google_data["MA_for_100_days"], google_data, 1, google_data["MA_for_250_days"]))
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(x_test[["Close"]])
x_data = []
y_data = []
for i in range(100, len(scaled_data)):
x_data.append(scaled_data[i - 100 : i])
y_data.append(scaled_data[i])
x_data, y_data = np.array(x_data), np.array(y_data)
predictions = model.predict(x_data)
inv_pre = scaler.inverse_transform(predictions)
inv_y_test = scaler.inverse_transform(y_data)
ploting_data = pd.DataFrame(
{"original_test_data": inv_y_test.reshape(-1), "predictions": inv_pre.reshape(-1)},
index=google_data.index[splitting_len + 100 :],
)
st.subheader("Original values vs Predicted values")
st.write(ploting_data)
st.subheader("Original Close Price vs Predicted Close price")
fig = plt.figure(figsize=(15, 6))
plt.plot(pd.concat([google_data.Close[: splitting_len + 100], ploting_data], axis=0))
plt.legend(["Data- not used", "Original Test data", "Predicted Test data"])
st.pyplot(fig)