强制sklearn cross val score使用分层k折?

Forcing sklearn cross val score to use stratified k fold?

基于Sklearn Docs

使用这个:

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

示例:

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) 

sklearn documentation中写道:

"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."...

如果您的模型是分类器,只需使用整数即可 StratifiedKFold