With more than two classes, "the F1 score" is ambiguous. You can pool every decision together and compute one F1 (micro), which weights each row equally and so favors large classes. Or you can compute F1 for each class and average those (macro), which weights each class equally and so exposes a model that ignores a rare class.
Implement two functions for single-label multiclass predictions.
f1_micro(y_true, y_pred): pool TP, FP, and FN across all classes, then return 2·TP / (2·TP + FP + FN).f1_macro(y_true, y_pred): for each label in the union of y_true and y_pred, compute its own F1 as 2·TP / (2·TP + FP + FN), using 0.0 when the denominator is zero, then return the mean.ValueError.Open one at a time. Each gives away a little more than the last.
For single-label data, micro F1 reduces to a count of correct predictions over total rows. Check your micro result against that.
Loop over np.union1d(y_true, y_pred) for the macro version. Count TP, FP, and FN for each label with masks.
No results yet
Run your code to check it against the examples, or submit to run every test.