Forcing sklearn cross val score to use stratified k fold?
Asked Answered
C

2

9

Based on Sklearn Docs:

  • Is it possible to force the use of StratifiedKFold?
  • How can I know which KFold has been used?
Clowers answered 22/11, 2019 at 22:40 Comment(0)
B
9

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) 

Beamer answered 22/11, 2019 at 22:52 Comment(2)
and how do you check if it is the method I wantedClowers
you cannot explicitly check this but it is guaranteed by the source code internallyBeamer
N
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.

Nullifidian answered 18/12, 2021 at 14:45 Comment(1)
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 ReviewControversial

© 2022 - 2024 — McMap. All rights reserved.