File size: 11,796 Bytes
0974646
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Balanced Accuracy Metrics for 🤗 Evaluate

A minimal, production-ready set of **balanced accuracy** metrics for imbalanced vision/NLP tasks, implemented as plain Python scripts that you can load with `evaluate` from a **dataset-type** repo on the Hugging Face Hub.

> **What this is**
>
> Three drop‑in metrics that focus on fair evaluation under class imbalance:
>
> - `balanced_accuracy.py` — **binary & multiclass** balanced accuracy with options for `sample_weight`, `threshold="auto"` (Youden’s J), `ignore_index`, `adjusted`, `class_mask`, `return_per_class`, and `support_per_class`.
> - `balanced_accuracy_multilabel.py` — **multilabel** balanced accuracy with `average={"macro","weighted","micro"}`, `threshold="auto"` (per label), `sample_weight`, `class_mask`, `ignore_index`, and `support_per_label`.
> - `balanced_topk_accuracy.py` — **balanced top‑k accuracy** (macro top‑k recall across classes) with `sample_weight`, multiple `k` values, and class masking.
>
> **Why it’s useful**
>
> - Works without packaging: just download the script and load via `evaluate`.
> - Designed for long‑tail / imbalanced setups; supports masking, weighting, and chance‑adjustment.
> - Clear error messages and `reason` fields for edge cases.

---

## Requirements & Installation

Install the minimal dependencies (Python ≥3.9 recommended):

```bash
pip install --upgrade pip
pip install evaluate datasets huggingface_hub numpy
```

> **Windows note**: You may see a symlink warning from `huggingface_hub`. It only affects caching and can be ignored. To silence it, set `HF_HUB_DISABLE_SYMLINKS_WARNING=1` or enable Windows Developer Mode.

---

## Repository Layout

This project is intentionally lightweight—each metric is a single Python file living in a dataset‑type Hub repo:

```
balanced_accuracy.py
balanced_accuracy_multilabel.py
balanced_topk_accuracy.py
README.md
```

All three metrics are loadable from the Hub via `hf_hub_download(...)` + `evaluate.load(local_path, module_type="metric")` — no installation step required.

---

## Quickstart

### 0) Common helper

```python
from huggingface_hub import hf_hub_download
import evaluate

REPO = "OliverOnHF/balanced-accuracy"   # dataset-type repo
REV  = "main"                           # or a specific commit hash for reproducibility

def load_metric_from_hf(filename):
    path = hf_hub_download(REPO, filename, repo_type="dataset", revision=REV)
    return evaluate.load(path, module_type="metric")
```

### 1) Binary & Multiclass — `balanced_accuracy.py`

```python
m = load_metric_from_hf("balanced_accuracy.py")

# Binary (labels)
print(m.compute(references=[0,1,1,0], predictions=[0,1,0,0], task="binary"))
# → {'balanced_accuracy': 0.75}

# Binary (probabilities) + automatic threshold search (Youden’s J)
print(m.compute(references=[0,1,1,0],
                predictions=[0.2, 0.9, 0.1, 0.3],
                task="binary", threshold="auto"))
# → {'balanced_accuracy': 0.75, 'optimal_threshold': 0.6}

# Multiclass (macro BA) with per-class recall & sample_weight
print(m.compute(references=[0,1,2,1],
                predictions=[0,2,2,1],
                task="multiclass", num_classes=3,
                return_per_class=True,
                sample_weight=[1, 0.5, 1, 1]))
# → {'balanced_accuracy': 0.888888..., 'per_class_recall': [1.0, 0.6666..., 1.0], 'support_per_class': [1.0, 1.5, 1.0]}

# Class masking (e.g., tail classes only)
print(m.compute(references=[0,1,2,1], predictions=[0,2,2,1],
                task="multiclass", num_classes=3,
                class_mask=[1,2], return_per_class=True))
```

**Key arguments**

- `task`: `"binary"` or `"multiclass"` (default `"binary"`)
- `threshold`: float in (0,1) or `"auto"` (binary probabilities only). If predictions are 0/1 labels, threshold is ignored.
- `num_classes`: for multiclass; inferred if not set (when labels are 0..K‑1).
- `sample_weight`: per‑sample weights; confusion **counts become weighted sums**.
- `ignore_index`: skip samples where `reference == ignore_index`.
- `adjusted`: chance‑corrected BA (`2*BA-1` for binary; `(BA-1/K)/(1-1/K)` for multiclass).
- `class_mask`: compute macro‑BA over a subset of classes.
- `return_per_class`: also return per‑class recalls; `support_per_class` is **count or weighted sum** (if `sample_weight` is provided).

