Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from sklearn.linear_model import LinearRegression
|
|
5 |
from sklearn.ensemble import RandomForestRegressor
|
6 |
from sklearn.preprocessing import StandardScaler
|
7 |
from sklearn.model_selection import train_test_split
|
|
|
8 |
from datetime import datetime, timedelta
|
9 |
import numpy as np
|
10 |
|
@@ -45,6 +46,10 @@ stock_data.dropna(inplace=True)
|
|
45 |
X = stock_data[['Open', 'High', 'Low', 'Volume', 'MA_10', 'MA_50', 'RSI', 'Return']]
|
46 |
y = stock_data['Close']
|
47 |
|
|
|
|
|
|
|
|
|
48 |
# Split the data
|
49 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
50 |
|
@@ -63,10 +68,21 @@ rf_model.fit(X_train_scaled, y_train)
|
|
63 |
|
64 |
# Predict future prices using ensemble method
|
65 |
future_dates = [stock_data['Date'].iloc[-1] + timedelta(days=x) for x in range(1, 15)]
|
66 |
-
future_df = pd.DataFrame(index=future_dates, columns=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
future_df = future_df.fillna(method='ffill')
|
68 |
|
69 |
-
|
|
|
|
|
|
|
70 |
lr_predictions = lr_model.predict(future_X_scaled)
|
71 |
rf_predictions = rf_model.predict(future_X_scaled)
|
72 |
|
|
|
5 |
from sklearn.ensemble import RandomForestRegressor
|
6 |
from sklearn.preprocessing import StandardScaler
|
7 |
from sklearn.model_selection import train_test_split
|
8 |
+
from sklearn.impute import SimpleImputer
|
9 |
from datetime import datetime, timedelta
|
10 |
import numpy as np
|
11 |
|
|
|
46 |
X = stock_data[['Open', 'High', 'Low', 'Volume', 'MA_10', 'MA_50', 'RSI', 'Return']]
|
47 |
y = stock_data['Close']
|
48 |
|
49 |
+
# Handle missing values
|
50 |
+
imputer = SimpleImputer(strategy='mean')
|
51 |
+
X = imputer.fit_transform(X)
|
52 |
+
|
53 |
# Split the data
|
54 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
55 |
|
|
|
68 |
|
69 |
# Predict future prices using ensemble method
|
70 |
future_dates = [stock_data['Date'].iloc[-1] + timedelta(days=x) for x in range(1, 15)]
|
71 |
+
future_df = pd.DataFrame(index=future_dates, columns=stock_data.columns)
|
72 |
+
future_df['Open'] = stock_data['Open'].iloc[-1]
|
73 |
+
future_df['High'] = stock_data['High'].iloc[-1]
|
74 |
+
future_df['Low'] = stock_data['Low'].iloc[-1]
|
75 |
+
future_df['Volume'] = stock_data['Volume'].iloc[-1]
|
76 |
+
future_df['MA_10'] = stock_data['MA_10'].iloc[-1]
|
77 |
+
future_df['MA_50'] = stock_data['MA_50'].iloc[-1]
|
78 |
+
future_df['RSI'] = stock_data['RSI'].iloc[-1]
|
79 |
+
future_df['Return'] = stock_data['Return'].iloc[-1]
|
80 |
future_df = future_df.fillna(method='ffill')
|
81 |
|
82 |
+
# Handle missing values in future data
|
83 |
+
future_X = imputer.transform(future_df[['Open', 'High', 'Low', 'Volume', 'MA_10', 'MA_50', 'RSI', 'Return']])
|
84 |
+
future_X_scaled = scaler.transform(future_X)
|
85 |
+
|
86 |
lr_predictions = lr_model.predict(future_X_scaled)
|
87 |
rf_predictions = rf_model.predict(future_X_scaled)
|
88 |
|