python - 如何在 scikit learn 中为 cross_validate 制定自定义评分指标?

标签 python scikit-learn cross-validation

我想开发 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 is neither increasing nor decreasing : [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 更改为 roc_auc_score,如下所示,即可开始:

from sklearn.datasets import make_classification
from sklearn.model_selection import cross_validate
from xgboost import XGBClassifier
from sklearn.metrics import roc_auc_score, make_scorer

def cus_pr_auc(x, y):
    score=roc_auc_score(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)

scores
{'fit_time': array([0.0627017, 0.0569284, 0.046772 ]),
 'score_time': array([0.00534487, 0.00616908, 0.00347471]),
 'test_score': array([0.90244639, 0.90242424, 0.94969697])}

您的 auc 无法正常工作,因为根据docs :

x coordinates. These must be either monotonic increasing or monotonic decreasing.

因此出现了错误(另请参阅 here,了解错误消息的具体来源)。

通常,auc 是一个低级评分指标,用作 auc(fpr,tpr)

关于python - 如何在 scikit learn 中为 cross_validate 制定自定义评分指标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60428953/

相关文章:

python - 将复杂的 SQL 查询转换为 SQLAlchemy

machine-learning - 如何使用机器学习模型(或其特征系数)来解释该特征是否与特定类别相关?

machine-learning - 处理 SVM 中缺失的属性

python - Scikit-Learn:如何处理不可排序的类型错误?

python - 机器学习+Python : Drawing Validation curve

python - cross_val_score 是采用顺序样本还是随机样本?

python - 'PolynomialFeatures' 对象没有属性 'predict'

python - Tensorflow:tf.image.central_crop 问题

python - 在多输出 keras 模型中计算损失时,y_pred 和 y_true 是什么?

javascript - Selenium (Python): How to insert value on a hidden input?