ScorpionTaj
commited on
Commit
β’
2a15b67
1
Parent(s):
18f1934
Upload 3 files
Browse files- .gitattributes +1 -0
- Latest_stock_price_prediction.keras +3 -0
- app.py +135 -0
- stock_price.ipynb +0 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
Latest_stock_price_prediction.keras filter=lfs diff=lfs merge=lfs -text
|
Latest_stock_price_prediction.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9c7b4232ecf9af8750042dfd8a8bcb00373ff36ae510d37732868db155f403cc
|
3 |
+
size 1444539
|
app.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from keras.models import load_model
|
5 |
+
from sklearn.preprocessing import MinMaxScaler
|
6 |
+
from sklearn.metrics import mean_squared_error, r2_score
|
7 |
+
import yfinance as yf
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
from datetime import datetime
|
10 |
+
|
11 |
+
st.sidebar.title("Stock Price Prediction App π")
|
12 |
+
st.sidebar.subheader("About the Developer")
|
13 |
+
st.sidebar.markdown(
|
14 |
+
"Developed by [Tajeddine Bourhim ](https://tajeddine-portfolio.netlify.app/)."
|
15 |
+
)
|
16 |
+
st.sidebar.markdown(
|
17 |
+
"[![GitHub](https://img.shields.io/badge/GitHub-Profile-blue?logo=github)](https://github.com/scorpionTaj)"
|
18 |
+
)
|
19 |
+
st.sidebar.markdown(
|
20 |
+
"[![LinkedIn](https://img.shields.io/badge/LinkedIn-Profile-blue?logo=linkedin)](https://www.linkedin.com/in/tajeddine-bourhim/)"
|
21 |
+
)
|
22 |
+
|
23 |
+
st.sidebar.subheader("π About This App")
|
24 |
+
st.sidebar.markdown(
|
25 |
+
"This is a stock price prediction app that uses a Long Short-Term Memory (LSTM) neural network to predict the closing price of a stock. The app uses the Yahoo Finance API to fetch the stock data."
|
26 |
+
)
|
27 |
+
|
28 |
+
stock = st.text_input("Enter the stock symbol (e.g. AAPL):")
|
29 |
+
|
30 |
+
if stock:
|
31 |
+
end = st.date_input("End Date", datetime.now())
|
32 |
+
start = st.date_input("Start Date", datetime(end.year - 20, end.month, end.day))
|
33 |
+
|
34 |
+
apple_data = yf.download(stock, start, end)
|
35 |
+
|
36 |
+
if not apple_data.empty:
|
37 |
+
model = load_model("Latest_stock_price_prediction.keras")
|
38 |
+
st.subheader("π Stock Data")
|
39 |
+
st.write(apple_data)
|
40 |
+
else:
|
41 |
+
st.error("No data found for the given stock symbol.")
|
42 |
+
else:
|
43 |
+
st.warning("Please enter a stock symbol.")
|
44 |
+
|
45 |
+
split_len = int(len(apple_data) * 0.8)
|
46 |
+
x_test = pd.DataFrame(apple_data.Close[split_len:])
|
47 |
+
|
48 |
+
|
49 |
+
def graph_plotting(figsize, values, data, extra_data=0, extra_dataset=None):
|
50 |
+
"""
|
51 |
+
Function to plot graphs with given parameters.
|
52 |
+
"""
|
53 |
+
fig = plt.figure(figsize=figsize)
|
54 |
+
plt.plot(values, "Red")
|
55 |
+
plt.plot(data.Close, "b")
|
56 |
+
if extra_data:
|
57 |
+
plt.plot(extra_dataset)
|
58 |
+
return fig
|
59 |
+
|
60 |
+
|
61 |
+
try:
|
62 |
+
# Display the original close price and moving average for 250 days
|
63 |
+
st.subheader("π Original Close Price and 250-Day Moving Average")
|
64 |
+
apple_data["MA_for_250_days"] = apple_data.Close.rolling(250).mean()
|
65 |
+
st.pyplot(graph_plotting((15, 6), apple_data["MA_for_250_days"], apple_data, 0))
|
66 |
+
|
67 |
+
# Display the original close price and moving average for 200 days
|
68 |
+
st.subheader("π Original Close Price and 200-Day Moving Average")
|
69 |
+
apple_data["MA_for_200_days"] = apple_data.Close.rolling(200).mean()
|
70 |
+
st.pyplot(graph_plotting((15, 6), apple_data["MA_for_200_days"], apple_data, 0))
|
71 |
+
|
72 |
+
# Display the original close price and moving average for 100 days
|
73 |
+
st.subheader("π Original Close Price and 100-Day Moving Average")
|
74 |
+
apple_data["MA_for_100_days"] = apple_data.Close.rolling(100).mean()
|
75 |
+
st.pyplot(graph_plotting((15, 6), apple_data["MA_for_100_days"], apple_data, 0))
|
76 |
+
|
77 |
+
# Display the original close price and moving average for 100 days and 250 days
|
78 |
+
st.subheader("π Original Close Price and 100-Day and 250-Day Moving Average")
|
79 |
+
st.pyplot(
|
80 |
+
graph_plotting(
|
81 |
+
(15, 6),
|
82 |
+
apple_data["MA_for_100_days"],
|
83 |
+
apple_data,
|
84 |
+
1,
|
85 |
+
apple_data["MA_for_250_days"],
|
86 |
+
)
|
87 |
+
)
|
88 |
+
|
89 |
+
scaler = MinMaxScaler(feature_range=(0, 1))
|
90 |
+
scaled_data = scaler.fit_transform(x_test[["Close"]])
|
91 |
+
|
92 |
+
x_data = []
|
93 |
+
y_data = []
|
94 |
+
|
95 |
+
for i in range(100, len(scaled_data)):
|
96 |
+
x_data.append(scaled_data[i - 100 : i])
|
97 |
+
y_data.append(scaled_data[i])
|
98 |
+
|
99 |
+
x_data, y_data = np.array(x_data), np.array(y_data)
|
100 |
+
|
101 |
+
predictions = model.predict(x_data)
|
102 |
+
|
103 |
+
# Inverse transform the predictions and test data to original scale
|
104 |
+
inv_pre = scaler.inverse_transform(predictions)
|
105 |
+
inv_y_test = scaler.inverse_transform(y_data)
|
106 |
+
|
107 |
+
# Create a DataFrame to hold the original test data and predictions
|
108 |
+
ploting_data = pd.DataFrame(
|
109 |
+
{
|
110 |
+
"original_test_data": inv_y_test.reshape(-1),
|
111 |
+
"predictions": inv_pre.reshape(-1),
|
112 |
+
},
|
113 |
+
index=apple_data.index[split_len + 100 :],
|
114 |
+
)
|
115 |
+
|
116 |
+
# Display the original values vs predicted values
|
117 |
+
st.subheader("π Original values vs Predicted values")
|
118 |
+
st.write(ploting_data)
|
119 |
+
|
120 |
+
# Plot the original close price vs predicted close price
|
121 |
+
st.subheader("π Original Close Price vs Predicted Close price")
|
122 |
+
fig = plt.figure(figsize=(15, 6))
|
123 |
+
plt.plot(pd.concat([apple_data.Close[: split_len + 100], ploting_data], axis=0))
|
124 |
+
plt.legend(["Data- not used", "Original Test data", "Predicted Test data"])
|
125 |
+
st.pyplot(fig)
|
126 |
+
|
127 |
+
# Calculate and display performance metrics
|
128 |
+
mse = mean_squared_error(inv_y_test, inv_pre)
|
129 |
+
r2 = r2_score(inv_y_test, inv_pre)
|
130 |
+
st.subheader("π Performance Metrics")
|
131 |
+
st.write(f"Mean Squared Error: {mse}")
|
132 |
+
st.write(f"R-squared: {r2}")
|
133 |
+
|
134 |
+
except Exception as e:
|
135 |
+
st.error(f"An error occurred: {e}")
|
stock_price.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|