【发布时间】:2021-03-23 23:47:44
【问题描述】:
我已使用sklearn.metrics.jaccard_score 从我的 python 模型的二进制分类测试中收集参考分数。它输出如下所示,但是当我手动计算指标时,它会产生另一个值。我在这个函数的使用中误解了“jaccard”的含义吗?还是我用错了? sklearn 函数收集的所有其他指标都返回正确的值。
下面是我的代码,手动测试了 jaccard(在计算器中通过将向量作为集合进行比较得到相同的结果,因为我(不是那么)松了一口气)。
def test(X, y, model):
predictions = model.predict(X, verbose=1).ravel()
report = classification_report(y, predictions, target_names=['nao_doentes', 'doentes'])
confMatrix = confusion_matrix(y, predictions)
tn, fp, fn, tp = confMatrix.ravel()
jaccard = jaccard_score(y, predictions) # Se comportando de forma estranha
print(tn, fp, fn, tp)
print(predictions)
print(y)
print(report)
print(confMatrix)
print("Jaccard by function: {}".format(jaccard))
# Note that in binary classification, recall of the positive class is also known as “sensitivity”;
# recall of the negative class is “specificity”.
dice = ((2*tp) / ((2*tp) + fp + fn))
jaccard = ((tp + tn) / ((2*(tp + tn + fn + fp)) - (tp + tn)))
print(dice)
print("Jaccard by hand: {}".format(jaccard))
然后跟随输出:
2 0 1 1
[1. 0. 0. 0.]
[1 0 1 0]
precision recall f1-score support
nao_doentes 0.67 1.00 0.80 2
doentes 1.00 0.50 0.67 2
accuracy 0.75 4
macro avg 0.83 0.75 0.73 4
weighted avg 0.83 0.75 0.73 4
[[2 0]
[1 1]]
Jaccard by function: 0.5
0.6666666666666666
Jaccard by hand: 0.6
作为第二个问题,为什么classification_report 似乎将nao_doentes(未生病,葡萄牙语)设置为 1,而将doentes(生病)设置为 0?不应该反其道而行之吗? nao_doentes 在我的集合中设置为 0,doentes 设置为 1(所以在 y 中)。
【问题讨论】:
标签: python machine-learning scikit-learn neural-network metrics