mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-13 19:08:06 +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:
@@ -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"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user