Spaces:
Sleeping
Sleeping
Add Jared read-only token OHLCV ingestion and manual contract lookup
Browse files- core/data.py +11 -8
core/data.py
CHANGED
|
@@ -509,11 +509,14 @@ def preprocess_data(df, features, target, window_size, horizon):
|
|
| 509 |
raise ValueError("DataFrame index must be a DatetimeIndex for preprocessing.")
|
| 510 |
|
| 511 |
# Filter features to only include those present in the DataFrame columns
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
|
|
|
|
|
|
|
|
|
| 517 |
data = df[full_features].copy()
|
| 518 |
data.dropna(inplace=True)
|
| 519 |
|
|
@@ -523,11 +526,11 @@ def preprocess_data(df, features, target, window_size, horizon):
|
|
| 523 |
feature_scaler = MinMaxScaler()
|
| 524 |
target_scaler = MinMaxScaler()
|
| 525 |
|
| 526 |
-
data_features_scaled = feature_scaler.fit_transform(data[
|
| 527 |
data_target_scaled = target_scaler.fit_transform(data[[target]])
|
| 528 |
|
| 529 |
full_scaled = np.hstack((data_features_scaled, data_target_scaled))
|
| 530 |
-
target_idx = len(
|
| 531 |
|
| 532 |
X, y = [], []
|
| 533 |
for i in range(len(full_scaled) - window_size - horizon + 1):
|
|
@@ -541,7 +544,7 @@ def preprocess_data(df, features, target, window_size, horizon):
|
|
| 541 |
raise ValueError(f"Insufficient data after preprocessing. Data length: {len(full_scaled)}, window_size: {window_size}, horizon: {horizon}")
|
| 542 |
|
| 543 |
print_log(f"Preprocessed data: X.shape={X.shape}, y.shape={y.shape}, Final features: {full_features}, Target idx: {target_idx}")
|
| 544 |
-
return X, y, feature_scaler, target_scaler, full_features, target_idx, None,
|
| 545 |
except Exception as e:
|
| 546 |
print_log(f"Preprocessing error: {str(e)}", 'ERROR')
|
| 547 |
raise ValueError(f"Preprocessing failed: {str(e)}")
|
|
|
|
| 509 |
raise ValueError("DataFrame index must be a DatetimeIndex for preprocessing.")
|
| 510 |
|
| 511 |
# Filter features to only include those present in the DataFrame columns
|
| 512 |
+
# Never include the target twice. The fallback indicator path may
|
| 513 |
+
# return ['value']; duplicating it here makes target_scaler fit two
|
| 514 |
+
# columns and breaks multi-step inverse scaling.
|
| 515 |
+
updated_feature_cols = list(dict.fromkeys(
|
| 516 |
+
f for f in features if f in df.columns and f != target
|
| 517 |
+
))
|
| 518 |
+
model_feature_cols = updated_feature_cols or [target]
|
| 519 |
+
full_features = list(dict.fromkeys(model_feature_cols + [target]))
|
| 520 |
data = df[full_features].copy()
|
| 521 |
data.dropna(inplace=True)
|
| 522 |
|
|
|
|
| 526 |
feature_scaler = MinMaxScaler()
|
| 527 |
target_scaler = MinMaxScaler()
|
| 528 |
|
| 529 |
+
data_features_scaled = feature_scaler.fit_transform(data[model_feature_cols])
|
| 530 |
data_target_scaled = target_scaler.fit_transform(data[[target]])
|
| 531 |
|
| 532 |
full_scaled = np.hstack((data_features_scaled, data_target_scaled))
|
| 533 |
+
target_idx = len(model_feature_cols)
|
| 534 |
|
| 535 |
X, y = [], []
|
| 536 |
for i in range(len(full_scaled) - window_size - horizon + 1):
|
|
|
|
| 544 |
raise ValueError(f"Insufficient data after preprocessing. Data length: {len(full_scaled)}, window_size: {window_size}, horizon: {horizon}")
|
| 545 |
|
| 546 |
print_log(f"Preprocessed data: X.shape={X.shape}, y.shape={y.shape}, Final features: {full_features}, Target idx: {target_idx}")
|
| 547 |
+
return X, y, feature_scaler, target_scaler, full_features, target_idx, None, model_feature_cols
|
| 548 |
except Exception as e:
|
| 549 |
print_log(f"Preprocessing error: {str(e)}", 'ERROR')
|
| 550 |
raise ValueError(f"Preprocessing failed: {str(e)}")
|