Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python scikit学习中的特异性_Python_Scikit Learn - Fatal编程技术网

Python scikit学习中的特异性

Python scikit学习中的特异性,python,scikit-learn,Python,Scikit Learn,我需要特异性用于我的分类,其定义如下: TN/(TN+FP) 我正在编写一个自定义记分器函数: from sklearn.metrics import make_scorer def specificity_loss_func(ground_truth, predictions): print predictions tp, tn, fn, fp = 0.0,0.0,0.0,0.0 for l,m in enumerate(ground_truth):

我需要
特异性
用于我的分类,其定义如下:
TN/(TN+FP)

我正在编写一个自定义记分器函数:

from sklearn.metrics import make_scorer
def specificity_loss_func(ground_truth, predictions):
    print predictions
    tp, tn, fn, fp = 0.0,0.0,0.0,0.0
    for l,m in enumerate(ground_truth):        
        if m==predictions[l] and m==1:
            tp+=1
        if m==predictions[l] and m==0:
            tn+=1
        if m!=predictions[l] and m==1:
            fn+=1
        if m!=predictions[l] and m==0:
            fp+=1
    `return tn/(tn+fp)

score = make_scorer(specificity_loss_func, greater_is_better=True)
那么

当我运行这些命令时,
p
打印为:

[0 0 0 0 0 0 0 0 0 0 0 0 0]
1.0

为什么我的
p
在输入
p=[0,0,0,1,0,1,1,1,1,0,0,1,0]
首先你需要知道:

DummyClassifier(strategy='most_frequent'...
将为您提供从训练集中返回最频繁标签的分类器。它甚至不考虑X中的样本。你可以传递任何东西,而不是这行中的基本事实:

clf_dummy = clf_dummy.fit(ground_truth, p)
训练结果和预测将保持不变,因为p中的大多数标签都是标签“0”

第二件你需要知道的事情: 使用接口make_scorer returns函数
scorer(estimator,X,y)
此函数将调用集合X上估计器的预测方法,并计算预测标签和y之间的特异性函数

因此,它在任何数据集上调用clf_dummy(不管是哪一个,它总是返回0),并返回0的向量,然后计算基本真理和预测之间的特异性损失。您的预测为0,因为0是训练集中的大多数类。你的分数等于1,因为没有假阳性预测

我修改了你的代码,增加了更多的方便

from sklearn.dummy import DummyClassifier
clf_dummy = DummyClassifier(strategy='most_frequent', random_state=0)
X = [[0],[0],[1],[0],[1],[1],[1],[0],[0],[1],[0],[0],[1]]
p  = [0,0,0,1,0,1,1,1,1,0,0,1,0]
clf_dummy = clf_dummy.fit(X, p)
score(clf_dummy, X, p)

您可以从中获得
特异性
。对于二进制分类问题,它类似于:

from sklearn.metrics import confusion_matrix
y_true = [0, 0, 0, 1, 1, 1, 1, 1]
y_pred = [0, 1, 0, 1, 0, 1, 0, 1]
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
specificity = tn / (tn+fp)

据我所知,“特异性”只是“回忆”的一个特例。召回率是针对实际阳性类别(TP/[TP+FN])计算的,而“特异性”是针对实际阴性类别(TN/[TN+FP])计算的相同类型

对于二进制分类问题,只有使用这样的特定术语才有意义。对于多类分类问题,讨论每个类的召回更为方便。即使在处理二进制分类问题时(例如,类0的召回,类1的召回),也没有理由不能以这种方式谈论召回

例如,回忆告诉我们实际患有癌症、被成功诊断为癌症的患者比例。然而,概括来说,你可以说X类回忆告诉我们实际属于X类的样本比例,被成功预测为属于X类


有鉴于此,您可以使用sklearn.metrics import classification_报告中的
生成一个。您还可以依赖sklearn.metrics导入精度_召回_fscore_支持
,具体取决于您的偏好

记住,在二元分类法中,正类的回忆也称为“敏感性”;阴性类的回忆是“特异性”,我用这个:

unique, counts = np.unique(y_test, return_counts=True)

for i in unique:
    score = precision_score(y_true, y_pred, labels=unique, pos_label=i)
    print('score ' + str(i) + '  ' + str(score))

如果scikit内置了
专用性
,那就太棒了。你的问题不是很清楚。希望现在更好。我应该更好地阅读文档。问:为什么要列出
X
的各个元素?因为scikit在我的机器上学习将1d数字列表视为一个样本。可能是因为我有Python3.4。要获得特异性,必须使用召回分数,而不是精度。所以,
recall\u score(y\u true,y\u pred,pos\u label=0)
。如果将精度分数与0标签一起使用,则实际上会得到负预测值。
unique, counts = np.unique(y_test, return_counts=True)

for i in unique:
    score = precision_score(y_true, y_pred, labels=unique, pos_label=i)
    print('score ' + str(i) + '  ' + str(score))