Home

classweighted

Classweighted is a technique in supervised learning used to address class imbalance by assigning different importance to classes during model training. By giving higher weight to underrepresented classes, the method aims to improve the model's performance on those classes without requiring data resampling.

Weights are typically derived from class frequencies. Common schemes include w_c = N / (K * n_c) and w_c

Implementation details vary by framework. In neural networks, a per-class weight vector is passed to loss functions

Considerations and limitations include the fact that weighting alters the optimization objective and can improve minority-class

See also: class imbalance, resampling, focal loss, sample_weight.

=
1
/
n_c,
where
N
is
the
total
number
of
samples,
K
is
the
number
of
classes,
and
n_c
is
the
count
of
samples
in
class
c.
Some
libraries
offer
a
“balanced”
option
that
computes
these
weights
automatically.
In
multi-class
problems
the
weight
for
each
class
is
independent
and
is
used
to
scale
the
loss
contributed
by
samples
of
that
class.
such
as
cross-entropy,
for
example
in
PyTorch
via
CrossEntropyLoss(weight=...),
which
multiplies
the
loss
for
each
sample
by
the
weight
of
its
true
class.
In
scikit-learn,
class
weights
can
be
provided
to
estimators
or
computed
with
compute_class_weight.
In
Keras
and
TensorFlow,
class_weight
is
supplied
to
fit(),
or
sample_weight
can
be
used
for
finer
control.
recall
at
the
expense
of
overall
accuracy.
Overemphasis
on
rare
classes
can
lead
to
overfitting.
Proper
evaluation
with
metrics
that
reflect
class
balance
(precision,
recall,
F1-score,
balanced
accuracy)
is
recommended.
Alternatives
or
complements
include
data
resampling
(oversampling
minority
or
undersampling
majority)
and
loss
variants
such
as
focal
loss.