从 CSV 多类数据集计算精度和召回率。
Calculate Precision and Recall from CSV multiclass datasets.
我需要从包含 multiclass classification 的 CSV 中计算 precision 和 recall .
更具体地说,我的 csv 结构如下:
real_class1, classified_class1
real_class2, classified_class3
real_class3, classified_class4
real_class4, classified_class2
总共有六个 class class化。
在二进制示例中,我很容易理解如何计算真阳性、假阳性、真阴性和假阴性。但是有了multi-class我不知道如何进行。
谁能给我举个例子?可能在 python?
按照评论中的建议,您必须创建混淆矩阵并按照以下步骤操作:
(我假设您使用 spark 是为了获得更好的机器学习处理性能)
from __future__ import division
import pandas as pd
import numpy as np
import pickle
from pyspark import SparkContext, SparkConf
from pyspark.sql import SQLContext, functions as fn
from sklearn.metrics import confusion_matrix
def getFirstColumn(line):
parts = line.split(',')
return parts[0]
def getSecondColumn(line):
parts = line.split(',')
return parts[1]
# Initialization
conf= SparkConf()
conf.setAppName("ConfusionMatrixPrecisionRecall")
sc = SparkContext(conf= conf) # SparkContext
sqlContext = SQLContext(sc) # SqlContext
data = sc.textFile('YOUR_FILE_PATH') # Load dataset
y_true = data.map(getFirstColumn).collect() # Split from line the class
y_pred = data.map(getSecondColumn).collect() # Split from line the tags
confusion_matrix = confusion_matrix(y_true, y_pred)
print("Confusion matrix:\n%s" % confusion_matrix)
# The True Positives are simply the diagonal elements
TP = np.diag(confusion_matrix)
print("\nTP:\n%s" % TP)
# The False Positives are the sum of the respective column, minus the diagonal element (i.e. the TP element
FP = np.sum(confusion_matrix, axis=0) - TP
print("\nFP:\n%s" % FP)
# The False Negatives are the sum of the respective row, minus the diagonal (i.e. TP) element:
FN = np.sum(confusion_matrix, axis=1) - TP
print("\nFN:\n%s" % FN)
num_classes = INTEGER #static kwnow a priori, put your number of classes
TN = []
for i in range(num_classes):
temp = np.delete(confusion_matrix, i, 0) # delete ith row
temp = np.delete(temp, i, 1) # delete ith column
TN.append(sum(sum(temp)))
print("\nTN:\n%s" % TN)
precision = TP/(TP+FP)
recall = TP/(TP+FN)
print("\nPrecision:\n%s" % precision)
print("\nRecall:\n%s" % recall)
我需要从包含 multiclass classification 的 CSV 中计算 precision 和 recall .
更具体地说,我的 csv 结构如下:
real_class1, classified_class1
real_class2, classified_class3
real_class3, classified_class4
real_class4, classified_class2
总共有六个 class class化。
在二进制示例中,我很容易理解如何计算真阳性、假阳性、真阴性和假阴性。但是有了multi-class我不知道如何进行。
谁能给我举个例子?可能在 python?
按照评论中的建议,您必须创建混淆矩阵并按照以下步骤操作:
(我假设您使用 spark 是为了获得更好的机器学习处理性能)
from __future__ import division
import pandas as pd
import numpy as np
import pickle
from pyspark import SparkContext, SparkConf
from pyspark.sql import SQLContext, functions as fn
from sklearn.metrics import confusion_matrix
def getFirstColumn(line):
parts = line.split(',')
return parts[0]
def getSecondColumn(line):
parts = line.split(',')
return parts[1]
# Initialization
conf= SparkConf()
conf.setAppName("ConfusionMatrixPrecisionRecall")
sc = SparkContext(conf= conf) # SparkContext
sqlContext = SQLContext(sc) # SqlContext
data = sc.textFile('YOUR_FILE_PATH') # Load dataset
y_true = data.map(getFirstColumn).collect() # Split from line the class
y_pred = data.map(getSecondColumn).collect() # Split from line the tags
confusion_matrix = confusion_matrix(y_true, y_pred)
print("Confusion matrix:\n%s" % confusion_matrix)
# The True Positives are simply the diagonal elements
TP = np.diag(confusion_matrix)
print("\nTP:\n%s" % TP)
# The False Positives are the sum of the respective column, minus the diagonal element (i.e. the TP element
FP = np.sum(confusion_matrix, axis=0) - TP
print("\nFP:\n%s" % FP)
# The False Negatives are the sum of the respective row, minus the diagonal (i.e. TP) element:
FN = np.sum(confusion_matrix, axis=1) - TP
print("\nFN:\n%s" % FN)
num_classes = INTEGER #static kwnow a priori, put your number of classes
TN = []
for i in range(num_classes):
temp = np.delete(confusion_matrix, i, 0) # delete ith row
temp = np.delete(temp, i, 1) # delete ith column
TN.append(sum(sum(temp)))
print("\nTN:\n%s" % TN)
precision = TP/(TP+FP)
recall = TP/(TP+FN)
print("\nPrecision:\n%s" % precision)
print("\nRecall:\n%s" % recall)