8

Based on Sklearn Docs:

  • Is it possible to force the use of StratifiedKFold?
  • How can I know which KFold has been used?
Mario
  • 1,631
  • 2
  • 21
  • 51
Marine Galantin
  • 1,634
  • 1
  • 17
  • 28

2 Answers2

8

Use this:

 cross_val_score(estimator, X, y, cv=YOURCHOICE)

Example:

from sklearn import datasets, linear_model
from sklearn.model_selection import cross_val_score
diabetes = datasets.load_diabetes()
from sklearn.model_selection import StratifiedKFold

X = diabetes.data[:150]
y = diabetes.target[:150]
lasso = linear_model.Lasso()

skf = StratifiedKFold(n_splits=2)
results = cross_val_score(lasso, X, y, cv=skf) 

seralouk
  • 30,938
  • 9
  • 118
  • 133
0

in the sklearn documentation wrote that:

"For int/None inputs, if the estimator is a classifier and y is either binary or multiclass, StratifiedKFold is used. In all other cases, KFold is used."...

If your model is a classifier, just use an integer for using StratifiedKFold.

Mario
  • 1,631
  • 2
  • 21
  • 51
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30621448) – camille Dec 18 '21 at 22:28