PankhuriSharma9795 commited on
Commit
b0f549d
ยท
verified ยท
1 Parent(s): dde4133

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[2]:
5
+
6
+
7
+ import streamlit as st
8
+ import pickle
9
+ import pandas as pd
10
+ import numpy as np
11
+ from datetime import timedelta
12
+ import yfinance as yf
13
+ import warnings
14
+ warnings.filterwarnings('ignore')
15
+
16
+ # Load your trained Linear Regression model
17
+ with open('mod_AAPL.pkl', 'rb') as file:
18
+ model = pickle.load(file)
19
+
20
+ st.title("๐Ÿ“ˆ Apple Stock Forecast (Next 30 Days)")
21
+ st.write("Uses a Linear Regression model with `Date_Ordinal` and `Prev_Close` as features.")
22
+
23
+ # --- ๐Ÿ“… Select date range to fetch data
24
+ st.subheader("๐Ÿ“… Select date range to fetch Apple stock data")
25
+ start_date = st.date_input("Start Date", value=pd.Timestamp("2020-01-01"))
26
+ end_date = st.date_input("End Date", value=pd.Timestamp.now())
27
+
28
+ # --- Fetch data from Yahoo Finance
29
+ st.write("๐Ÿ”„ Fetching AAPL stock data...")
30
+ apple_data = yf.download("AAPL", start=start_date, end=end_date)
31
+
32
+ # --- Handle empty data case
33
+ if apple_data.empty:
34
+ st.error("โš ๏ธ No data found for the selected date range. Please try another range.")
35
+ else:
36
+ st.success("โœ… Data fetched successfully.")
37
+ st.write("Showing latest data:")
38
+ st.dataframe(apple_data.tail())
39
+
40
+ # --- Preprocessing
41
+ apple_data.index = pd.to_datetime(apple_data.index)
42
+ apple_data['Date_Ordinal'] = apple_data.index.map(lambda x: x.toordinal())
43
+ apple_data['Prev_Close'] = apple_data['Close'].shift(1)
44
+ apple_data.dropna(inplace=True)
45
+
46
+ # Predict next 30 days from last date
47
+ last_known_close = apple_data['Close'].iloc[-1]
48
+ last_date = apple_data.index.max()
49
+ future_dates = pd.date_range(start=last_date + timedelta(days=1), periods=30)
50
+ future_preds = []
51
+
52
+ prev_close = last_known_close
53
+ for date in future_dates:
54
+ date_ordinal = date.toordinal()
55
+ features = pd.DataFrame({'Date_Ordinal': [date_ordinal], 'Prev_Close': [prev_close]})
56
+ pred = model.predict(features)[0]
57
+ future_preds.append(pred)
58
+ prev_close = pred # Roll forward
59
+
60
+ forecast_df = pd.DataFrame({'Date': future_dates, 'Predicted_Close': future_preds})
61
+
62
+ st.subheader("๐Ÿ”ฎ Next 30-Day Forecast")
63
+ st.dataframe(forecast_df)
64
+
65
+ # --- Plot
66
+ st.line_chart(forecast_df.set_index('Date')['Predicted_Close'])
67
+
68
+
69
+ # In[ ]:
70
+
71
+
72
+
73
+