Spaces:
Sleeping
Sleeping
Update core/data.py
Browse files- core/data.py +28 -12
core/data.py
CHANGED
|
@@ -1,25 +1,41 @@
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
try:
|
| 4 |
import yfinance as yf
|
| 5 |
-
except:
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# core/data.py
|
| 9 |
-
import yfinance as yf
|
| 10 |
-
import pandas as pd
|
| 11 |
|
| 12 |
-
def load_data(ticker, start, end):
|
| 13 |
df = yf.download(ticker, start=start, end=end)
|
| 14 |
|
| 15 |
if df.empty:
|
| 16 |
raise ValueError(f"No data found for ticker '{ticker}'. Please check the symbol or try another.")
|
| 17 |
|
| 18 |
-
# Select only the 'Close' column and drop missing values
|
| 19 |
df = df[['Close']].dropna()
|
| 20 |
df = df.rename(columns={'Close': 'value'})
|
| 21 |
-
|
| 22 |
-
# Ensure index is datetime and reset if necessary
|
| 23 |
df.reset_index(inplace=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
return
|
|
|
|
| 1 |
+
# core/data.py
|
| 2 |
+
|
| 3 |
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import torch
|
| 6 |
+
from sklearn.preprocessing import StandardScaler
|
| 7 |
+
|
| 8 |
try:
|
| 9 |
import yfinance as yf
|
| 10 |
+
except ImportError:
|
| 11 |
+
raise ImportError("yfinance must be installed to fetch financial data.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def load_data(ticker="AAPL", start="2020-01-01", end="2023-01-01"):
|
| 14 |
df = yf.download(ticker, start=start, end=end)
|
| 15 |
|
| 16 |
if df.empty:
|
| 17 |
raise ValueError(f"No data found for ticker '{ticker}'. Please check the symbol or try another.")
|
| 18 |
|
|
|
|
| 19 |
df = df[['Close']].dropna()
|
| 20 |
df = df.rename(columns={'Close': 'value'})
|
|
|
|
|
|
|
| 21 |
df.reset_index(inplace=True)
|
| 22 |
+
return df
|
| 23 |
+
|
| 24 |
+
def preprocess_data(df, window_size=30):
|
| 25 |
+
values = df['value'].values.reshape(-1, 1)
|
| 26 |
+
|
| 27 |
+
scaler = StandardScaler()
|
| 28 |
+
scaled = scaler.fit_transform(values)
|
| 29 |
+
|
| 30 |
+
X, y = [], []
|
| 31 |
+
for i in range(len(scaled) - window_size):
|
| 32 |
+
X.append(scaled[i:i + window_size])
|
| 33 |
+
y.append(scaled[i + window_size])
|
| 34 |
+
|
| 35 |
+
X = np.array(X)
|
| 36 |
+
y = np.array(y)
|
| 37 |
+
|
| 38 |
+
X = torch.tensor(X, dtype=torch.float32)
|
| 39 |
+
y = torch.tensor(y, dtype=torch.float32)
|
| 40 |
|
| 41 |
+
return X, y
|