【问题标题】:Computing Macro average F1 score using numpy python(Without using scikit learn) [closed]使用 numpy python 计算宏平均 F1 分数(不使用 scikit learn)[关闭]
【发布时间】:2020-11-16 14:43:42
【问题描述】:

我想单独使用 numpy 计算宏观平均 f1 分数? 例如:

Actual = np.array(["A","A","B","C","C"])
predicted = np.array[("A","A","B","C","C")]

【问题讨论】:

标签: python numpy data-science


【解决方案1】:

除非您想了解更多信息,或者您由于某种原因无法访问 sklearn,否则我不建议您这样做。 sklearn 提供了一个已经受信任(并且更加优化)的代码。

您可以通过以下方式实现自己的版本:

def f1(actual, predicted, label):

    """ A helper function to calculate f1-score for the given `label` """

    # F1 = 2 * (precision * recall) / (precision + recall)
    tp = np.sum((actual==label) & (predicted==label))
    fp = np.sum((actual!=label) & (predicted==label))
    fn = np.sum((predicted!=label) & (actual==label))
    
    precision = tp/(tp+fp)
    recall = tp/(tp+fn)
    f1 = 2 * (precision * recall) / (precision + recall)
    return f1

def f1_macro(actual, predicted):
    # `macro` f1- unweighted mean of f1 per label
    return np.mean([f1(actual, predicted, label) 
        for label in np.unique(actual)])

那么,你可以计算"macro-f1"如下:

f1_macro(actual, predicted) #outputs 1.0

您可以使用sklearn.metrics.f1_score(actual, predicted, average='macro') 测试您的实现。

【讨论】:

  • 这看起来很整洁
【解决方案2】:
import numpy as np
def f1(y_true, y_pred):
  TP = np.sum(np.multiply([i==True for i in y_pred], y_true))
  TN = np.sum(np.multiply([i==False for i in y_pred], [not(j) for j in y_true]))
  FP = np.sum(np.multiply([i==True for i in y_pred], [not(j) for j in y_true]))
  FN = np.sum(np.multiply([i==False for i in y_pred], y_true))
  precision = TP/(TP+FP)
  recall = TP/(TP+FN)
  if precision != 0 and recall != 0:
    f1 = (2 * precision * recall) / (precision + recall)
  else:
    f1 = 0
  return f1

def f1_macro(y_true, y_pred):
  macro = []
  for i in np.unique(y_true):
    modified_true = [i==j for j in y_true]
    modified_pred = [i==j for j in y_pred]
    score = f1(modified_true, modified_pred)
    macro.append(score)
  return np.mean(macro)

测试用例

y_true = [0, 1, 2, 0, 1, 2]
y_pred = [0, 2, 1, 0, 0, 1]
s = f1_macro(y_true, y_pred)
print(f"Macro F1 score: {s}")
>>> Macro F1 score: 0.26666666666666666

y_true = np.array(["A","A","B","C","C"])
y_pred = np.array(["A","A","B","C","C"])
s = f1_macro(y_true, y_pred)
print(f"Macro F1 score: {s}")
>>> Macro F1 score: 1.0

【讨论】:

    猜你喜欢
    • 2017-03-20
    • 2017-09-11
    • 1970-01-01
    • 2015-10-19
    • 2016-01-24
    • 2016-01-24
    • 2019-04-23
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多