polars.Expr.entropy#

Expr.entropy(
base: float = 2.718281828459045,
*,
normalize: bool = True,
) → Expr[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.

engine:In MemoryStreaming
Parameters:
base

Given base, defaults to e

normalize

Normalize pk if it doesn’t sum to 1.

Examples

>>> df = pl.DataFrame({"a": [1, 2, 3]})
>>> df.select(pl.col("a").entropy(base=2))
shape: (1, 1)
┌──────────┐
│ a        │
│ ---      │
│ f64      │
╞══════════╡
│ 1.459148 │
└──────────┘
>>> df.select(pl.col("a").entropy(base=2, normalize=False))
shape: (1, 1)
┌───────────┐
│ a         │
│ ---       │
│ f64       │
╞═══════════╡
│ -6.754888 │
└───────────┘