---

### 2) Multilabel — `balanced_accuracy_multilabel.py`

```python
m = load_metric_from_hf("balanced_accuracy_multilabel.py")

y_true = [[1,0,1],
          [0,1,0]]
y_pred = [[1,0,0],
          [0,1,1]]

# Labels (0/1)
print(m.compute(references=y_true, predictions=y_pred, return_per_label=True))
# → {'balanced_accuracy': 0.6666..., 'per_label_ba': [1.0, 1.0, 0.0], 'support_per_label': [1, 1, 1]}

# Probabilities + per-label automatic threshold
probs = [[0.9,0.2,0.1],
         [0.1,0.8,0.7]]
print(m.compute(references=y_true, predictions=probs,
                from_probas=True, threshold="auto"))
# → {'balanced_accuracy': 0.8333..., 'per_label_thresholds': [0.5, 0.5, ~0.7]}

# Weighted / micro / class_mask
print(m.compute(references=y_true, predictions=y_pred,
                average="micro",
                sample_weight=[1.0, 0.5],
                class_mask=[0,2]))
```

**Key arguments**

- `from_probas`: if `True`, `predictions` are probabilities in `[0,1]`; else must be 0/1 labels.
- `threshold`: float in (0,1) or `"auto"` (when `from_probas=True`; `"auto"` selects a threshold per label).
- `average`: `"macro" | "weighted" | "micro"`  
  - *macro*: average BA across labels;  
  - *weighted*: weighted by each label’s **positive support**;  
  - *micro*: pool TP/TN/FP/FN across all labels then compute BA.
- `class_mask`: evaluate only the specified label indices.
- `return_per_label`: additionally return `per_label_ba` and `support_per_label`.

---

### 3) Balanced Top‑K Accuracy — `balanced_topk_accuracy.py`

```python
import numpy as np
m = load_metric_from_hf("balanced_topk_accuracy.py")

scores = np.array([[0.7,  0.2, 0.1],
                   [0.1,  0.3, 0.6],
                   [0.05, 0.05,0.9],
                   [0.05, 0.9, 0.05]])
y_true = [0,1,2,1]

# top-1 (macro recall across classes)
print(m.compute(references=y_true, predictions=scores, k=1, return_per_class=True))
# → {'balanced_topk_accuracy': 0.8333..., 'per_class_recall': [1.0, 0.5, 1.0]}

# multiple k at once
print(m.compute(references=y_true, predictions=scores, k_list=[1,2], return_per_class=True))
# → {'balanced_topk_accuracy': {1: 0.8333..., 2: 1.0}, 'per_class_recall': {1: [...], 2: [...]}}

# with sample_weight and class_mask
print(m.compute(references=y_true, predictions=scores, k=1,
                sample_weight=[1,0.5,1,1], class_mask=[0,1,2]))
```

**Intuition**: For each class `c`, compute recall@k among samples of class `c`, then macro‑average across classes (optionally over a masked subset).

---

## Expected Outputs (Sanity Check)

These should match what you get locally:

```
# Binary BA
{'balanced_accuracy': 0.75}

# Binary BA with auto threshold (probs: [0.2, 0.9, 0.1, 0.3])
{'balanced_accuracy': 0.75, 'optimal_threshold': 0.6}

# Multiclass BA with weights
{'balanced_accuracy': 0.888888..., 'per_class_recall': [1.0, 0.6666..., 1.0], 'support_per_class': [1.0, 1.5, 1.0]}

# Multilabel BA (labels)
{'balanced_accuracy': 0.6666..., 'per_label_ba': [1.0, 1.0, 0.0], 'support_per_label': [1, 1, 1]}

# Multilabel BA (probs + auto thresholds)
{'balanced_accuracy': 0.8333..., 'per_label_thresholds': [0.5, 0.5, ~0.7]}

# Balanced top-1 and top-2
{'balanced_topk_accuracy': 0.8333..., 'per_class_recall': [1.0, 0.5, 1.0]}
{'balanced_topk_accuracy': {1: 0.8333..., 2: 1.0}, 'per_class_recall': {1: [...], 2: [...]}}
```

---

## API Reference (TL;DR)

### `balanced_accuracy.py` (binary/multiclass)

