polyregcv py from polyreg import k_vals [ number of folds cv np zeros (lenk_vals )max_p )cv loss np ones (( )for in p_range if np hstack ((xu**( - )) for in k_vals loss [for in range ( + )integer indices of test samples test_ind (( / )*( - np arange ( , / + - astype ('int 'train_ind np setdiff (np arange ( )test_ind x_train y_train [train_ind :] [train_ind :x_test y_test [test_ind :]ytest_ind fit model and evaluate test loss betahat solve x_train x_train x_train y_train loss append (normy_test x_test betahat * cv[jp - sum(loss)/ + basic plotting plt plot(p_range cv[ :]' 'plt plot(p_range cv[ :]' 'plt plot(p_range cv[ :]' --'plt show ( -fold cross-validation loss = = = number of parameters figure -fold cross-validation for the polynomial regression example