It may sounds like a silly question but let's take the RidgeCV model from sklearn.linear_model. This one has the parameter "cv".

cv : int, cross-validation generator or an iterable, optional

Determines the cross-validation splitting strategy. Possible inputs for cv are:

  • None, to use the efficient Leave-One-Out cross-validation (also known as Generalized Cross-Validation).

  • integer, to specify the number of folds.

  • CV splitter,
  • An iterable yielding (train, test) splits as arrays of indices.

For integer/None inputs, if y is binary or multiclass, sklearn.model_selection.StratifiedKFold is used, else, sklearn.model_selection.KFold is used.

My question is the following: Why do we have to call KFold() when the parameter 'cv' is already performing a k-fold ? (if integer specified)

To make it clearer, why do we have this:

kfolds = KFold(n_splits=10, shuffle=True, random_state=42)
ridge = RidgeCV(alphas=alphas_alt, cv=kfolds)

And not only this ?

ridge = RidgeCV(alphas=alphas_alt, cv=10)

You could do it, it is still OK. In the second one, shuffle option for KFold will be False, and you won't be able to set random_state in order for your analysis to be reproducible. Sometimes, it's more convenient to manage the splitter object at a greater detail, which is the case for the first usage.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.