from sklearn feature_selection import selectpercentilef_regression select selectpercentile(score_func=f_regressionpercentile= fit(xyx_selected select transform(xprint("x_selected shape{}format(x_selected shape)out[ ]x_selected shape( in[ ]from sklearn model_selection import cross_val_score from sklearn linear_model import ridge print("cross-validation accuracy (cv only on ridge){ }formatnp mean(cross_val_score(ridge()x_selectedycv= )))out[ ]cross-validation accuracy (cv only on ridge) the mean computed by cross-validation is indicating very good model this clearly cannot be rightas our data is entirely random what happened here is that our feature selection picked out some features among the , random features that are (by chancevery well correlated with the target because we fit the feature selection outside of the cross-validationit could find features that are correlated both on the training and the test folds the information we leaked from the test folds was very informativeleading to highly unrealistic results let' compare this to proper cross-validation using pipelinein[ ]pipe pipeline([("select"selectpercentile(score_func=f_regressionpercentile= ))("ridge"ridge())]print("cross-validation accuracy (pipeline){ }formatnp mean(cross_val_score(pipexycv= )))out[ ]cross-validation accuracy (pipeline)- this timewe get negative scoreindicating very poor model using the pipelinethe feature selection is now inside the cross-validation loop this means features can only be selected using the training folds of the datanot the test fold the feature selection finds features that are correlated with the target on the training setbut because the data is entirely randomthese features are not correlated with the target on the test set in this examplerectifying the data leakage issue in the feature selection makes the difference between concluding that model works very well and concluding that model works not at all using pipelines in grid searches