ML Foundations
Compare macro and weighted F1 on imbalanced classes
Calculate per-class F1 before comparing equal-class and support-weighted averages.

Evidence and testing
Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.
The average defines whose errors count
For multiclass reporting, macro F1 is the unweighted mean of per-class F1 scores. Weighted F1 averages the same per-class scores using each class support, the number of true instances, as weights. The values can differ substantially when class supports are uneven. [1, 2]
A single summary does not replace the per-class scores. A majority class can dominate a support-weighted average even while the model misses every instance of a smaller class. The importance of that failure depends on the application, so report the supports and class-level results alongside the chosen summary.
Work through a small example
Run python examples/macro_weighted_f1.py. The fixture has nine examples of class zero and one of class one; every prediction is class zero. For each class, calculate F1 as 2TP divided by 2TP plus FP plus FN. Class zero scores 18/19 and class one scores zero. [1]
The macro average gives both classes equal weight, producing approximately 0.4737. The support-weighted average assigns nine times as much weight to class zero, producing approximately 0.8526. Assertions check the exact fractions using numerical tolerance; printed scores are rounded. [1, 2]
The example calculates the formulas directly with NumPy for transparency. It does not execute the scikit-learn API; the documentation is used for the metric definitions and averaging conventions.
Executed with Python 3.12.14 and local NumPy; stdout and checksum are in private evidence.
import numpy as np
truth = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 1])
predicted = np.zeros(10, dtype=int)
classes = [0, 1]
f1 = []
support = []
for label in classes:
tp = np.sum((truth == label) & (predicted == label))
fp = np.sum((truth != label) & (predicted == label))
fn = np.sum((truth == label) & (predicted != label))
f1.append(float(2 * tp / (2 * tp + fp + fn)))
support.append(int(np.sum(truth == label)))
macro = sum(f1) / len(f1)
weighted = sum(score * count for score, count in zip(f1, support)) / sum(support)
assert np.allclose(f1, [18 / 19, 0])
assert np.isclose(macro, 9 / 19)
assert np.isclose(weighted, 162 / 190)
print("support:", support)
print("per-class F1:", [round(value, 4) for value in f1])
print("macro F1:", round(macro, 4))
print("weighted F1:", round(weighted, 4))
Recorded output
support: [9, 1] per-class F1: [0.9474, 0.0] macro F1: 0.4737 weighted F1: 0.8526
Fix the label set and undefined-score policy
Choose the labels included in a report and state how undefined per-class scores are handled. In scikit-learn, the labels, average and zero_division parameters influence these cases; a class absent from both truth and predictions requires a deliberate convention. [1, 2]
This fixture is a small single-label classification example with nonzero denominators for both classes. It does not evaluate probabilities, threshold selection, statistical uncertainty or model performance on real data.