根据scikit-learn文档,
根据定义,混淆矩阵C使得C [i,j]等于已知在第i组中但预计在第j组中的观测次数。
因此,在二进制分类中,真实负数的计数为C [0,0],错误负数的计数为C [1,0,0,真正正数的计数为C [1,1],错误正数的计数为C [0,1]。
CM = confusion_matrix(y_true, y_pred)
TN = CM[0][0]
FN = CM[1][0]
TP = CM[1][1]
FP = CM[0][1]
0
根据scikit-learn文档,
根据定义,混淆矩阵C使得C [i,j]等于已知在第i组中但预计在第j组中的观测次数。
因此,在二进制分类中,真实负数的计数为C [0,0],错误负数的计数为C [1,0,0,真正正数的计数为C [1,1],错误正数的计数为C [0,1]。
CM = confusion_matrix(y_true, y_pred)
TN = CM[0][0]
FN = CM[1][0]
TP = CM[1][1]
FP = CM[0][1]
0
您可以尝试sklearn.metrics.classification_report
,如下所示:
import sklearn
y_true = [1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0]
y_pred = [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0]
print sklearn.metrics.classification_report(y_true, y_pred)
输出:
precision recall f1-score support
0 0.80 0.57 0.67 7
1 0.50 0.75 0.60 4
avg / total 0.69 0.64 0.64 11
0
在scikit-learn'metrics'库中,有一个confusion_matrix方法可为您提供所需的输出。
您可以使用所需的任何分类器。在这里,我以KNeighbors为例。
from sklearn import metrics, neighbors
clf = neighbors.KNeighborsClassifier()
X_test = ...
y_test = ...
expected = y_test
predicted = clf.predict(X_test)
conf_matrix = metrics.confusion_matrix(expected, predicted)
>>> print conf_matrix
>>> [[1403 87]
[ 56 3159]]
docs: http : //scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html#sklearn.metrics.confusion_matrix
0
您可以从混淆矩阵中获取所有参数。混淆矩阵(2X2矩阵)的结构如下
TP|FP
FN|TN
所以
TP = cm[0][0]
FP = cm[0][1]
FN = cm[1][0]
TN = cm[1][1]
有关更多详细信息, 请访问https://en.wikipedia.org/wiki/Confusion_matrix
0
对于多类情况,可以从混淆矩阵中找到所需的一切。例如,如果您的混淆矩阵如下所示:
然后可以按以下方式找到您要查找的每个类的内容:
使用pandas / numpy,您可以一次对所有类执行此操作,如下所示:
FP = confusion_matrix.sum(axis=0) - np.diag(confusion_matrix)
FN = confusion_matrix.sum(axis=1) - np.diag(confusion_matrix)
TP = np.diag(confusion_matrix)
TN = confusion_matrix.values.sum() - (FP + FN + TP)
# Sensitivity, hit rate, recall, or true positive rate
TPR = TP/(TP+FN)
# Specificity or true negative rate
TNR = TN/(TN+FP)
# Precision or positive predictive value
PPV = TP/(TP+FP)
# Negative predictive value
NPV = TN/(TN+FN)
# Fall out or false positive rate
FPR = FP/(FP+TN)
# False negative rate
FNR = FN/(TP+FN)
# False discovery rate
FDR = FP/(TP+FP)
# Overall accuracy
ACC = (TP+TN)/(TP+FP+FN+TN)
0
如果您有两个包含预测值和实际值的列表;看起来就像您所做的那样,您可以将它们传递给一个函数,该函数将使用以下内容来计算TP,FP,TN,FN:
def perf_measure(y_actual, y_hat):
TP = 0
FP = 0
TN = 0
FN = 0
for i in range(len(y_hat)):
if y_actual[i]==y_hat[i]==1:
TP += 1
if y_hat[i]==1 and y_actual[i]!=y_hat[i]:
FP += 1
if y_actual[i]==y_hat[i]==0:
TN += 1
if y_hat[i]==0 and y_actual[i]!=y_hat[i]:
FN += 1
return(TP, FP, TN, FN)
我认为您可以从这里计算出您感兴趣的利率以及其他性能指标(例如特异性和敏感性)。
0
我写了一个仅使用numpy的版本。希望对您有帮助。
import numpy as np
def perf_metrics_2X2(yobs, yhat):
"""
Returns the specificity, sensitivity, positive predictive value, and
negative predictive value
of a 2X2 table.
where:
0 = negative case
1 = positive case
Parameters
----------
yobs : array of positive and negative ``observed`` cases
yhat : array of positive and negative ``predicted`` cases
Returns
-------
sensitivity = TP / (TP+FN)
specificity = TN / (TN+FP)
pos_pred_val = TP/ (TP+FP)
neg_pred_val = TN/ (TN+FN)
Author: Julio Cardenas-Rodriguez
"""
TP = np.sum( yobs[yobs==1] == yhat[yobs==1] )
TN = np.sum( yobs[yobs==0] == yhat[yobs==0] )
FP = np.sum( yobs[yobs==1] == yhat[yobs==0] )
FN = np.sum( yobs[yobs==0] == yhat[yobs==1] )
sensitivity = TP / (TP+FN)
specificity = TN / (TN+FP)
pos_pred_val = TP/ (TP+FP)
neg_pred_val = TN/ (TN+FN)
return sensitivity, specificity, pos_pred_val, neg_pred_val
0
在一个班轮得到真正postives等出来的混淆矩阵是的绽吧:
from sklearn.metrics import confusion_matrix
y_true = [1, 1, 0, 0]
y_pred = [1, 0, 1, 0]
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
print(tn, fp, fn, tp) # 1 1 1 1
0
我的问题:
我有一个很大的JSON文件的数据集。我阅读并将其存储在
trainList
变量中。接下来,我对其进行预处理-以便能够使用它。
一旦完成,就开始分类:
kfold
交叉验证方法来获得平均准确度并训练分类器。True Positive(TP)
,True Negative(TN)
,False Positive(FP)
和False Negative(FN)
值。我将使用这些参数来获得灵敏度和特异性 。最后,我将使用它来放入HTML,以显示带有每个标签TP的图表。
码:
我目前拥有的变量:
该方法的大部分: