mirror of
https://github.com/webclinic017/drift.git
synced 2026-07-27 18:57:55 +00:00
feat(Transformations): replaced feature selection pre-processing step with online version (with cache) (#170)
* 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
This commit is contained in:
committed by
GitHub
parent
31dc847be1
commit
6b26643ece
@@ -0,0 +1,39 @@
|
||||
from __future__ import annotations
|
||||
from transformations.base import Transformation
|
||||
from typing import Optional
|
||||
from copy import deepcopy
|
||||
from sklearn.decomposition import PCA
|
||||
import pandas as pd
|
||||
|
||||
class PCATransformation(Transformation):
|
||||
|
||||
pca: PCA
|
||||
|
||||
def __init__(self, ratio_components_to_keep: float, sliding_window_size: int):
|
||||
self.ratio_components_to_keep = ratio_components_to_keep
|
||||
self.sliding_window_size = sliding_window_size
|
||||
|
||||
def fit(self, X: pd.DataFrame, y: Optional[pd.Series] = None) -> None:
|
||||
self.pca = PCA(n_components = min(int(len(X.columns) * self.ratio_components_to_keep), self.sliding_window_size))
|
||||
self.pca.fit(X, y)
|
||||
|
||||
def fit_transform(self, X: pd.DataFrame, y: Optional[pd.Series] = None) -> pd.DataFrame:
|
||||
self.fit(X, y)
|
||||
return self.transform(X)
|
||||
|
||||
def transform(self, X: pd.DataFrame) -> pd.DataFrame:
|
||||
X = pd.DataFrame(self.pca.transform(X), index = X.index)
|
||||
X.columns = ['PCA_' + str(i) for i in range(1, len(X.columns)+1)]
|
||||
return X
|
||||
|
||||
def clone(self) -> PCATransformation:
|
||||
return deepcopy(self)
|
||||
|
||||
def get_name(self) -> str:
|
||||
return "PCA"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
from __future__ import annotations
|
||||
from transformations.base import Transformation
|
||||
from typing import Optional
|
||||
from copy import deepcopy
|
||||
from sklearn.feature_selection import RFE
|
||||
import pandas as pd
|
||||
from models.base import Model
|
||||
from models.model_map import default_feature_selector_classification
|
||||
|
||||
class RFETransformation(Transformation):
|
||||
|
||||
rfe: RFE
|
||||
n_feature_to_select: int
|
||||
|
||||
def __init__(self, n_feature_to_select: int, model: Model, step = 0.1):
|
||||
self.n_feature_to_keep = n_feature_to_select
|
||||
self.model = model
|
||||
if hasattr(self.model, 'model') == False: return
|
||||
if hasattr(self.model.model, 'feature_importances_') == False and hasattr(self.model.model, 'coef_') == False:
|
||||
model = default_feature_selector_classification
|
||||
self.rfe = RFE(model.model, n_features_to_select= n_feature_to_select, step=step)
|
||||
|
||||
def fit(self, X: pd.DataFrame, y: Optional[pd.Series] = None) -> None:
|
||||
if self.rfe is None: return
|
||||
self.rfe.fit(X, y)
|
||||
|
||||
def fit_transform(self, X: pd.DataFrame, y: Optional[pd.Series] = None) -> pd.DataFrame:
|
||||
if self.rfe is None: return X
|
||||
self.fit(X, y)
|
||||
return self.transform(X)
|
||||
|
||||
def transform(self, X: pd.DataFrame) -> pd.DataFrame:
|
||||
if self.rfe is None: return X
|
||||
return pd.DataFrame(X[X.columns[self.rfe.support_]], index= X.index)
|
||||
|
||||
def clone(self) -> RFETransformation:
|
||||
return deepcopy(self)
|
||||
|
||||
def get_name(self) -> str:
|
||||
return "RFE"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user