Information-theoretic evaluation metrics.
Functions
Calculate entropy of a label distribution, the average information content (uncertainty) of the labels.
Parameters
labels
base
Returns
value
base is None, or in bits when base=2. Non-negative; 0 when all labels are identical, maximal when labels are uniformly distributed.
>>> from tuiml.evaluation.metrics import entropy
>>> round(entropy([0, 0, 1, 1]), 4) # ln(2) nats
0.6931
>>> entropy([0, 0, 1, 1], base=2) # 1 bit
1.0
Calculate conditional entropy H(Y|X), the remaining uncertainty in Y once X is known.
Parameters
y_true
y_pred
base
Returns
value
base. Ranges from 0 (X fully determines Y) up to H(Y) (X and Y are independent).
>>> from tuiml.evaluation.metrics import conditional_entropy
>>> round(conditional_entropy([0, 0, 1, 1], [0, 0, 1, 1], base=2), 4) # X determines Y
0.0
>>> round(conditional_entropy([0, 0, 1, 1], [0, 1, 0, 1], base=2), 4) # X independent of Y
1.0
Calculate mutual information I(Y;X) = H(Y) - H(Y|X), the reduction in uncertainty about Y from observing X.
Parameters
y_true
y_pred
base
Returns
value
base. Non-negative; 0 when X and Y are independent, and equal to H(Y) when X fully determines Y.
>>> from tuiml.evaluation.metrics import mutual_information
>>> round(mutual_information([0, 0, 1, 1], [0, 0, 1, 1], base=2), 4) # X determines Y
1.0
>>> round(mutual_information([0, 0, 1, 1], [0, 1, 0, 1], base=2), 4) # X independent of Y
0.0
Calculate information gain, numerically identical to mutual information.
Parameters
y_true
y_pred
base
Returns
value
base=2. Non-negative; 0 means the split carries no information about y_true.
>>> from tuiml.evaluation.metrics import information_gain
>>> round(information_gain([0, 0, 1, 1], [0, 0, 1, 1]), 4) # split matches labels exactly
1.0
Calculate gain ratio, information gain normalized by split information.
Introduced by Quinlan for C4.5 to counteract information gain's bias toward high-cardinality splits, and defined as
which corrects information gain's bias toward splits with many values.
Parameters
y_true
y_pred
base
Returns
value
>>> from tuiml.evaluation.metrics import gain_ratio
>>> round(gain_ratio([0, 0, 1, 1], [0, 0, 1, 1]), 4) # split matches labels exactly
1.0
Calculate Kullback-Leibler divergence KL(P||Q), the expected extra information needed to encode samples from P using a code optimized for Q instead of P.
Parameters
y_true_proba
y_pred_proba
log(0).
base
Returns
divergence
>>> from tuiml.evaluation.metrics import kullback_leibler_divergence
>>> round(kullback_leibler_divergence([0.5, 0.5], [0.4, 0.6]), 4)
0.0204
Calculate Jensen-Shannon divergence, a smoothed and symmetric version of KL divergence.
Unlike KL divergence, JSD is symmetric in P and Q, always finite, and its square root is a proper metric.
Parameters
p
q
base
Returns
value
>>> from tuiml.evaluation.metrics import jensen_shannon_divergence
>>> round(jensen_shannon_divergence([0.5, 0.5], [0.5, 0.5]), 4) # identical distributions
0.0
>>> round(jensen_shannon_divergence([1.0, 0.0], [0.0, 1.0], base=2), 4) # disjoint, base 2
1.0
Calculate cross-entropy H(P,Q) = H(P) + KL(P||Q), the average number of units needed to identify an event drawn from P when using a code optimized for Q.
Parameters
y_true_proba
y_pred_proba
log(0).
base
Returns
value
base. Always >= H(P), with equality only when Q equals P.
>>> from tuiml.evaluation.metrics import cross_entropy
>>> round(cross_entropy([0.5, 0.5], [0.5, 0.5]), 4) # Q equals P: reduces to H(P)
0.6931
>>> round(cross_entropy([1.0, 0.0], [0.9, 0.1], base=2), 4)
0.152
Calculate symmetrical uncertainty, a normalized, symmetric variant of mutual information.
Normalizing by H(X) + H(Y) compensates for mutual information's bias toward variables with more distinct values.
Parameters
y_true
y_pred
base
Returns
value
>>> from tuiml.evaluation.metrics import symmetrical_uncertainty
>>> round(symmetrical_uncertainty([0, 0, 1, 1], [0, 0, 1, 1]), 4) # X determines Y
1.0
>>> round(symmetrical_uncertainty([0, 0, 1, 1], [0, 1, 0, 1]), 4) # X independent of Y
0.0
Calculate prior entropy, the entropy of the class distribution before any model prediction is taken into account.
entropy that defaults to base 2.Parameters
y_true
base
Returns
value
base=2. Non-negative; 0 when all labels are identical.
>>> from tuiml.evaluation.metrics import prior_entropy
>>> round(prior_entropy([0, 0, 1, 1]), 4) # balanced two-class labels: 1 bit
1.0
Calculate prediction entropy, the average entropy of the model's predicted class distributions.
Parameters
y_pred_proba
base
Returns
value
base=2. Non-negative; 0 when every prediction is fully confident (a one-hot distribution).
>>> from tuiml.evaluation.metrics import prediction_entropy
>>> round(prediction_entropy([[0.9, 0.1], [0.5, 0.5]]), 4) # confident + maximally uncertain
0.7345
Calculate entropy gain, the reduction in entropy from using the model's predictions instead of the prior class distribution.
prior_entropy minus prediction_entropy.Parameters
y_true
y_pred_proba
base
Returns
value
base=2. Positive when the model's predictions are more confident (lower entropy) than the prior; can be negative if the model is less confident than the prior.
>>> from tuiml.evaluation.metrics import entropy_gain
>>> y_true = [0, 0, 1, 1]
>>> y_pred_proba = [[0.9, 0.1], [0.9, 0.1], [0.1, 0.9], [0.1, 0.9]]
>>> round(entropy_gain(y_true, y_pred_proba), 4) # confident, correct predictions
0.531
Calculate KB (Kononenko-Bratko) information, the average per-instance information gained by using the model's predicted probability for the true class instead of the prior probability of that class.
References
Parameters
y_true
y_pred_proba's columns and to estimate the prior class distribution.
y_pred_proba
log(0).
base
Returns
value
base=2. Positive when predictions assign more probability to the true class than the prior does; negative when predictions are worse than the prior.
>>> from tuiml.evaluation.metrics import kb_information
>>> y_true = [0, 0, 1, 1]
>>> y_pred_proba = [[0.9, 0.1], [0.9, 0.1], [0.1, 0.9], [0.1, 0.9]]
>>> round(kb_information(y_true, y_pred_proba), 4) # confident, correct predictions
0.848