refactor(Training): use date indexes instead of integers, need this to prepare for Events (#185)

This commit is contained in:
Mark Aron Szulyovszky
2022-01-24 12:22:30 +01:00
committed by GitHub
parent e80fffdb65
commit e6e2317fe0
15 changed files with 50 additions and 52 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ def train_meta_labeling_model(
models: list[tuple[str, Model]],
config: Config,
model_suffix: str,
from_index: Optional[int],
from_index: Optional[pd.Timestamp],
preloaded_models: Optional[list[tuple[str, pd.Series, list[pd.Series]]]] = None
) -> tuple[pd.Series, pd.Series, pd.DataFrame, list[Reporting.Single_Model]]:
+1 -1
View File
@@ -18,7 +18,7 @@ def train_primary_model(
expanding_window: bool,
sliding_window_size: int,
retrain_every: int,
from_index: Optional[int],
from_index: Optional[pd.Timestamp],
scaler: ScalerTypes,
no_of_classes: Literal['two', 'three-balanced', 'three-imbalanced'],
level: str,
+2 -2
View File
@@ -16,7 +16,7 @@ def primary_step(
target_returns: pd.Series,
config: Config,
reporting: Reporting,
from_index: Optional[int],
from_index: Optional[pd.Timestamp],
preloaded_training_step: Optional[Reporting.Training_Step] = None,
) -> tuple[Reporting.Training_Step, pd.DataFrame]:
training_step = Reporting.Training_Step(level='primary')
@@ -78,7 +78,7 @@ def secondary_step(
target_returns:pd.Series,
config: Config,
reporting: Reporting,
from_index: Optional[int],
from_index: Optional[pd.Timestamp],
preloaded_training_step: Optional[Reporting.Training_Step] = None,
) -> Reporting.Training_Step:
training_step = Reporting.Training_Step(level='secondary')
+17 -17
View File
@@ -15,7 +15,7 @@ def walk_forward_train(
expanding_window: bool,
window_size: int,
retrain_every: int,
from_index: Optional[int],
from_index: Optional[pd.Timestamp],
transformations: list[Transformation],
preloaded_transformations: Optional[list[pd.Series]],
) -> tuple[pd.Series, list[pd.Series]]:
@@ -24,7 +24,7 @@ def walk_forward_train(
transformations_over_time = [pd.Series(index=y.index).rename(t.get_name()) for t in transformations]
first_nonzero_return = max(get_first_valid_return_index(target_returns), get_first_valid_return_index(X.iloc[:,0]), get_first_valid_return_index(y))
train_from = first_nonzero_return + window_size + 1 if from_index is None else from_index
train_from = first_nonzero_return + window_size + 1 if from_index is None else X.index.to_list().index(from_index)
train_till = len(y)
iterations_before_retrain = 0
@@ -35,14 +35,14 @@ def walk_forward_train(
transformations = []
for index in tqdm(range(train_from, train_till)):
train_window_start = first_nonzero_return if expanding_window else index - window_size - 1
train_window_start = X.index[first_nonzero_return] if expanding_window else X.index[index - window_size - 1]
if iterations_before_retrain <= 0 or pd.isna(models_over_time[index-1]):
train_window_end = index - 1
train_window_end = X.index[index - 1]
X_expanding_window = X[first_nonzero_return:train_window_end]
y_expanding_window = y[first_nonzero_return:train_window_end]
X_expanding_window = X[X.index[first_nonzero_return]:train_window_end]
y_expanding_window = y[X.index[first_nonzero_return]:train_window_end]
if preloaded_transformations is not None and len(transformations) > 0:
current_transformations = [transformation_over_time[index] for transformation_over_time in preloaded_transformations]
@@ -66,9 +66,9 @@ def walk_forward_train(
iterations_before_retrain = retrain_every
models_over_time[index] = current_model
models_over_time[X.index[index]] = current_model
for transformation_index, transformation in enumerate(current_transformations):
transformations_over_time[transformation_index][index] = transformation
transformations_over_time[transformation_index][X.index[index]] = transformation
iterations_before_retrain -= 1
@@ -81,12 +81,12 @@ def walk_forward_inference(
X: pd.DataFrame,
expanding_window: bool,
window_size: int,
from_index: Optional[int],
from_index: Optional[pd.Timestamp],
) -> tuple[pd.Series, pd.DataFrame]:
predictions = pd.Series(index=X.index).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 from_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)
inference_till = X.shape[0]
first_model = model_over_time[inference_from]
@@ -98,16 +98,16 @@ def walk_forward_inference(
for index in tqdm(range(inference_from, inference_till)):
train_window_start = inference_from if expanding_window else index - window_size - 1
train_window_start = X.index[inference_from] if expanding_window else X.index[index - window_size - 1]
current_model = model_over_time[index]
current_transformations = [transformation_over_time[index] for transformation_over_time in transformations_over_time]
current_model = model_over_time[X.index[index]]
current_transformations = [transformation_over_time[X.index[index]] for transformation_over_time in transformations_over_time]
if current_model.predict_window_size == 'window_size':
next_timestep = X.iloc[train_window_start:index]
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.iloc[index:index+1]
next_timestep = X.loc[X.index[index]:X.index[index]]
for transformation in current_transformations:
next_timestep = transformation.transform(next_timestep)
@@ -115,9 +115,9 @@ def walk_forward_inference(
next_timestep = next_timestep.to_numpy()
prediction, probs = current_model.predict(next_timestep)
predictions[index] = prediction
predictions[X.index[index]] = prediction
if len(probabilities.columns) != len(probs):
probabilities = probabilities.reindex(columns = ["prob_" + str(num) for num in range(0, len(probs.T))])
probabilities.iloc[index] = probs
probabilities.loc[X.index[index]] = probs
return predictions, probabilities