mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-08 08:37:49 +00:00
feat(Project): use SKLearn models directly, removed custom ensembling, use 5 minute data, batch inference, numba cusum filter (#192)
* feat(Project): use 5 minute data, running training in parallel, sped up cusum filter by 10x with numba * fix(WalkForward): inference mini-batch parallelization * fix(WalkForward): don't use the parallel version of any of the functions * feat(CI): download the data required * fix(Project): 5min_crypto folder added * fix(Evaluate): make sure we have numerical stability in returns * feat(Models): use SKLearn models directly to enable composability * feat(Inference): batched inference now working, added forecasting_horizon * fix(Inference): works again * fix(Inference) * chore(Models): remove unused Ensemble model * fix(Labeller): don't just forward shift returns, also take the sum of the data happened until then * Update test.yml
This commit is contained in:
committed by
GitHub
parent
5c94af8b01
commit
9d47ee942d
@@ -18,7 +18,7 @@ def walk_forward_inference(
|
||||
retrain_every: int,
|
||||
from_index: Optional[pd.Timestamp],
|
||||
) -> tuple[PredictionsSeries, ProbabilitiesDataFrame]:
|
||||
predictions = pd.Series(index=X.index).rename(model_name)
|
||||
predictions = pd.Series(index=X.index, dtype='object').rename(model_name)
|
||||
probabilities = pd.DataFrame(index=X.index)
|
||||
|
||||
inference_from = max(get_first_valid_return_index(model_over_time), get_first_valid_return_index(X.iloc[:,0])) if from_index is None else X.index.to_list().index(from_index)
|
||||
@@ -31,32 +31,38 @@ def walk_forward_inference(
|
||||
if first_model.data_transformation == 'original':
|
||||
transformations_over_time = []
|
||||
|
||||
results = ray.get([__inference_from_window.remote(index, inference_from, retrain_every, X, model_over_time, transformations_over_time, expanding_window, window_size) for index in range(inference_from, inference_till)])
|
||||
for index, prediction, probs in results:
|
||||
predictions[X.index[index]] = prediction
|
||||
probabilities.loc[X.index[index]] = probs
|
||||
batch_size = int((inference_till - inference_from) / 10)
|
||||
batched_results = ray.get([__inference_from_window.remote(index, index + batch_size, inference_from, retrain_every, X, model_over_time, transformations_over_time, expanding_window, window_size) for index in range(inference_from, inference_till)])
|
||||
for batch in batched_results:
|
||||
for index, prediction, probs in batch:
|
||||
predictions[X.index[index]] = prediction
|
||||
probabilities.loc[X.index[index]] = probs
|
||||
|
||||
return predictions, probabilities
|
||||
|
||||
@ray.remote
|
||||
def __inference_from_window(index: int, inference_from: int, retrain_every: int, X: XDataFrame, model_over_time: ModelOverTime, transformations_over_time: TransformationsOverTime, expanding_window: bool, window_size: int) -> tuple[int, float, pd.Series]:
|
||||
|
||||
last_model_index = index - ((index - inference_from) % retrain_every)
|
||||
train_window_start = X.index[inference_from] if expanding_window else X.index[index - window_size - 1]
|
||||
|
||||
current_model = model_over_time[X.index[last_model_index]]
|
||||
current_transformations = [transformation_over_time[X.index[last_model_index]] for transformation_over_time in transformations_over_time]
|
||||
|
||||
if current_model.predict_window_size == 'window_size':
|
||||
next_timestep = X.loc[train_window_start:X.index[index]]
|
||||
else:
|
||||
# we need to get a Dataframe out of it, since the transformation step always expects a 2D array, but it's equivalent to X.iloc[index]
|
||||
next_timestep = X.loc[X.index[index]:X.index[index]]
|
||||
def __inference_from_window(index_start: int, index_end: int, inference_from: int, retrain_every: int, X: XDataFrame, model_over_time: ModelOverTime, transformations_over_time: TransformationsOverTime, expanding_window: bool, window_size: int) -> list[tuple[int, float, pd.Series]]:
|
||||
|
||||
for transformation in current_transformations:
|
||||
next_timestep = transformation.transform(next_timestep)
|
||||
results = []
|
||||
for index in range(index_start, index_end):
|
||||
last_model_index = index - ((index - inference_from) % retrain_every)
|
||||
train_window_start = X.index[inference_from] if expanding_window else X.index[index - window_size - 1]
|
||||
|
||||
next_timestep = next_timestep.to_numpy()
|
||||
current_model = model_over_time[X.index[last_model_index]]
|
||||
current_transformations = [transformation_over_time[X.index[last_model_index]] for transformation_over_time in transformations_over_time]
|
||||
|
||||
prediction, probs = current_model.predict(next_timestep)
|
||||
return index, prediction, probs
|
||||
if current_model.predict_window_size == 'window_size':
|
||||
next_timestep = X.loc[train_window_start:X.index[index]]
|
||||
else:
|
||||
# we need to get a Dataframe out of it, since the transformation step always expects a 2D array, but it's equivalent to X.iloc[index]
|
||||
next_timestep = X.loc[X.index[index]:X.index[index]]
|
||||
|
||||
for transformation in current_transformations:
|
||||
next_timestep = transformation.transform(next_timestep)
|
||||
|
||||
next_timestep = next_timestep.to_numpy()
|
||||
|
||||
prediction, probs = current_model.predict(next_timestep)
|
||||
results.append((index, prediction, probs))
|
||||
|
||||
return results
|
||||
Reference in New Issue
Block a user