计算微平均值时 F1-score 和 Accuracy 之间的差异 [重复]
pytorch 620
原文标题 :Difference between F1-score and Accuracy when computing micro-average [duplicate]
我在 scikit-learn 中尝试了许多 F1 micro 和 Accuracy 示例,在所有示例中,我发现 F1 micro 与 Accuracy 相同。这总是真的吗?
脚本
from sklearn import svm
from sklearn import metrics
from sklearn.cross_validation import train_test_split
from sklearn.datasets import load_iris
from sklearn.metrics import f1_score, accuracy_score
# prepare dataset
iris = load_iris()
X = iris.data[:, :2]
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# svm classification
clf = svm.SVC(kernel='rbf', gamma=0.7, C = 1.0).fit(X_train, y_train)
y_predicted = clf.predict(X_test)
# performance
print "Classification report for %s" % clf
print metrics.classification_report(y_test, y_predicted)
print("F1 micro: %1.4f\n" % f1_score(y_test, y_predicted, average='micro'))
print("F1 macro: %1.4f\n" % f1_score(y_test, y_predicted, average='macro'))
print("F1 weighted: %1.4f\n" % f1_score(y_test, y_predicted, average='weighted'))
print("Accuracy: %1.4f" % (accuracy_score(y_test, y_predicted)))
输出
Classification report for SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=None, degree=3, gamma=0.7, kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
precision recall f1-score support
0 1.00 0.90 0.95 10
1 0.50 0.88 0.64 8
2 0.86 0.50 0.63 12
avg / total 0.81 0.73 0.74 30
F1 micro: 0.7333
F1 macro: 0.7384
F1 weighted: 0.7381
Accuracy: 0.7333
F1 微 = 准确度
回复
我来回复-
shahensha 评论
在保证每个测试用例都被分配到一个类别的分类任务中,micro-F 相当于准确度。在多标签分类中不会出现这种情况。
2年前 -
Patrick 评论
我有同样的问题,所以我调查并想出了这个:
仅考虑理论,不可能每个数据集的准确性和
f1-score
都完全相同。原因是f1-score
独立于真阴性而准确度不是。通过获取数据集 where
f1 = acc
并向其添加真正的否定,你得到f1 != acc
。>>> from sklearn.metrics import accuracy_score as acc >>> from sklearn.metrics import f1_score as f1 >>> y_pred = [0, 1, 1, 0, 1, 0] >>> y_true = [0, 1, 1, 0, 0, 1] >>> acc(y_true, y_pred) 0.6666666666666666 >>> f1(y_true,y_pred) 0.6666666666666666 >>> y_true = [0, 1, 1, 0, 1, 0, 0, 0, 0] >>> y_pred = [0, 1, 1, 0, 0, 1, 0, 0, 0] >>> acc(y_true, y_pred) 0.7777777777777778 >>> f1(y_true,y_pred) 0.6666666666666666
2年前 -
Tanmoy Chowdhury 评论
这是因为我们正在处理多类分类,其中每个测试数据应该只属于 1 个类而不是多标签,在没有 TN 的情况下,我们可以将 True Negatives 称为 True Positives。
公式明智,
校正:F1 分数是 2*precision*recall /(precision+recall)
2年前 -
jasperr 评论
对于每个实例必须分类为一个(且只有一个)类的情况,微平均精度、召回率、f1 和准确率都是相等的。一个简单的方法是查看公式精度=TP/(TP+FP) 和召回率=TP/(TP+FN)。分子相同,一个类的每个FN都是另一个类的FP,这使得分母也相同。如果精度 = 召回率,那么 f1 也将相等。
对于任何输入都应该能够表明:
from sklearn.metrics import accuracy_score as acc from sklearn.metrics import f1_score as f1 f1(y_true,y_pred,average='micro')=acc(y_true,y_pred)
2年前