mirror of
https://github.com/webclinic017/drift.git
synced 2026-07-27 18:57:55 +00:00
feat(Baseline): added one-split baseline, pure sklearn (#247)
* feat(Baseline): added sklearn_baseline * feat(Baseline): completely working
This commit is contained in:
committed by
GitHub
parent
5482e3fc95
commit
7deeb2de01
@@ -20,7 +20,6 @@ def hash_data_config(data_config: dict) -> str:
|
||||
data_config["target_asset"].path
|
||||
+ data_config["target_asset"].file_name,
|
||||
data_config["load_non_target_asset"],
|
||||
data_config["start_date"],
|
||||
hash_feature_extractors(data_config["own_features"]),
|
||||
hash_feature_extractors(data_config["other_features"]),
|
||||
hash_feature_extractors(data_config["exogenous_features"]),
|
||||
|
||||
+2
-1
@@ -54,6 +54,7 @@ def get_minimal_config() -> RawConfig:
|
||||
meta_models = ["LogisticRegression_two_class", "LGBM"]
|
||||
|
||||
return RawConfig(
|
||||
start_date="2021-01-01",
|
||||
dimensionality_reduction_ratio=0,
|
||||
n_features_to_select=0,
|
||||
initial_window_size=3800,
|
||||
@@ -71,7 +72,7 @@ def get_minimal_config() -> RawConfig:
|
||||
meta_models=meta_models,
|
||||
event_filter="none",
|
||||
event_filter_multiplier=3.5,
|
||||
remove_overlapping_events=True,
|
||||
remove_overlapping_events=False,
|
||||
labeling="two_class",
|
||||
forecasting_horizon=10,
|
||||
transaction_costs=0.002,
|
||||
|
||||
+10
-10
@@ -11,20 +11,20 @@ from config.hashing import hash_data_config
|
||||
from .types import XDataFrame, ReturnSeries
|
||||
from diskcache import Cache
|
||||
|
||||
cache = Cache(".cachedir/data")
|
||||
# cache = Cache(".cachedir/data")
|
||||
|
||||
|
||||
def load_data(**kwargs) -> tuple[XDataFrame, ReturnSeries]:
|
||||
hashed = hash_data_config(kwargs)
|
||||
if hashed in cache:
|
||||
return cache.get(hashed)
|
||||
else:
|
||||
return_value = __load_data(**kwargs)
|
||||
cache[hashed] = return_value
|
||||
return return_value
|
||||
# def load_data(**kwargs) -> tuple[XDataFrame, ReturnSeries]:
|
||||
# hashed = hash_data_config(kwargs)
|
||||
# if hashed in cache:
|
||||
# return cache.get(hashed)
|
||||
# else:
|
||||
# return_value = __load_data(**kwargs)
|
||||
# cache[hashed] = return_value
|
||||
# return return_value
|
||||
|
||||
|
||||
def __load_data(
|
||||
def load_data(
|
||||
assets: DataCollection,
|
||||
other_assets: DataCollection,
|
||||
exogenous_data: DataCollection,
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
from re import S
|
||||
from typing import Optional
|
||||
|
||||
from sklearn.model_selection import train_test_split
|
||||
|
||||
from config.types import Config, RawConfig
|
||||
from config.preprocess import preprocess_config
|
||||
from config.presets import get_default_config, get_minimal_config
|
||||
|
||||
from data_loader.load import load_data
|
||||
from data_loader.process import check_data
|
||||
|
||||
from labeling.process import label_data
|
||||
|
||||
from sklearn.metrics import accuracy_score
|
||||
|
||||
|
||||
def run_sklearn_pipeline(raw_config: RawConfig):
|
||||
config = preprocess_config(raw_config)
|
||||
run_sklearn_training(config)
|
||||
|
||||
|
||||
def run_sklearn_training(config: Config):
|
||||
|
||||
print("---> Load data, check for validity")
|
||||
X, returns = load_data(
|
||||
assets=config.assets,
|
||||
other_assets=config.other_assets,
|
||||
exogenous_data=config.exogenous_data,
|
||||
target_asset=config.target_asset,
|
||||
load_non_target_asset=config.load_non_target_asset,
|
||||
own_features=config.own_features,
|
||||
other_features=config.other_features,
|
||||
exogenous_features=config.exogenous_features,
|
||||
start_date=config.start_date,
|
||||
)
|
||||
|
||||
assert check_data(X, config) == True, "Data is not valid."
|
||||
|
||||
print("---> Filter for significant events when we want to trade, and label data")
|
||||
events, X, y, forward_returns = label_data(
|
||||
event_filter=config.event_filter,
|
||||
event_labeller=config.labeling,
|
||||
X=X,
|
||||
returns=returns,
|
||||
remove_overlapping_events=config.remove_overlapping_events,
|
||||
)
|
||||
|
||||
X_train, X_test, y_train, y_test = train_test_split(
|
||||
X, y, test_size=0.2, random_state=42, shuffle=False
|
||||
)
|
||||
|
||||
print("---> Train directional models")
|
||||
|
||||
for transformation in config.transformations:
|
||||
X_train = transformation.fit_transform(X_train, y_train)
|
||||
|
||||
for transformation in config.transformations:
|
||||
X_test = transformation.transform(X_test)
|
||||
|
||||
config.directional_model.fit(X_train, y_train)
|
||||
preds = config.directional_model.predict(X_test)
|
||||
print(accuracy_score(y_test, preds))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_sklearn_pipeline(
|
||||
raw_config=get_minimal_config(),
|
||||
)
|
||||
Reference in New Issue
Block a user