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:
Mark Aron Szulyovszky
2022-01-17 11:43:51 +01:00
committed by GitHub
parent 31dc847be1
commit 6b26643ece
27 changed files with 240 additions and 278 deletions
-1
View File
@@ -9,7 +9,6 @@ class StaticAverageModel(Model):
data_transformation = 'original'
only_column = 'model_'
feature_selection = 'off'
model_type = 'static'
predict_window_size = 'single_timestamp'
+1 -2
View File
@@ -7,9 +7,8 @@ import numpy as np
class Model(ABC):
method: Literal["regression", "classification"]
data_transformation: Literal["transformed", "original"]
feature_selection: Literal["on", "off"]
# data_format: Literal["wide", "narrow"]
only_column: Optional[str]
model_type: Literal['ml', 'static']
predict_window_size: Literal['single_timestamp', 'window_size']
+92 -103
View File
@@ -1,111 +1,100 @@
from models.sklearn import SKLearnModel
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import RandomForestRegressor
from sklearnex.ensemble import RandomForestClassifier
from sklearnex.ensemble import RandomForestRegressor
model_map = {
"regression_models": dict(),
"classification_models": dict(),
"ensemble_models": dict()
}
default_feature_selector_classification = SKLearnModel(RandomForestClassifier(n_jobs=-1, max_depth=20, random_state=1), 'classification')
default_feature_selector_regression = SKLearnModel(RandomForestRegressor(n_jobs=-1, max_depth=20, random_state=1), 'regression')
def get_model_map(config:dict):
if len(config['primary_models']) > 0 and isinstance(config['primary_models'][0], str):
print("Going to Load models")
combined_list = config['primary_models'] + config['meta_labeling_models'] + [config['ensemble_model']]
for model_name in combined_list:
if model_name == 'LR':
from sklearn.linear_model import LinearRegression
model_map['regression_models']['LR'] = SKLearnModel(LinearRegression(n_jobs=-1))
elif model_name == 'Lasso':
from sklearn.linear_model import Lasso
model_map['regression_models']['Lasso'] = SKLearnModel(Lasso(alpha=100, random_state=1))
elif model_name == 'Ridge':
from sklearn.linear_model import Ridge
model_map['regression_models']['Ridge'] = SKLearnModel(Ridge(alpha=0.1))
elif model_name == 'BayesianRidge':
from sklearn.linear_model import BayesianRidge
model_map['regression_models']['BayesianRidge'] = SKLearnModel(BayesianRidge())
elif model_name == 'KNN':
from sklearnex.neighbors import KNeighborsRegressor
model_map['regression_models']['KNN'] = SKLearnModel(KNeighborsRegressor(n_neighbors=25))
elif model_name == 'AB':
from sklearn.ensemble import AdaBoostRegressor
model_map['regression_models']['AB'] = SKLearnModel(AdaBoostRegressor(random_state=1))
elif model_name == 'MLP':
from sklearn.neural_network import MLPRegressor
model_map['regression_models']['MLP'] = SKLearnModel(MLPRegressor(hidden_layer_sizes=(100,20), max_iter=1000))
elif model_name == 'RFR':
# from sklearn.ensemble import RandomForestRegressor
model_map['regression_models']['RFR'] = SKLearnModel(RandomForestRegressor(n_jobs=-1, max_depth=20, random_state=1))
elif model_name == 'SVR':
from sklearnex.svm import SVR
model_map['regression_models']['SVR'] = SKLearnModel(SVR(kernel='rbf', C=1e3, gamma=0.1))
elif model_name == 'StaticNaive':
from models.naive import StaticNaiveModel
model_map['regression_models']['StaticNaive'] = StaticNaiveModel()
elif model_name == 'DNN':
from models.neural import LightningNeuralNetModel
from models.pytorch.neural_nets import MultiLayerPerceptron
import torch.nn.functional as F
model_map['regression_models']['DNN'] = LightningNeuralNetModel(
MultiLayerPerceptron(
hidden_layers_ratio = [1.0],
probabilities = False,
loss_function = F.mse_loss),
max_epochs=15
)
elif model_name == 'LR_two_class':
from sklearn.linear_model import LogisticRegression
model_map['classification_models']['LR_two_class'] = SKLearnModel(LogisticRegression(C=10, random_state=1, solver='liblinear', max_iter=1000))
elif model_name == 'LR_three_class':
from sklearnex.linear_model import LogisticRegression as LogisticRegression_EX
model_map['classification_models']['LR_three_class'] = SKLearnModel(LogisticRegression_EX(C=10, random_state=1, max_iter=1000, n_jobs=-1))
elif model_name == 'LDA':
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
model_map['classification_models']['LDA'] = SKLearnModel(LinearDiscriminantAnalysis())
elif model_name == 'KNN':
from sklearn.neighbors import KNeighborsClassifier
model_map['classification_models']['KNN'] = SKLearnModel(KNeighborsClassifier())
elif model_name == 'CART':
from sklearn.tree import DecisionTreeClassifier
model_map['classification_models']['CART'] = SKLearnModel(DecisionTreeClassifier(max_depth=15, random_state=1))
elif model_name == 'NB':
from sklearn.naive_bayes import GaussianNB
model_map['classification_models']['NB'] = SKLearnModel(GaussianNB())
elif model_name == 'AB':
from sklearn.ensemble import AdaBoostClassifier
model_map['classification_models']['AB'] = SKLearnModel(AdaBoostClassifier(n_estimators=15))
elif model_name == 'RFC':
# from sklearn.ensemble import RandomForestClassifier
model_map['classification_models']['RFC'] = SKLearnModel(RandomForestClassifier(n_jobs=-1, max_depth=20, random_state=1))
elif model_name == 'SVC':
from sklearn.svm import SVC
model_map['classification_models']['SVC'] = SKLearnModel(SVC(kernel='rbf', C=1e3, probability=True, random_state=1))
elif model_name == 'XGB_two_class':
from xgboost import XGBClassifier
from models.xgboost import XGBoostModel
model_map['classification_models']['XGB_two_class'] = XGBoostModel(XGBClassifier(n_jobs=-1, max_depth = 20, random_state=1, objective='binary:logistic', use_label_encoder= False, eval_metric='mlogloss'))
elif model_name == 'LGBM':
from lightgbm import LGBMClassifier
model_map['classification_models']['LGBM'] = SKLearnModel(LGBMClassifier(n_jobs=-1, max_depth=20, random_state=1))
elif model_name == 'StaticMom':
from models.momentum import StaticMomentumModel
model_map['classification_models']['StaticMom'] = StaticMomentumModel(allow_short=True)
elif model_name == 'Average':
from models.average import StaticAverageModel
model_map['ensemble_models']['Average'] = StaticAverageModel()
model_names_classification = list(model_map["classification_models"].keys())
model_names_regression = list(model_map["regression_models"].keys())
model_map = {
"primary_models": dict(),
"ensemble_models": dict(),
}
default_feature_selector_classification = SKLearnModel(RandomForestClassifier(n_jobs=-1, max_depth=20, random_state=1))
default_feature_selector_regression = SKLearnModel(RandomForestRegressor(n_jobs=-1, max_depth=20, random_state=1))
combined_list = config['primary_models'] + config['meta_labeling_models'] + [config['ensemble_model']]
for model_name in combined_list:
if model_name == 'LinearRegression':
from sklearn.linear_model import LinearRegression
model_map['primary_models']['LR'] = SKLearnModel(LinearRegression(n_jobs=-1), 'regression')
elif model_name == 'Lasso':
from sklearn.linear_model import Lasso
model_map['primary_models']['Lasso'] = SKLearnModel(Lasso(alpha=100, random_state=1), 'regression')
elif model_name == 'Ridge':
from sklearn.linear_model import Ridge
model_map['primary_models']['Ridge'] = SKLearnModel(Ridge(alpha=0.1), 'regression')
elif model_name == 'BayesianRidge':
from sklearn.linear_model import BayesianRidge
model_map['primary_models']['BayesianRidge'] = SKLearnModel(BayesianRidge(), 'regression')
elif model_name == 'KNN':
from sklearnex.neighbors import KNeighborsRegressor
model_map['primary_models']['KNN'] = SKLearnModel(KNeighborsRegressor(n_neighbors=25), 'regression')
elif model_name == 'AB':
from sklearn.ensemble import AdaBoostRegressor
model_map['primary_models']['AB'] = SKLearnModel(AdaBoostRegressor(random_state=1), 'regression')
elif model_name == 'MLP':
from sklearn.neural_network import MLPRegressor
model_map['primary_models']['MLP'] = SKLearnModel(MLPRegressor(hidden_layer_sizes=(100,20), max_iter=1000), 'regression')
elif model_name == 'RFR':
# from sklearn.ensemble import RandomForestRegressor
model_map['primary_models']['RFR'] = SKLearnModel(RandomForestRegressor(n_jobs=-1, max_depth=20, random_state=1), 'regression')
elif model_name == 'SVR':
from sklearnex.svm import SVR
model_map['primary_models']['SVR'] = SKLearnModel(SVR(kernel='rbf', C=1e3, gamma=0.1), 'regression')
elif model_name == 'StaticNaive':
from models.naive import StaticNaiveModel
model_map['primary_models']['StaticNaive'] = StaticNaiveModel()
elif model_name == 'DNN':
from models.neural import LightningNeuralNetModel
from models.pytorch.neural_nets import MultiLayerPerceptron
import torch.nn.functional as F
model_map['primary_models']['DNN'] = LightningNeuralNetModel(
MultiLayerPerceptron(
hidden_layers_ratio = [1.0],
probabilities = False,
loss_function = F.mse_loss),
max_epochs=15
)
elif model_name == 'LogisticRegression_two_class':
from sklearn.linear_model import LogisticRegression
model_map['primary_models']['LogisticRegression_two_class'] = SKLearnModel(LogisticRegression(C=10, random_state=1, solver='liblinear', max_iter=1000), 'classification')
elif model_name == 'LogisticRegression_three_class':
from sklearnex.linear_model import LogisticRegression as LogisticRegression_EX
model_map['primary_models']['LogisticRegression_three_class'] = SKLearnModel(LogisticRegression_EX(C=10, random_state=1, max_iter=1000, n_jobs=-1), 'classification')
elif model_name == 'LDA':
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
model_map['primary_models']['LDA'] = SKLearnModel(LinearDiscriminantAnalysis(), 'classification')
elif model_name == 'KNN':
from sklearn.neighbors import KNeighborsClassifier
model_map['primary_models']['KNN'] = SKLearnModel(KNeighborsClassifier(), 'classification')
elif model_name == 'CART':
from sklearn.tree import DecisionTreeClassifier
model_map['primary_models']['CART'] = SKLearnModel(DecisionTreeClassifier(max_depth=15, random_state=1), 'classification')
elif model_name == 'NB':
from sklearn.naive_bayes import GaussianNB
model_map['primary_models']['NB'] = SKLearnModel(GaussianNB(), 'classification')
elif model_name == 'AB':
from sklearn.ensemble import AdaBoostClassifier
model_map['primary_models']['AB'] = SKLearnModel(AdaBoostClassifier(n_estimators=15), 'classification')
elif model_name == 'RFC':
model_map['primary_models']['RFC'] = SKLearnModel(RandomForestClassifier(n_jobs=-1, max_depth=20, random_state=1), 'classification')
elif model_name == 'SVC':
from sklearn.svm import SVC
model_map['primary_models']['SVC'] = SKLearnModel(SVC(kernel='rbf', C=1e3, probability=True, random_state=1), 'classification')
elif model_name == 'XGB_two_class':
from xgboost import XGBClassifier
from models.xgboost import XGBoostModel
model_map['primary_models']['XGB_two_class'] = XGBoostModel(XGBClassifier(n_jobs=-1, max_depth = 20, random_state=1, objective='binary:logistic', use_label_encoder= False, eval_metric='mlogloss'))
elif model_name == 'LGBM':
from lightgbm import LGBMClassifier
model_map['primary_models']['LGBM'] = SKLearnModel(LGBMClassifier(n_jobs=-1, max_depth=20, random_state=1), 'classification')
elif model_name == 'StaticMom':
from models.momentum import StaticMomentumModel
model_map['primary_models']['StaticMom'] = StaticMomentumModel(allow_short=True)
elif model_name == 'Average':
from models.average import StaticAverageModel
model_map['ensemble_models']['Average'] = StaticAverageModel()
return model_map, model_names_classification, model_names_regression, default_feature_selector_regression, default_feature_selector_classification
return model_map
+1 -1
View File
@@ -7,9 +7,9 @@ class StaticMomentumModel(Model):
Model that uses only one feature: momentum. It's positive if momentum is greater than 0, otherwise it's negative.
'''
method = 'classification'
data_transformation = 'original'
only_column = 'mom'
feature_selection = 'off'
model_type = 'static'
predict_window_size = 'single_timestamp'
+1 -1
View File
@@ -7,9 +7,9 @@ class StaticNaiveModel(Model):
Model that carries the last observation (from returns) to the next one, naively.
'''
method = 'regression'
data_transformation = 'original'
only_column = None
feature_selection = 'off'
model_type = 'static'
predict_window_size = 'single_timestamp'
+1 -1
View File
@@ -7,9 +7,9 @@ import pytorch_lightning as pl
class LightningNeuralNetModel(Model):
method = 'regression'
data_transformation = 'transformed'
only_column = None
feature_selection = 'off'
model_type = 'ml'
''' Standard lightning methods '''
+5 -3
View File
@@ -1,4 +1,5 @@
from __future__ import annotations
from typing import Literal
from models.base import Model
import numpy as np
from sklearn.base import clone
@@ -6,14 +7,15 @@ from sklearn.base import clone
class SKLearnModel(Model):
method: Literal["regression", "classification"]
data_transformation = 'transformed'
only_column = None
feature_selection = 'on'
model_type = 'ml'
predict_window_size = 'single_timestamp'
def __init__(self, model):
def __init__(self, model, method: Literal['regression', 'classification']):
self.model = model
self.method = method
def fit(self, X: np.ndarray, y: np.ndarray) -> None:
self.model.fit(X, y)
@@ -24,7 +26,7 @@ class SKLearnModel(Model):
return (pred, probability)
def clone(self) -> SKLearnModel:
return SKLearnModel(clone(self.model))
return SKLearnModel(clone(self.model), self.method)
def get_name(self) -> str:
return self.model.__class__.__name__
-1
View File
@@ -10,7 +10,6 @@ class StatsModel(Model):
# This is work in progress
data_transformation = 'transformed'
only_column = None
feature_selection = 'on'
model_type = 'ml'
predict_window_size = 'single_timestamp'
+1 -1
View File
@@ -6,9 +6,9 @@ from sklearn.base import clone
class XGBoostModel(Model):
method = 'classification'
data_transformation = 'transformed'
only_column = None
feature_selection = 'on'
model_type = 'ml'
predict_window_size = 'single_timestamp'