accuracy, precision, recall, F1四个函数是分类问题中常见的四个模型评价函数。原来都是自己写代码来实现。现在没办法,懒了。所以打算直接调用 Sklearn.metrics 中的函数。第一次用,所有出现了几个问题,这里记录一下,省得自己以后又要再找。
from sklearn.metrics import accuracy_score
y_true = [2, 0, 2, 2, 0, 1]
y_pred = [0, 0, 2, 2, 0, 2]
accuracy = accuracy_score(y_true, y_pred)
print(accuracy)
0.6666666666666666
>>> from sklearn.metrics import confusion_matrix
>>> y_true = [2, 0, 2, 2, 0, 1]
>>> y_pred = [0, 0, 2, 2, 0, 2]
>>> confusion_matrix(y_true, y_pred)
array([[2, 0, 0],
[0, 0, 1],
[1, 0, 2]])
这三个不仅适合二分类,也适合多分类。只需要指出参数average=‘micro’/‘macro’/'weighted’
- macro:计算二分类metrics的均值,为每个类给出相同权重的分值。当小类很重要时会出问题,因为该macro-averging方法是对性能的平均。另一方面,该方法假设所有分类都是一样重要的,因此macro-averaging方法会对小类的性能影响很大
- micro: 给出了每个样本类以及它对整个metrics的贡献的pair(sample-weight),而非对整个类的metrics求和,它会每个类的metrics上的权重及因子进行求和,来计算整个份额。Micro-averaging方法在多标签(multilabel)问题中设置,包含多分类,此时,大类将被忽略
- weighted: 对于不均衡数量的类来说,计算二分类metrics的平均,通过在每个类的score上进行加权实现
1、多分类,当 average="micro" 时,算出的数值均相等。
from sklearn import metrics
y_test = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
predictions = [0, 0, 1, 1, 0, 0, 0, 2, 2, 0, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2]
accuracy = metrics.accuracy_score(y_test, predictions)
recall = metrics.recall_score(y_test, predictions, average="micro")
precision = metrics.precision_score(y_test, predictions, average="micro")
F1 = metrics.f1_score(y_test, predictions, average="micro")
print("accuracy:", accuracy, '\n', "precision:", precision, '\n', "recall:", recall, '\n', "F1 :", F1)
输出结果:
2、多分类,当 average="macro" 时,算出的数值与我们正常公式算出的结果相同。
from sklearn import metrics
y_test = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
predictions = [0, 0, 1, 1, 0, 0, 0, 2, 2, 0, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2]
accuracy = metrics.accuracy_score(y_test, predictions)
recall = metrics.recall_score(y_test, predictions, average="macro")
precision = metrics.precision_score(y_test, predictions, average="macro")
F1 = metrics.f1_score(y_test, predictions, average="macro")
print("accuracy:", accuracy, '\n', "precision:", precision, '\n', "recall:", recall, '\n', "F1 :", F1)
输出结果: