Prior shift¶
Evaluating a measure under a different class prior.
A classifier’s conditional rates, P(predicted k | true j), are what a test set
measures. The measures that are not skew-invariant (Definition 11 and Theorem 5 of
the companion paper; Proposition 6 for one-vs-rest rates) also depend on how often
each class occurs, so the same classifier scores differently where the prevalence
differs. under_prior answers “what would this measure be if the classes occurred
with these frequencies?” by keeping the rates of the data and replacing the class
sizes.
The assumption is the usual one for a change of prior (label shift): the rates of each class do not change, only how many examples it has. It holds when the class-conditional distribution of the inputs is the same in both settings. It does not hold when the shift also changes what the examples look like.
- class aggmetrics.shift.PriorShifted(model, priors, task)¶
Bases:
objectThe data of an
IndicatorMatrixat another prior.Built by
under_prior(). It offers the parts of theIndicatorMatrixinterface that depend only on the confusion matrix:compute,accuracy,countsandper_class.- Parameters:
model (Rates)
task (str)
- property counts¶
Pooled
(tp, fp, fn, tn).
- compute(measure, aggregation='micro', *, convention='exclude')¶
Value of
measureat the new prior, under the operatoraggregation.- Parameters:
measure (str | BinaryMeasure)
aggregation (str)
convention (str)
- Return type:
- aggmetrics.shift.under_prior(im, priors)¶
Re-evaluate
imas if its classes occurred with the frequenciespriors.- Parameters:
im – A binary, multiclass or multilabel
IndicatorMatrixwithout a mask or a t-norm.priors – Binary: the prevalence of the positive class, a number in [0, 1]. Multiclass: the frequency of each class, non-negative and summing to 1. Multilabel: the prevalence of each label, one number in [0, 1] per label.
- Returns:
An object with
compute,accuracy,countsandper_class. At the prior of the data itself it reproducesimexactly.- Return type:
Examples
>>> import numpy as np >>> from aggmetrics import IndicatorMatrix, under_prior >>> y = np.array([1] * 80 + [0] * 120) >>> p = np.array([1] * 60 + [0] * 20 + [1] * 30 + [0] * 90) >>> im = IndicatorMatrix(y, p, task="binary") >>> round(im.compute("precision"), 3) 0.667 >>> shifted = under_prior(im, 0.05) # 5% positives instead of 40% >>> round(shifted.compute("precision"), 3) 0.136 >>> round(shifted.compute("recall"), 3) == round(im.compute("recall"), 3) True