本文介绍了如何将特异性定义为模型评估的可调用评分器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码来比较多个模型的性能:

I am using this code to compare performance of a number of models:

from sklearn import model_selection

X = input data
Y = binary labels

models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))

results = []
names = []
scoring = 'accuracy'

for name, model in models:
    kfold = model_selection.KFold(n_splits=10, random_state=7)
    cv_results = model_selection.cross_val_score(model, X, Y, cv=kfold,scoring=scoring)
    results.append(cv_results)
    names.append(name)
    msg = "%s: %.2f (%.2f)" % (name, cv_results.mean(), cv_results.std())
    print(msg)

我可以使用准确性"和回忆"作为评分,这些将提供准确性和敏感性.我怎样才能创建一个给我特异性"的记分员

I can use 'accuracy' and 'recall' as scoring and these will give accuracy and sensitivity. How can I create a scorer that gives me 'specificity'

特异性= TN/(TN+FP)

Specificity= TN/(TN+FP)

其中 TN 和 FP 是混淆矩阵中的真负值和假正值

where TN, and FP are true negative and false positive values in the confusion matrix

我已经试过了

def tp(y_true, y_pred):
error= confusion_matrix(y_true, y_pred)[0,0]/(confusion_matrix(y_true,y_pred)[0,0] + confusion_matrix(y_true, y_pred)[0,1])
return error

my_scorer = make_scorer(tp, greater_is_better=True)

然后

cv_results = model_selection.cross_val_score(model, X,Y,cv=kfold,scoring=my_scorer)

但它不适用于 n_split >=10计算 my_scorer 时出现此错误

but it will not work for n_split >=10I get this error for calculation of my_scorer

IndexError: 索引 1 超出轴 1 的边界,大小为 1

IndexError: index 1 is out of bounds for axis 1 with size 1

推荐答案

如果您将二元分类器的 recall_score 参数更改为 pos_label=0 您将获得特异性(默认是灵敏度,pos_label=1)

If you change the recall_score parameters for a binary classifier to pos_label=0 you get specificity (default is sensitivity, pos_label=1)

scoring = {
    'accuracy': make_scorer(accuracy_score),
    'sensitivity': make_scorer(recall_score),
    'specificity': make_scorer(recall_score,pos_label=0)
}

这篇关于如何将特异性定义为模型评估的可调用评分器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 20:33