13.08.2022 Views

advanced-algorithmic-trading

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

290

Figure 20.4: Test MSE curves for multiple training-validation splits for a linear regression with

polynomial features of increasing degree

training-test split. However, we need to use the KFold object to iterate over k "folds".

In particular the KFold object provides an iterator that allows us to correctly index the

samples in the data set and create separate training/test folds. I have chosen k = 10 for this

example.

As with the validation set approach we create a pipeline of polynomial feature transformations

and then apply a linear regression model. We then calculate the test MSE and construct separate

test MSE curves for each fold. Finally we create an average MSE curve across folds:

..

..

def k_fold_cross_val_poly(folds, degrees, X, y):

"""

Use the k-fold cross validation method to create

k separate training test splits over linear

regression models of varying flexibility

"""

# Create the KFold object and

# set the initial fold to zero

n = len(X)

kf = KFold(n, n_folds=folds)

kf_dict = dict(

[("fold_%s" % i,[]) for i in range(1, folds+1)]

)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!