dibend commited on
Commit
aadb13b
1 Parent(s): da6b632

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -2
app.py CHANGED
@@ -20,13 +20,26 @@ def train_predict_wrapper(ticker, start_date, end_date, prediction_days):
20
  # Download stock data
21
  data = yf.download(ticker, start=start_date, end=end_date)
22
  data = data["Close"]
23
-
 
 
 
 
24
  # Train linear regression model
25
- X = data.index.values[:-prediction_days].reshape(-1, 1)
26
  y = data.values[:-prediction_days]
27
  model = LinearRegression()
28
  model.fit(X, y)
29
 
 
 
 
 
 
 
 
 
 
30
  # Predict future prices
31
  future_dates = data.index.values[-prediction_days:]
32
  X_future = future_dates.reshape(-1, 1)
 
20
  # Download stock data
21
  data = yf.download(ticker, start=start_date, end=end_date)
22
  data = data["Close"]
23
+
24
+ # Convert dates to a numerical format (days since start date)
25
+ start_date = pd.to_datetime(start_date)
26
+ days_since_start = (data.index - start_date).days
27
+
28
  # Train linear regression model
29
+ X = days_since_start.values[:-prediction_days].reshape(-1, 1)
30
  y = data.values[:-prediction_days]
31
  model = LinearRegression()
32
  model.fit(X, y)
33
 
34
+ # Prepare data for prediction
35
+ last_date = data.index[-1]
36
+ future_dates = pd.date_range(start=last_date, periods=prediction_days + 1, closed='right')
37
+ future_days_since_start = (future_dates - start_date).days
38
+ X_future = future_days_since_start.values.reshape(-1, 1)
39
+
40
+ # Predict future prices
41
+ predicted_prices = model.predict(X_future)
42
+
43
  # Predict future prices
44
  future_dates = data.index.values[-prediction_days:]
45
  X_future = future_dates.reshape(-1, 1)