mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-23 15:58:08 +00:00
feat(Ensembling): added possiblity of stacking models
This commit is contained in:
+36
-19
@@ -1,3 +1,4 @@
|
|||||||
|
from sklearn.model_selection import TimeSeriesSplit
|
||||||
from .types import Config, RawConfig
|
from .types import Config, RawConfig
|
||||||
from utils.helpers import flatten
|
from utils.helpers import flatten
|
||||||
from feature_extractors.feature_extractor_presets import (
|
from feature_extractors.feature_extractor_presets import (
|
||||||
@@ -8,9 +9,11 @@ from data_loader.collections import data_collections
|
|||||||
from labeling.eventfilters_map import eventfilters_map
|
from labeling.eventfilters_map import eventfilters_map
|
||||||
from labeling.labellers_map import labellers_map
|
from labeling.labellers_map import labellers_map
|
||||||
from models.sklearn import SKLearnModel
|
from models.sklearn import SKLearnModel
|
||||||
from sklearn.ensemble import VotingClassifier
|
from sklearn.ensemble import VotingClassifier, StackingClassifier
|
||||||
from transformations.retrieve import get_pca, get_rfe, get_scaler
|
from transformations.retrieve import get_pca, get_rfe, get_scaler
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
from models.base import Model
|
||||||
|
from typing import Literal
|
||||||
|
|
||||||
|
|
||||||
def preprocess_config(raw_config: RawConfig) -> Config:
|
def preprocess_config(raw_config: RawConfig) -> Config:
|
||||||
@@ -40,11 +43,25 @@ def __preprocess_feature_extractors_config(data_dict: dict) -> dict:
|
|||||||
|
|
||||||
|
|
||||||
def __preprocess_model_config(model_config: dict) -> dict:
|
def __preprocess_model_config(model_config: dict) -> dict:
|
||||||
def map_ensembling_method(method: str) -> str:
|
def get_ensemble_model(
|
||||||
|
estimators: list[Model], method: Literal["voting_soft", "stacking"]
|
||||||
|
) -> Model:
|
||||||
|
|
||||||
if method == "voting_soft":
|
if method == "voting_soft":
|
||||||
return "soft"
|
return SKLearnModel(
|
||||||
elif method == "voing_hard":
|
VotingClassifier(
|
||||||
return "hard"
|
[(m.name, m) for m in directional_models],
|
||||||
|
voting="soft",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
elif method == "stacking":
|
||||||
|
return SKLearnModel(
|
||||||
|
StackingClassifier(
|
||||||
|
[(m.name, m) for m in estimators],
|
||||||
|
final_estimator=estimators[0],
|
||||||
|
cv=TimeSeriesSplit(gap=100),
|
||||||
|
)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
raise Exception(f"Unknown ensembling method: {method}")
|
raise Exception(f"Unknown ensembling method: {method}")
|
||||||
|
|
||||||
@@ -52,22 +69,22 @@ def __preprocess_model_config(model_config: dict) -> dict:
|
|||||||
get_model(model_name) for model_name in model_config["directional_models"]
|
get_model(model_name) for model_name in model_config["directional_models"]
|
||||||
]
|
]
|
||||||
model_config.pop("directional_models")
|
model_config.pop("directional_models")
|
||||||
model_config["directional_model"] = SKLearnModel(
|
|
||||||
VotingClassifier(
|
if len(directional_models) > 1:
|
||||||
[(m.name, m) for m in directional_models],
|
model_config["directional_model"] = get_ensemble_model(
|
||||||
voting=map_ensembling_method(model_config["ensembling_method"]),
|
directional_models, method=model_config["ensembling_method"]
|
||||||
)
|
)
|
||||||
)
|
else:
|
||||||
if len(model_config["meta_models"]) > 0:
|
model_config["directional_model"] = directional_models[0]
|
||||||
meta_models = [
|
|
||||||
get_model(model_name) for model_name in model_config["meta_models"]
|
meta_models = [get_model(model_name) for model_name in model_config["meta_models"]]
|
||||||
]
|
if len(model_config["meta_models"]) > 1:
|
||||||
model_config["meta_model"] = SKLearnModel(
|
model_config["meta_model"] = get_ensemble_model(
|
||||||
VotingClassifier(
|
meta_models, method=model_config["ensembling_method"]
|
||||||
[(m.name, m) for m in meta_models],
|
|
||||||
voting=map_ensembling_method(model_config["ensembling_method"]),
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
else:
|
||||||
|
model_config["meta_model"] = meta_models[0]
|
||||||
|
|
||||||
model_config.pop("meta_models")
|
model_config.pop("meta_models")
|
||||||
model_config.pop("ensembling_method")
|
model_config.pop("ensembling_method")
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -32,6 +32,6 @@ def get_default_config() -> RawConfig:
|
|||||||
event_filter="cusum_fixed",
|
event_filter="cusum_fixed",
|
||||||
labeling="two_class",
|
labeling="two_class",
|
||||||
forecasting_horizon=50,
|
forecasting_horizon=50,
|
||||||
save_models=False,
|
save_models=True,
|
||||||
ensembling_method="voting_soft",
|
ensembling_method="voting_soft",
|
||||||
)
|
)
|
||||||
|
|||||||
+1
-1
@@ -29,7 +29,7 @@ class RawConfig(BaseModel):
|
|||||||
labeling: Literal["two_class", "three_class_balanced", "three_class_imbalanced"]
|
labeling: Literal["two_class", "three_class_balanced", "three_class_imbalanced"]
|
||||||
forecasting_horizon: int
|
forecasting_horizon: int
|
||||||
save_models: bool
|
save_models: bool
|
||||||
ensembling_method: Literal["voting_soft", "voting_hard"]
|
ensembling_method: Literal["voting_soft", "stacking"]
|
||||||
|
|
||||||
directional_models: list[str]
|
directional_models: list[str]
|
||||||
meta_models: list[str]
|
meta_models: list[str]
|
||||||
|
|||||||
@@ -75,6 +75,35 @@ def get_model(model_name: str) -> Model:
|
|||||||
return set_name(
|
return set_name(
|
||||||
SKLearnModel(LGBMClassifier(n_jobs=-1, max_depth=20, random_state=1))
|
SKLearnModel(LGBMClassifier(n_jobs=-1, max_depth=20, random_state=1))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
elif model_name == "AutoML":
|
||||||
|
from supervised.automl import AutoML
|
||||||
|
|
||||||
|
return set_name(
|
||||||
|
SKLearnModel(
|
||||||
|
AutoML(
|
||||||
|
total_time_limit=60,
|
||||||
|
mode="Compete",
|
||||||
|
algorithms=[
|
||||||
|
"Baseline",
|
||||||
|
"Linear",
|
||||||
|
"Random Forest",
|
||||||
|
"Extra Trees",
|
||||||
|
"LightGBM",
|
||||||
|
"CatBoost",
|
||||||
|
"Neural Network",
|
||||||
|
"Nearest Neighbors",
|
||||||
|
],
|
||||||
|
validation_strategy={
|
||||||
|
"validation_type": "split",
|
||||||
|
"train_ratio": 0.75,
|
||||||
|
"shuffle": False,
|
||||||
|
"stratify": True
|
||||||
|
},
|
||||||
|
eval_metric="f1",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
elif model_name == "StaticMom":
|
elif model_name == "StaticMom":
|
||||||
from models.momentum import StaticMomentumModel
|
from models.momentum import StaticMomentumModel
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user