- **Args**:  
  `predictions`, `references`, `task={"binary","multiclass"}`, `num_classes=None`,  
  `adjusted=False`, `zero_division=0.0`, `threshold=None|"auto" (binary prob)`,  
  `ignore_index=None`, `return_per_class=False`, `class_mask=None`, `sample_weight=None`  
- **Returns**:  
  `{"balanced_accuracy": float}` + optional `{"optimal_threshold": float}` (binary, auto) +  
  optional `{"per_class_recall": list[float], "support_per_class": list[int|float]}` (multiclass).

### `balanced_accuracy_multilabel.py`

- **Args**:  
  `predictions`, `references`, `from_probas=False`, `threshold=0.5|"auto"`,  
  `zero_division=0.0`, `average="macro"|"weighted"|"micro"`, `class_mask=None`,  
  `ignore_index=None`, `return_per_label=False`, `sample_weight=None`  
- **Returns**:  
  `{"balanced_accuracy": float}` + optional `{"per_label_thresholds": list[float]}` (auto) +  
  optional `{"per_label_ba": list[float], "support_per_label": list[int]}`.

### `balanced_topk_accuracy.py`

- **Args**:  
  `predictions (N,K)`, `references (N)`, `k=1` or `k_list=[...]`, `class_mask=None`,  
  `sample_weight=None`, `zero_division=0.0`, `return_per_class=False`  
- **Returns**:  
  `{"balanced_topk_accuracy": float | dict[int,float]}` + optional `{"per_class_recall": ...}`.

---

## Error Messages & Special Reasons

Friendly messages you may encounter by design:

- **Length/shape**: “Mismatch in the number of predictions …” / “Multilabel expects 2D arrays …”  
- **NaN/Inf**: “`predictions` contains NaN/Inf.”  
- **Binary**:  
  - labels not in {0,1} → “For binary with label predictions, values must be 0/1.”  
  - probs not in [0,1] → “For binary with probabilities, `predictions` must be in [0,1].”  
- **Multiclass**: label out of range → “`predictions`/`references` must be in [0,K‑1] …”  
- **Multilabel**: average invalid / prob or label value invalid / shape mismatch  
- **Top‑k**: invalid `k` / label out of range  
- **Reasoned NaN**:
  - `{"reason": "empty_after_ignore_index"}` — all samples were ignored  
  - `{"reason": "empty_class_mask_after_filtering"}` — class/label mask removed everything

---

## Reproducible Smoke Test

Copy into `test_all.py` and run:

```python
from huggingface_hub import hf_hub_download
import evaluate, numpy as np

REPO, REV = "OliverOnHF/balanced-accuracy", "main"
def load(fname): return evaluate.load(hf_hub_download(REPO, fname, repo_type="dataset", revision=REV), module_type="metric")

# 1) binary & multiclass
mba = load("balanced_accuracy.py")
print(mba.compute(references=[0,1,1,0], predictions=[0,1,0,0], task="binary"))
print(mba.compute(references=[0,1,1,0], predictions=[0.2,0.9,0.1,0.3], task="binary", threshold="auto"))
print(mba.compute(references=[0,1,2,1], predictions=[0,2,2,1], task="multiclass", num_classes=3, return_per_class=True, sample_weight=[1,0.5,1,1]))

# 2) multilabel
mml = load("balanced_accuracy_multilabel.py")
y_true = [[1,0,1],[0,1,0]]; y_pred = [[1,0,0],[0,1,1]]; probs = [[0.9,0.2,0.1],[0.1,0.8,0.7]]
print(mml.compute(references=y_true, predictions=y_pred, return_per_label=True))
print(mml.compute(references=y_true, predictions=probs, from_probas=True, threshold="auto"))

# 3) top-k
mtk = load("balanced_topk_accuracy.py")
scores = np.array([[0.7,0.2,0.1],[0.1,0.3,0.6],[0.05,0.05,0.9],[0.05,0.9,0.05]]); y_true = [0,1,2,1]
print(mtk.compute(references=y_true, predictions=scores, k=1, return_per_class=True))
print(mtk.compute(references=y_true, predictions=scores, k_list=[1,2], return_per_class=True))
```

---

## Tips

- Pin `revision` to a commit hash for exact reproducibility.  
- `support_per_class` / `support_per_label` are **counts** when unweighted; if `sample_weight` is provided they become **effective weight sums** (floats).  
- For extreme long‑tail distributions, combine `class_mask` with per‑class analysis for stable reporting.

---

## License

MIT (suggested). If you need a specific license, add a root `LICENSE` file.