You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

sklearn中precision_recall_fscore_support返回support为None的原因及解决方法

Why support is Returning None in precision_recall_fscore_support & How to Get Valid Values

The Main Cause

The support parameter returning None almost always boils down to your choice of the average argument in the function call.

When you use an averaging strategy like 'micro', 'macro', 'weighted', or 'samples', the function computes aggregated precision, recall, and F1-score values (single scalars instead of per-class arrays). Since support is defined as the count of true samples for each individual class, it doesn't have a meaningful aggregated value—so the function returns None for this output.

Fix: Get Valid Support Values

To retrieve actual support numbers, adjust the average parameter:

  • Set average=None. This tells the function to return metrics for each class separately. You'll get arrays for precision, recall, F1-score, and support, where each element maps to a class in your label set.

Example Implementation

from sklearn.metrics import precision_recall_fscore_support
import numpy as np

# Sample true labels and predictions
y_true = np.array([0, 1, 0, 1, 2, 2])
y_pred = np.array([0, 1, 1, 1, 2, 0])

# Fetch per-class metrics including support
precision, recall, fscore, support = precision_recall_fscore_support(y_true, y_pred, average=None)

print("Support counts per class:", support)
# Output: Support counts per class: [2 2 2] (each class has 2 true samples)

Extra Tips

  • If you need both aggregated metrics and per-class support, call the function twice: once with your preferred averaging method for the aggregated scores, and once with average=None to get the support values.
  • Ensure your y_true and y_pred are properly formatted (1D arrays with consistent label values) to avoid any edge cases that might affect the output.

内容的提问来源于stack exchange,提问作者user9399405

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:14:07