Class-balance weights

q.label.class_balance_weights assigns inverse-frequency factors so every observed class receives equal total weight. The resulting factors have unit mean.

Use class balancing when the training objective should not be dominated by the majority target. It changes the loss contribution, not the labels themselves.

import pandas as pd

import qrt as q

events = pd.date_range("2026-01-01", periods=10, name="event_time")
labels = pd.Series([0, 0, 0, 0, 0, 0, 1, 1, -1, -1], index=events, name="label")
weights = q.label.class_balance_weights(labels)
pd.DataFrame({"label": labels, "class_balance_weight": weights})
label class_balance_weight
event_time
2026-01-01 0 0.555556
2026-01-02 0 0.555556
2026-01-03 0 0.555556
2026-01-04 0 0.555556
2026-01-05 0 0.555556
2026-01-06 0 0.555556
2026-01-07 1 1.666667
2026-01-08 1 1.666667
2026-01-09 -1 1.666667
2026-01-10 -1 1.666667
weights.groupby(labels).agg(["count", "sum"]).rename_axis("label")
count sum
label
-1 2 3.333333
0 6 3.333333
1 2 3.333333
Back to top