如何从sklearn gridsearchcv获取灵敏度和特异性(真阳性率和真阴性率)?

How to acquire sensitivity and specificty(true positive rate and true negative rate) from sklearn's gridsearchcv?

我一直在使用 Gridsearchcv 和 RBF SVM(二元分类器)来获取验证精度热图。我使用的代码几乎直接来自 SKlearn 的网站。有没有办法从中找到敏感性和特异性?至于Gridsearchcv使用的参数取值范围?

如果您的问题是二进制或多class class化,那么 confusion matrix 可能就是您要找的。

from sklearn.metrics import confusion_matrix

y_true = [2, 0, 2, 2, 0, 1]
y_pred = [0, 0, 2, 2, 0, 2]
confusion_matrix(y_true, y_pred)

array([[2, 0, 0],
       [0, 0, 1],
       [1, 0, 2]])

解释如下:

对于属于 class 0 的示例,estimator 正确预测了其中的 100% (2/2)。
对于属于 class 1 的示例,估计器是 100% 错误的,因为它将唯一的示例预测为 class 2.
对于属于 class 2 的示例,估计器的正确率为 66% (2/3),因为它将 2 个示例预测为 class 2,将 1 个示例预测为 class 0。

对于二进制class化 :

y_true = [1, 0, 1, 0, 0, 1]
y_pred = [1, 0, 1, 1, 0, 1]

cm = confusion_matrix(y_true, y_pred)
print cm

tp = float(cm[0][0])/np.sum(cm[0])
tn = float(cm[1][1])/np.sum(cm[1])

print tp
print tn

[[2 1]
 [0 3]]
0.666666666667
1.0

关于您的 GridSearchCV 中使用的参数,您可以在 grid_scores_ 属性中找到它们。