polars.Series.entropy#

Series.entropy(
base: float = 2.718281828459045,
*,
normalize: bool = True,
) → float | None[source]#

Computes the entropy.

Uses the formula -sum(pk * log(pk)) where pk are discrete probabilities. 0 * log(0) is taken as 0 and null values are ignored. Negative values give NaN. With normalize=True, input that sums to zero (including empty, all-null and all-zero input) gives NaN; with normalize=False it gives 0.

Parameters:
base

Given base, defaults to e

normalize

Normalize pk if it doesn’t sum to 1.

Examples

>>> a = pl.Series([0.99, 0.005, 0.005])
>>> a.entropy(normalize=True)
0.06293300616044681
>>> b = pl.Series([0.65, 0.10, 0.25])
>>> b.entropy(normalize=True)
0.8568409950394724