场景:

数组recalls和precisions,计算 使用numpy求f1_score最大值 - 图1,

数组对应元素相乘

  1. recalls = np.array([0, 0, 0, 0.1, 0.1, 0.1, 0.5, 0.3, 0.3])
  2. precisions = np.array([0, 0, 0, 1, 0.3, 0.3, 0.3, 0.3, 0.6])
  3. f1_scores = 2*recalls*precisions/(recalls+precisions)

image.png

特定元素赋值

给np数组中为nan的数值赋值为零

  1. f1_scores[np.isnan(f1_scores)] = 0

错误写法

  1. f1_scores[f1_scores==np.nan] = 0

求numpy数组中最大元素索引

  1. max_index = np.argmax(f1_scores)