Method: Polars::Expr#entropy

Defined in:
lib/polars/expr.rb

#entropy(base: nil, normalize: true) ⇒ Expr

Computes the entropy.

Uses the formula -sum(pk * log(pk) where pk are discrete probabilities.

Examples:

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

Parameters:

  • base (Float) (defaults to: nil)

    Given base, defaults to 2.

  • normalize (Boolean) (defaults to: true)

    Normalize pk if it doesn't sum to 1.

Returns:



7639
7640
7641
7642
7643
7644
7645
7646
7647
# File 'lib/polars/expr.rb', line 7639

def entropy(base: nil, normalize: true)
  # TODO update (including param docs)
  if base.nil?
    warn "The default `base` for `entropy` method will change from `2` to `Math::E` in a future version"
    base = 2
  end

  wrap_expr(_rbexpr.entropy(base, normalize))
end