【发布时间】:2020-06-11 05:07:48
【问题描述】:
我想开发 pr_auc 作为 cross_validate() 的评分指标。所以我遵循了 Scikit Learn 的用户指南:https://scikit-learn.org/stable/modules/model_evaluation.html#scoring
我的代码如下所示:
from sklearn.datasets import make_classification
from sklearn.model_selection import cross_validate
from xgboost import XGBClassifier
from sklearn.metrics import auc, make_scorer
def cus_pr_auc(x, y):
score=auc(x, y)
return score
X, y = make_classification(n_samples=1000, n_features=2, n_redundant=0,
n_clusters_per_class=2, weights=[0.9], flip_y=0, random_state=7)
model = XGBClassifier(scale_pos_weight=9)
scores = cross_validate(model, X, y, scoring=make_scorer(cus_pr_auc, greater_is_better=True), cv=3, n_jobs=-1)
但是,我收到以下错误消息:
ValueError: x 既不增加也不减少:[1 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 1]。
如何修复代码?
【问题讨论】:
-
auc 在这里似乎是一个奇怪的选择。为什么不使用 f1_score 之类的东西?
-
Precision-Recall AUC 是针对罕见事件的算法性能的一个很好的测试。 Precision-Recall 曲线下的区域很好地近似了算法对稀有事件分类的技能。
标签: python scikit-learn cross-validation