feat(Selection): added toggleable feature selection step into the pipeline (#83)

* feat(Selection): added prototype feature selection python script

* feat(Utils): added some helpers for the future from Advances in Financial ML book

* feat(Selection): added RFECV

* feat(Selection): added configurable feature selection step into pipeline

* feat(Config): added level_1 & level_2 default config, PCA before feature selection process starts

* feat(Selection): added backup feature selector models if current one can't output feature importance, removed unnecessary array for level-2 models

* fix(Training): deal with zero first value coming out of static models

* feat(Sweep): added feature selection sweep

* fix(Sweep): config problem

* fix(Sweep): config

* chore(Utils): removed unnecessary purged k-fold crossval class

* feat(Config): added dimensionality_reduction as a separate flag

* fix(Sweep): config updated

* fix(Sweep): sweep name

* chore(Config): updated level_2 config to the best performing configuation
This commit is contained in:
Mark Aron Szulyovszky
2021-12-27 21:59:22 +01:00
committed by GitHub
parent a9b05dbd42
commit cc70d3f907
19 changed files with 442 additions and 59 deletions
+8
View File
@@ -0,0 +1,8 @@
import pandas as pd
from sklearn.decomposition import PCA
def reduce_dimensionality(X: pd.DataFrame, no_of_compoments: int) -> pd.DataFrame:
pca = PCA(n_components= no_of_compoments)
result = pd.DataFrame(pca.fit_transform(X), index= X.index)
result.columns = ['PCA_' + str(i) for i in range(1, no_of_compoments+1)]
return result
+23
View File
@@ -0,0 +1,23 @@
from sklearn.feature_selection import RFE
from sklearn.model_selection import TimeSeriesSplit
import pandas as pd
from models.base import Model, SKLearnModel
from sklearn.decomposition import PCA
def select_features(X: pd.DataFrame, y: pd.Series, model: Model, min_features_to_select: int, backup_model: SKLearnModel) -> pd.DataFrame:
''' Select features using RFECV, returns a pd.DataFrame (X) with only the selected features.'''
if model.model_type != 'ml': return X
# 2. Recursive feature selection
cv = TimeSeriesSplit(n_splits=5)
feat_selector_model = model.model
if hasattr(feat_selector_model, 'feature_importances_') == False and hasattr(feat_selector_model, 'coef_') == False:
feat_selector_model = backup_model.model
# selector = RFECV(feat_selector_model, cv = cv, step=5, min_features_to_select=min_features_to_select)
selector = RFE(feat_selector_model, n_features_to_select=10)
selector = selector.fit(X, y)
print("Kept %d features out of %d" % (selector.n_features_, X.shape[1]))
return pd.DataFrame(X[X.columns[selector.support_]], index= X.index)