fix: train valid purged ML artifacts
This commit is contained in:
+7083
-111
File diff suppressed because it is too large
Load Diff
+10
-2
@@ -156,6 +156,14 @@ def purged_time_series_splits(rows, n_splits=VALIDATION_SPLITS,
|
|||||||
yield np.array(purged_train, dtype=int), np.array(val_idx, dtype=int)
|
yield np.array(purged_train, dtype=int), np.array(val_idx, dtype=int)
|
||||||
|
|
||||||
|
|
||||||
|
def viable_classification_splits(y, splits):
|
||||||
|
"""Yield only folds whose training window contains both target classes."""
|
||||||
|
for train_idx, val_idx in splits:
|
||||||
|
if len(np.unique(y[train_idx])) < 2:
|
||||||
|
continue
|
||||||
|
yield train_idx, val_idx
|
||||||
|
|
||||||
|
|
||||||
def _build_model():
|
def _build_model():
|
||||||
return GradientBoostingClassifier(
|
return GradientBoostingClassifier(
|
||||||
n_estimators=300,
|
n_estimators=300,
|
||||||
@@ -377,12 +385,12 @@ def train_model(rows):
|
|||||||
cv_recall = []
|
cv_recall = []
|
||||||
fold_results = []
|
fold_results = []
|
||||||
|
|
||||||
splits = list(purged_time_series_splits(
|
splits = list(viable_classification_splits(y, purged_time_series_splits(
|
||||||
labeled,
|
labeled,
|
||||||
n_splits=VALIDATION_SPLITS,
|
n_splits=VALIDATION_SPLITS,
|
||||||
label_horizon_days=LABEL_HORIZON_DAYS,
|
label_horizon_days=LABEL_HORIZON_DAYS,
|
||||||
embargo_days=0,
|
embargo_days=0,
|
||||||
))
|
)))
|
||||||
if not splits:
|
if not splits:
|
||||||
log.error("No viable purged validation splits. Need more history for %dd label horizon.",
|
log.error("No viable purged validation splits. Need more history for %dd label horizon.",
|
||||||
LABEL_HORIZON_DAYS)
|
LABEL_HORIZON_DAYS)
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
from ml import optimizer
|
from ml import optimizer
|
||||||
|
|
||||||
|
|
||||||
@@ -72,3 +74,16 @@ def test_run_out_of_sample_comparison_scores_only_validation_rows_with_fold_weig
|
|||||||
|
|
||||||
caution_equal = next(bucket for bucket in comparison["equal_weight"] if bucket["label"] == "CAUTION — OVERHEATED")
|
caution_equal = next(bucket for bucket in comparison["equal_weight"] if bucket["label"] == "CAUTION — OVERHEATED")
|
||||||
assert caution_equal["days"] == 2
|
assert caution_equal["days"] == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_classification_splits_skip_training_windows_with_one_class():
|
||||||
|
y = np.array([1, 1, 1, 0, 1, 0])
|
||||||
|
splits = [
|
||||||
|
(np.array([0, 1]), np.array([2, 3])),
|
||||||
|
(np.array([0, 1, 3, 4]), np.array([5])),
|
||||||
|
]
|
||||||
|
|
||||||
|
viable = list(optimizer.viable_classification_splits(y, splits))
|
||||||
|
|
||||||
|
assert len(viable) == 1
|
||||||
|
assert viable[0][0].tolist() == [0, 1, 3, 4]
|
||||||
|
|||||||
Reference in New Issue
Block a user