将 "cross_val_predict" 与 "RepeatedStratifiedKFold" 一起使用会引发错误
Using "cross_val_predict" with "RepeatedStratifiedKFold" throws error
我正在尝试绘制 ROC AUC 曲线。我得到的分数如下:
# bagged decision trees on an imbalanced classification problem
from numpy import mean
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import BaggingClassifier
# generate dataset
X, y = make_classification(n_samples=10000, n_features=2, n_redundant=0,
n_clusters_per_class=1, weights=[0.99], flip_y=0, random_state=4)
# define model
model = BaggingClassifier()
# define evaluation procedure
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# evaluate model
scores = cross_val_score(model, X, y, scoring='roc_auc', cv=cv, n_jobs=-1)
# summarize performance
print('Mean ROC AUC: %.3f' % mean(scores))
但是当我尝试如下调用(用于绘制 ROC AUC 曲线)时:
scores2 = cross_val_predict(model, X, y, cv=cv,method='predict_proba')
我收到错误消息
ValueError: cross_val_predict only works for partitions
请问如何修改绘制曲线的代码?
我在another Whosebug question中发现了一些类似的问题。但目前还没有答案。
使用 cross_val_score(RFmodel, X, y, scoring='accuracy', cv=cv, n_jobs=-1, error_score='raise')
因为它不适合,它只有 returns y_predicted
值。
我正在尝试绘制 ROC AUC 曲线。我得到的分数如下:
# bagged decision trees on an imbalanced classification problem
from numpy import mean
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.ensemble import BaggingClassifier
# generate dataset
X, y = make_classification(n_samples=10000, n_features=2, n_redundant=0,
n_clusters_per_class=1, weights=[0.99], flip_y=0, random_state=4)
# define model
model = BaggingClassifier()
# define evaluation procedure
cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
# evaluate model
scores = cross_val_score(model, X, y, scoring='roc_auc', cv=cv, n_jobs=-1)
# summarize performance
print('Mean ROC AUC: %.3f' % mean(scores))
但是当我尝试如下调用(用于绘制 ROC AUC 曲线)时:
scores2 = cross_val_predict(model, X, y, cv=cv,method='predict_proba')
我收到错误消息
ValueError: cross_val_predict only works for partitions
请问如何修改绘制曲线的代码?
我在another Whosebug question中发现了一些类似的问题。但目前还没有答案。
使用 cross_val_score(RFmodel, X, y, scoring='accuracy', cv=cv, n_jobs=-1, error_score='raise')
因为它不适合,它只有 returns y_predicted
值。