mirror of
https://github.com/webclinic017/drift.git
synced 2026-08-20 14:28:09 +00:00
fix(Training): major bug in scaling, we only scaled training data, not the ones we're predicting from (#45)
This commit is contained in:
@@ -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]))
|
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_from = first_nonzero_return + window_size + 1
|
||||||
train_till = len(y)
|
train_till = len(y)
|
||||||
|
iterations_before_retrain = 0
|
||||||
iterations_since_retrain = 0
|
|
||||||
if scaler is not None:
|
if scaler is not None:
|
||||||
scaler = clone(scaler)
|
scaler = clone(scaler)
|
||||||
|
|
||||||
for index in range(train_from, train_till):
|
for index in range(train_from, train_till):
|
||||||
|
|
||||||
iterations_since_retrain += 1
|
if iterations_before_retrain <= 0 or pd.isna(models[index-1]):
|
||||||
|
|
||||||
if iterations_since_retrain >= retrain_every or pd.isna(models[index-1]):
|
|
||||||
train_window_start = index - window_size - 1
|
train_window_start = index - window_size - 1
|
||||||
train_window_end = index - 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
|
# First we need to fit on the expanding window data slice
|
||||||
# This is our only way to avoid lookahead bia
|
# This is our only way to avoid lookahead bia
|
||||||
X_expanding_window = X[first_nonzero_return:train_window_end]
|
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]
|
X_slice = X[train_window_start:train_window_end]
|
||||||
y_slice = y[train_window_start:train_window_end]
|
y_slice = y[train_window_start:train_window_end]
|
||||||
|
|
||||||
if scaler is not None:
|
if scaler is not None:
|
||||||
X_slice = scaler.transform(X_slice)
|
X_slice = scaler.transform(X_slice.values)
|
||||||
else:
|
else:
|
||||||
X_slice = X_slice.to_numpy()
|
X_slice = X_slice.to_numpy()
|
||||||
|
|
||||||
current_model = clone(model)
|
current_model = clone(model)
|
||||||
current_model.fit(X_slice, y_slice.to_numpy())
|
current_model.fit(X_slice, y_slice.to_numpy())
|
||||||
iterations_since_retrain = 0
|
iterations_before_retrain = retrain_every
|
||||||
else:
|
else:
|
||||||
current_model = models[index-1]
|
current_model = models[index-1]
|
||||||
|
|
||||||
models[index] = current_model
|
models[index] = current_model
|
||||||
|
|
||||||
next_timestep = X.iloc[index].to_numpy().reshape(1, -1)
|
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()
|
prediction = current_model.predict(next_timestep).item()
|
||||||
predictions[index] = prediction
|
predictions[index] = prediction
|
||||||
|
iterations_before_retrain -= 1
|
||||||
|
|
||||||
return models, predictions
|
return models, predictions
|
||||||
|
|||||||
Reference in New Issue
Block a user