强迫sklearn交叉val分数使用分层k折?

时间:2019-11-22 22:40:28

标签: python scikit-learn k-fold

1 个答案:

答案 0 :(得分:1)

使用此:

 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)