IniziaInizia gratis

Cross-validation for R-squared

Cross-validation is a vital approach to evaluating a model. It maximizes the amount of data that is available to the model, as the model is not only trained but also tested on all of the available data.

In this exercise, you will build a linear regression model, then use 6-fold cross-validation to assess its accuracy for predicting sales using social media advertising expenditure. You will display the individual score for each of the six-folds.

The sales_df dataset has been split into y for the target variable, and X for the features, and preloaded for you. LinearRegression has been imported from sklearn.linear_model.

Questo esercizio fa parte del corso

Supervised Learning with scikit-learn

Visualizza il corso

Istruzioni dell'esercizio

  • Import KFold and cross_val_score.
  • Create kf by calling KFold(), setting the number of splits to six, shuffle to True, and setting a seed of 5.
  • Perform cross-validation using reg on X and y, passing kf to cv.
  • Print the cv_scores.

Esercizio pratico interattivo

Prova questo esercizio completando il codice di esempio.

# Import the necessary modules
from ____.____ import ____, ____

# Create a KFold object
kf = ____(n_splits=____, shuffle=____, random_state=____)

reg = LinearRegression()

# Compute 6-fold cross-validation scores
cv_scores = ____(____, ____, ____, cv=____)

# Print scores
print(____)
Modifica ed esegui il codice