当一个整数被传递给 的cv
参数时cross_val_score()
:
StratifiedKFold
如果估计器是分类器并且y
是二元或多类时使用。
- 在所有其他情况下,
KFold
使用。
from sklearn import datasets
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import cross_val_score, KFold, StratifiedKFold
data = datasets.load_breast_cancer()
x, y = data.data, data.target
print(cross_val_score(DecisionTreeClassifier(random_state=1), x, y, cv=5))
print(cross_val_score(DecisionTreeClassifier(random_state=1), x, y, cv=KFold(n_splits=5)))
print(cross_val_score(DecisionTreeClassifier(random_state=1), x, y, cv=StratifiedKFold(n_splits=5)))
[0.90434783 0.90434783 0.92035398 0.94690265 0.91150442]
[0.89473684 0.92982456 0.94736842 0.95614035 0.82300885]
[0.90434783 0.90434783 0.92035398 0.94690265 0.91150442]