python - Scikit-learn:如何获得真阳性、真阴性、假阳性和假阴性

标签 python machine-learning scikit-learn classification supervised-learning

我的问题:

我有一个大型 JSON 文件的数据集。我读取它并将其存储在 trainList 变量中。

接下来,我对其进行预处理 - 以便能够使用它。

完成后我开始分类:

  1. 我使用 kfold 交叉验证方法来获得均值 准确度并训练分类器。
  2. 我做出预测并获得该折叠的准确性和混淆矩阵。
  3. 在此之后,我想获取 True Positive(TP)True Negative(TN)False Positive(FP)False Negative(FN) 值。我将使用这些参数来获得 SensitivitySpecificity

最后,我会用它来放入 HTML 中,以显示带有每个标签的 TP 的图表。

代码:

我目前拥有的变量:

trainList #It is a list with all the data of my dataset in JSON form
labelList #It is a list with all the labels of my data 

大部分方法:

#I transform the data from JSON form to a numerical one
X=vec.fit_transform(trainList)

#I scale the matrix (don't know why but without it, it makes an error)
X=preprocessing.scale(X.toarray())

#I generate a KFold in order to make cross validation
kf = KFold(len(X), n_folds=10, indices=True, shuffle=True, random_state=1)

#I start the cross validation
for train_indices, test_indices in kf:
    X_train=[X[ii] for ii in train_indices]
    X_test=[X[ii] for ii in test_indices]
    y_train=[listaLabels[ii] for ii in train_indices]
    y_test=[listaLabels[ii] for ii in test_indices]

    #I train the classifier
    trained=qda.fit(X_train,y_train)

    #I make the predictions
    predicted=qda.predict(X_test)

    #I obtain the accuracy of this fold
    ac=accuracy_score(predicted,y_test)

    #I obtain the confusion matrix
    cm=confusion_matrix(y_test, predicted)

    #I should calculate the TP,TN, FP and FN 
    #I don't know how to continue

最佳答案

对于多类的情况,你需要的一切都可以从混淆矩阵中找到。例如,如果您的混淆矩阵如下所示:

confusion matrix

然后,您可以在每个类(class)中找到您要查找的内容,如下所示:

overlay

使用 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)

关于python - Scikit-learn:如何获得真阳性、真阴性、假阳性和假阴性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31324218/

相关文章:

python - 属性错误: 'Pipeline' object has no attribute 'partial_fit'

python - 我应该如何转换 scikit-learn 管道中的多个键/值列?

python 3.3.2 年龄 >= 24 : TypeError: unorderable types: str() >= int()

python - 转换日期格式 python - 不寻常的日期格式 - 提取 %Y%M%D

python - 从 main() 在 Python 中运行特定的单元测试

statistics - 如何将误报和漏报合并为一项措施

python - 训练逻辑分类器

python - 如何在python中拟合三个高斯峰?

python - 如果我们可以直接使用变量而无需初始化,为什么我们在tensorflow中使用tf.Variable?

c# - Accord.NET 中的多标签支持向量机