Geek7 commited on
Commit
6e74a3e
·
verified ·
1 Parent(s): 16e2221

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -27
app.py CHANGED
@@ -14,12 +14,12 @@ def fetch_stock_data(symbol, start_date, end_date):
14
 
15
  # Function to create features for the model
16
  def create_features(data):
17
- data.index = pd.to_datetime(data.index) # Convert index to datetime
18
- data['Year'] = data.index.year
19
- data['Month'] = data.index.month
20
- data['Day'] = data.index.day
21
- data['Hour'] = data.index.hour
22
- data['Minute'] = data.index.minute
23
 
24
  return data
25
 
@@ -28,20 +28,37 @@ def train_model(data):
28
  features = ['Year', 'Month', 'Day', 'Hour', 'Minute']
29
  target = 'Close'
30
 
 
 
 
 
31
  X = data[features]
32
  y = data[target]
33
 
34
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- model = RandomForestRegressor()
37
- model.fit(X_train, y_train)
 
 
38
 
39
- # Evaluate the model
40
- predictions = model.predict(X_test)
41
- mse = mean_squared_error(y_test, predictions)
42
- st.write(f"Mean Squared Error: {mse}")
43
 
44
- return model
 
 
45
 
46
  # Streamlit UI
47
  def main():
@@ -61,19 +78,20 @@ def main():
61
  # Train the model
62
  model = train_model(stock_data)
63
 
64
- # Predict the stock price for a specific date (e.g., the last date in the dataset)
65
- prediction_date = stock_data.index[-1]
66
- prediction_features = [[
67
- prediction_date.year,
68
- prediction_date.month,
69
- prediction_date.day,
70
- prediction_date.hour,
71
- prediction_date.minute
72
- ]]
73
- predicted_price = model.predict(prediction_features)[0]
74
-
75
- st.subheader(f"Predicted Stock Price on {prediction_date} (UTC):")
76
- st.write(f"${predicted_price:.2f}")
 
77
 
78
  # Run the Streamlit app
79
  if __name__ == '__main__':
 
14
 
15
  # Function to create features for the model
16
  def create_features(data):
17
+ data['Date'] = data.index
18
+ data['Year'] = data['Date'].dt.year
19
+ data['Month'] = data['Date'].dt.month
20
+ data['Day'] = data['Date'].dt.day
21
+ data['Hour'] = data['Date'].dt.hour
22
+ data['Minute'] = data['Date'].dt.minute
23
 
24
  return data
25
 
 
28
  features = ['Year', 'Month', 'Day', 'Hour', 'Minute']
29
  target = 'Close'
30
 
31
+ if len(data) == 0:
32
+ st.write("Not enough data for training.")
33
+ return None
34
+
35
  X = data[features]
36
  y = data[target]
37
 
38
+ if len(data) <= 1:
39
+ st.write("Not enough data for splitting.")
40
+ return None
41
+
42
+ try:
43
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
44
+
45
+ if len(X_train) == 0 or len(X_test) == 0:
46
+ st.write("Not enough data after splitting.")
47
+ return None
48
+
49
+ model = RandomForestRegressor()
50
+ model.fit(X_train, y_train)
51
 
52
+ # Evaluate the model
53
+ predictions = model.predict(X_test)
54
+ mse = mean_squared_error(y_test, predictions)
55
+ st.write(f"Mean Squared Error: {mse}")
56
 
57
+ return model
 
 
 
58
 
59
+ except ValueError as e:
60
+ st.write(f"Error during train-test split: {e}")
61
+ return None
62
 
63
  # Streamlit UI
64
  def main():
 
78
  # Train the model
79
  model = train_model(stock_data)
80
 
81
+ if model:
82
+ # Predict the stock price for a specific date (e.g., the last date in the dataset)
83
+ prediction_date = stock_data['Date'].iloc[-1]
84
+ prediction_features = [[
85
+ prediction_date.year,
86
+ prediction_date.month,
87
+ prediction_date.day,
88
+ prediction_date.hour,
89
+ prediction_date.minute
90
+ ]]
91
+ predicted_price = model.predict(prediction_features)[0]
92
+
93
+ st.subheader(f"Predicted Stock Price on {prediction_date} (UTC):")
94
+ st.write(f"${predicted_price:.2f}")
95
 
96
  # Run the Streamlit app
97
  if __name__ == '__main__':