当前位置:实例文章 » 其他实例» [文章]评价指标--Micro Average, Weighted Average, Macro average

评价指标--Micro Average, Weighted Average, Macro average

发布人:shili8 发布时间:2024-12-25 22:31 阅读次数:0

**评价指标**

在机器学习和深度学习中,评价指标是用来评估模型性能的重要工具。不同的评价指标可以反映出模型在不同方面的表现。下面我们将介绍三种常见的评价指标:Micro Average、Weighted Average和Macro average。

**1. Micro Average**

Micro Average(微平均)是指对所有样本进行评估,然后计算准确率或F1分数。它不考虑类别之间的差异,仅关注每个样本的预测结果。

公式:Micro Average = (TP + TN) / (TP + TN + FP + FN)

其中:

* TP(True Positive):正确预测为正类的样本数量* TN(True Negative):正确预测为负类的样本数量* FP(False Positive):错误预测为正类的样本数量* FN(False Negative):错误预测为负类的样本数量**示例代码**

from sklearn.metrics import accuracy_score, f1_scorey_true = [0,1,1,0,1,0]
y_pred = [0,1,0,0,1,1]

micro_avg_accuracy = accuracy_score(y_true, y_pred)
print("Micro Average Accuracy:", micro_avg_accuracy)

micro_avg_f1 = f1_score(y_true, y_pred, average='micro')
print("Micro Average F1 Score:", micro_avg_f1)

**2. Weighted Average**

Weighted Average(加权平均)是指根据类别的分布情况对每个类别进行评估,然后计算准确率或F1分数。它考虑了类别之间的差异。

公式:Weighted Average = (TP1 + TP2 + ... + TPN) / (N * (TP1 + TN1 + FP1 + FN1) + N-1 * (TP2 + TN2 + FP2 + FN2) + ... +1 * (TPN + TNN + FPN + FNN))

其中:

* TPi、TNi、FPi、FNi:第i类的正确预测为正类的样本数量、正确预测为负类的样本数量、错误预测为正类的样本数量和错误预测为负类的样本数量* N:类别的数量**示例代码**
from sklearn.metrics import accuracy_score, f1_scorey_true = [0,1,1,0,1,0]
y_pred = [0,1,0,0,1,1]

weighted_avg_accuracy = accuracy_score(y_true, y_pred, sample_weight=[1,2,3,4,5,6])
print("Weighted Average Accuracy:", weighted_avg_accuracy)

weighted_avg_f1 = f1_score(y_true, y_pred, average='weighted')
print("Weighted Average F1 Score:", weighted_avg_f1)

**3. Macro Average**

Macro Average(宏平均)是指对每个类别进行评估,然后计算准确率或F1分数。它考虑了类别之间的差异。

公式:Macro Average = (TP1 / N + TN1 / N + FP1 / N + FN1 / N + ... + TPN / N + TNN / N + FPN / N + FNN / N)

其中:

* TPi、TNi、FPi、FNi:第i类的正确预测为正类的样本数量、正确预测为负类的样本数量、错误预测为正类的样本数量和错误预测为负类的样本数量* N:类别的数量**示例代码**
from sklearn.metrics import accuracy_score, f1_scorey_true = [0,1,1,0,1,0]
y_pred = [0,1,0,0,1,1]

macro_avg_accuracy = accuracy_score(y_true, y_pred)
print("Macro Average Accuracy:", macro_avg_accuracy)

macro_avg_f1 = f1_score(y_true, y_pred, average='macro')
print("Macro Average F1 Score:", macro_avg_f1)

综上所述,Micro Average、Weighted Average和Macro Average是三种常见的评价指标,它们分别考虑了样本之间的差异、类别之间的差异和类别之间的均衡。选择合适的评价指标有助于评估模型在不同方面的表现。

相关标签:
其他信息

其他资源

Top