mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-09 17:11:05 +00:00
6b26643ece
* feat(Transformations): removed feature-selection pre-processing step completely * fix(Core): removed unnecessary `original_X` * fix(Transformations): use the X_expanding_window to transform subsequent data * fix(RFE): should check for model correctly * fix(Config): only re-train the model every 40 timestamp * fix(MetaLabeling): pass in the correct X to meta-labeling step * fix(Transformation): PCA should at least keep as many features as sliding_window_size * feat(Transformations): cache transformations across the same asset * fix(Tests): missing preloaded_transformations arg * chore(Config): got rid of unnecessary 'classification_models' and 'regression_models' dictionary keys
69 lines
2.9 KiB
Python
69 lines
2.9 KiB
Python
from utils.evaluate import discretize_threeway_threshold, evaluate_predictions
|
|
from utils.helpers import equal_except_nan
|
|
from training.primary_model import train_primary_model
|
|
import pandas as pd
|
|
from models.model_map import default_feature_selector_classification, default_feature_selector_regression
|
|
from models.base import Model
|
|
from reporting.types import Reporting
|
|
from typing import Union
|
|
|
|
|
|
def train_meta_labeling_model(
|
|
target_asset: str,
|
|
X: pd.DataFrame,
|
|
input_predictions: pd.Series,
|
|
y: pd.Series,
|
|
target_returns: pd.Series,
|
|
models: list[tuple[str, Model]],
|
|
data_config: dict,
|
|
model_config: dict,
|
|
training_config: dict,
|
|
model_suffix: str,
|
|
preloaded_models: Union[list[Reporting.Single_Model], None] = None
|
|
) -> tuple[pd.Series, pd.Series, pd.DataFrame, list[Reporting.Single_Model]]:
|
|
|
|
discretize = discretize_threeway_threshold(0.33)
|
|
discretized_predictions = input_predictions.apply(discretize)
|
|
meta_y: pd.Series = pd.concat([discretized_predictions, y], axis=1).apply(equal_except_nan, axis = 1)
|
|
|
|
meta_X = pd.concat([X, input_predictions, discretized_predictions], axis = 1)
|
|
|
|
_, meta_preds, meta_probabilities, all_models_single_asset = train_primary_model(
|
|
ticker_to_predict = "prediction_correct",
|
|
X = meta_X,
|
|
y = meta_y,
|
|
target_returns = target_returns,
|
|
models = models,
|
|
method = 'classification',
|
|
expanding_window = training_config['expanding_window_meta_labeling'],
|
|
sliding_window_size = training_config['sliding_window_size_meta_labeling'],
|
|
retrain_every = training_config['retrain_every'],
|
|
scaler = training_config['scaler'],
|
|
no_of_classes = 'two',
|
|
level = 'meta_labeling',
|
|
print_results = False,
|
|
preloaded_models = preloaded_models
|
|
)
|
|
if len(models) > 1:
|
|
meta_preds = meta_preds.mean(axis = 1)
|
|
bet_size = meta_probabilities[meta_probabilities.columns[1::2]].mean(axis = 1)
|
|
else:
|
|
bet_size = meta_probabilities.iloc[:,1]
|
|
avg_predictions_with_sizing = input_predictions * bet_size
|
|
avg_predictions_with_sizing.rename("model_" + target_asset + "_" + model_suffix, inplace=True)
|
|
|
|
meta_result = evaluate_predictions(
|
|
model_name = "Meta",
|
|
target_returns = target_returns,
|
|
y_pred = avg_predictions_with_sizing,
|
|
y_true = y,
|
|
method = 'classification',
|
|
no_of_classes = 'two',
|
|
print_results = True,
|
|
discretize=False
|
|
)
|
|
meta_result.rename("model_" + target_asset + "_" + model_suffix, inplace=True)
|
|
|
|
|
|
return meta_result, avg_predictions_with_sizing, meta_probabilities, all_models_single_asset
|