Recursive forecasting using random forest and xgboost

Viewed 11

I have built a random forest regressor and a xgboost regressor. The inputs to the models are 'Age', '10m[t-2]', '10m[t-1]', and '10m'. I want to forecast recursively and the test set is called forecasting test, which is a dictionary where the keys are ids of an athletes and the values are dataframes containing results. I don't understand what is happening because all the iterations are working as I expected, except the first one. The first iteration changes the scores in the array I can not understand why. Please see code and prints below. The ages for when the athletes was tested varies.


recursive_rf = np.array([])
recursive_xgb = np.array([])
results = {}
for i in forecasting_test:
    print(i)

    #Initialize arrays to first row of athletes' test

    recursive_rf = []
    recursive_xgb = []
    recursive_rf = forecasting_test[i].reset_index().iloc[0][['Age[1/2]','10m[t-2]','10m[t-1]','10m']].to_numpy().reshape(1,-1)
    recursive_xgb = forecasting_test[i].reset_index().iloc[0][['Age[1/2]','10m[t-2]','10m[t-1]','10m']].to_numpy().reshape(1,-1)

    #include all the actual sprinting times in the results for 
     comparison

    results[i] = forecasting_test[i].reset_index()[['Age[1/2]','10m[t+1]']]
    print(recursive_xgb)
    for row in results[i].index:
        print(recursive_xgb)

        #predict and include prediction in the recursive array and 
        results.

        rf_pred = rf.predict(recursive_rf.reshape(1,-1))
        xgb_pred = xgb.predict(recursive_xgb.reshape(1,-1))
        print(xgb_pred)
        results[i].at[row,'rf_pred'] = rf_pred
        results[i].at[row,'xgb_pred'] = xgb_pred
        recursive_rf[0] = recursive_rf[0] + 0.5
        recursive_rf = np.delete(recursive_rf, 2)
        recursive_rf = np.append(recursive_rf, rf_pred)
        recursive_xgb[0] = recursive_xgb[0] + 0.5
        recursive_xgb = np.delete(recursive_xgb,1)
        recursive_xgb = np.append(recursive_xgb, xgb_pred)

[[13.0 1.74 1.81 1.78]]

[[13.0 1.74 1.81 1.78]]

[1.7205878]

[13.5 2.31 2.2800000000000002 1.7205878496170044]

[1.7198453]

[14.0 2.2800000000000002 1.7205878496170044 1.7198452949523926]

[1.7282244]

[14.5 1.7205878496170044 1.7198452949523926 1.7282243967056274]

[1.6709775]

[15.0 1.7198452949523926 1.7282243967056274 1.6709774732589722]

[1.731813]

[15.5 1.7282243967056274 1.6709774732589722 1.7318129539489746]

[1.7032571]

[16.0 1.6709774732589722 1.7318129539489746 1.7032570838928223]

[1.6683441]

And one more:

[[12.0 1.74 1.84 1.69]]

[[12.0 1.74 1.84 1.69]]

[1.6803993]

[12.5 2.34 2.19 1.6803992986679077]

[1.7228063]

[13.0 2.19 1.6803992986679077 1.7228063344955444]

[1.7405888]

[13.5 1.6803992986679077 1.7228063344955444 1.7405887842178345]

[1.6507494]

[14.0 1.7228063344955444 1.7405887842178345 1.6507494449615479]

[1.6835413]

[14.5 1.7405887842178345 1.6507494449615479 1.6835412979125977]

[1.6193277]

[15.0 1.6507494449615479 1.6835412979125977 1.6193276643753052]

[1.726941]

[15.5 1.6835412979125977 1.6193276643753052 1.7269409894943237]

[1.6881592]

[16.0 1.6193276643753052 1.7269409894943237 1.6881592273712158]

[1.6308529]

0 Answers
Related