mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-02 13:47:47 +00:00
1cd0119589
* fix(FeatureExtractor): apply log to transform some series to normality * feat(DataLoader): add ability of not returning returns when they're not needed (exogenous data), applied log to certain features * feat(FeatureExtractors): added standard scaling for exogenous data * feat(FeatureSelection): scale data with the passed in scaler before doing feature-selection * fix(Config): sweep config * feat(Models): output probability, store it * feat(Core): added caching to select_features() and load_data() * fix(Dependencies): added diskcache * fix(Training): error when creating results DF * feat(Models): added xgboost, fixed tests * refactor(Cache): moved hashing to a separate function, created wrapper functions to separate business logic and caching * fix(Tests): new syntax * fix(Model): XGboost can't handle -1 class, so we'll use the deprecated label_encoder fornow * fix(Model): XGBoost config * feat(Cache): add run_clear_cache script * fix(Pipeline) accidentally re-instatiating all_predictions for each asset
55 lines
1.5 KiB
Python
55 lines
1.5 KiB
Python
from __future__ import annotations
|
|
from typing import Literal, Optional
|
|
from sklearn.base import clone
|
|
from abc import ABC, abstractmethod
|
|
import numpy as np
|
|
|
|
class Model(ABC):
|
|
|
|
data_scaling: Literal["scaled", "unscaled"]
|
|
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']
|
|
|
|
@abstractmethod
|
|
def fit(self, X: np.ndarray, y: np.ndarray) -> None:
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def predict(self, X) -> tuple[float, np.ndarray]:
|
|
raise NotImplementedError
|
|
|
|
@abstractmethod
|
|
def clone(self) -> Model:
|
|
raise NotImplementedError
|
|
|
|
def get_name(self) -> str:
|
|
raise NotImplementedError
|
|
|
|
|
|
class SKLearnModel(Model):
|
|
|
|
data_scaling = 'scaled'
|
|
only_column = None
|
|
feature_selection = 'on'
|
|
model_type = 'ml'
|
|
predict_window_size = 'single_timestamp'
|
|
|
|
def __init__(self, model):
|
|
self.model = model
|
|
|
|
def fit(self, X: np.ndarray, y: np.ndarray) -> None:
|
|
self.model.fit(X, y)
|
|
|
|
def predict(self, X) -> tuple[float, np.ndarray]:
|
|
pred = self.model.predict(X).item()
|
|
probability = self.model.predict_proba(X).squeeze()
|
|
return (pred, probability)
|
|
|
|
def clone(self) -> SKLearnModel:
|
|
return SKLearnModel(clone(self.model))
|
|
|
|
def get_name(self) -> str:
|
|
return self.model.__class__.__name__ |