fix(Training): major bug in scaling, we only scaled training data, not the ones we're predicting from (#45)

This commit is contained in:
Mark Aron Szulyovszky
2021-12-19 13:14:14 +01:00
committed by GitHub
parent b456ec3cb7
commit 034bc1f213
+10 -8
View File
@@ -21,16 +21,14 @@ def walk_forward_train_test(
first_nonzero_return = max(get_first_valid_return_index(target_returns), get_first_valid_return_index(X.iloc[:,0]))
train_from = first_nonzero_return + window_size + 1
train_till = len(y)
iterations_since_retrain = 0
iterations_before_retrain = 0
if scaler is not None:
scaler = clone(scaler)
for index in range(train_from, train_till):
iterations_since_retrain += 1
if iterations_since_retrain >= retrain_every or pd.isna(models[index-1]):
if iterations_before_retrain <= 0 or pd.isna(models[index-1]):
train_window_start = index - window_size - 1
train_window_end = index - 1
@@ -38,26 +36,30 @@ def walk_forward_train_test(
# First we need to fit on the expanding window data slice
# This is our only way to avoid lookahead bia
X_expanding_window = X[first_nonzero_return:train_window_end]
scaler.fit(X_expanding_window)
scaler.fit(X_expanding_window.values)
X_slice = X[train_window_start:train_window_end]
y_slice = y[train_window_start:train_window_end]
if scaler is not None:
X_slice = scaler.transform(X_slice)
X_slice = scaler.transform(X_slice.values)
else:
X_slice = X_slice.to_numpy()
current_model = clone(model)
current_model.fit(X_slice, y_slice.to_numpy())
iterations_since_retrain = 0
iterations_before_retrain = retrain_every
else:
current_model = models[index-1]
models[index] = current_model
next_timestep = X.iloc[index].to_numpy().reshape(1, -1)
if scaler is not None:
next_timestep = scaler.transform(next_timestep)
prediction = current_model.predict(next_timestep).item()
predictions[index] = prediction
iterations_before_retrain -= 1
return models, predictions