我有两个数组:
correct = [['*','*'],['*','PER','*','GPE','ORG'],['GPE','*','*','*','ORG']]
predicted = [['PER','*'],['*','ORG','*','GPE','ORG'],['PER','*','*','*','MISC']]
正确和预测的长度相同(10K +)两个阵列中每个位置元素的长度也具有相同的长度。 我想用python计算这两个数组的精度,召回率和f1分数。 我有以下6个班级: ' PER'' ORG'' MISC'' LOC'' *',' GPE'
想要计算5个班级的精确度和召回率(除了' *')也要找到f1得分。 什么是使用python执行此操作的有效方法?
答案 0 :(得分:1)
您必须按照here展示您的列表,然后使用scikit-learn中的classification_report
:
correct = [['*','*'],['*','PER','*','GPE','ORG'],['GPE','*','*','*','ORG']]
predicted = [['PER','*'],['*','ORG','*','GPE','ORG'],['PER','*','*','*','MISC']]
target_names = ['PER','ORG','MISC','LOC','GPE'] # leave out '*'
correct_flat = [item for sublist in correct for item in sublist]
predicted_flat = [item for sublist in predicted for item in sublist]
from sklearn.metrics import classification_report
print(classification_report(correct_flat, predicted_flat, target_names=target_names))
结果:
precision recall f1-score support
PER 1.00 0.86 0.92 7
ORG 1.00 0.50 0.67 2
MISC 0.00 0.00 0.00 0
LOC 0.50 0.50 0.50 2
GPE 0.00 0.00 0.00 1
avg / total 0.83 0.67 0.73 12
在此特定示例中,您还会收到警告:
UndefinedMetricWarning: Recall and F-score are ill-defined and being set to 0.0 in labels with no true samples.
这是由于'MISC'
不存在于此处的真实标签中(correct
),但可以说这不应该发生在您的真实数据中。