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=Noneto get the support values. - Ensure your
y_trueandy_predare properly formatted (1D arrays with consistent label values) to avoid any edge cases that might affect the output.
内容的提问来源于stack exchange,提问作者user9399405
相关产品推荐
相关产品推荐

