DataLane

29. Shannon entropy

hardProNot started~12 minPythonImplement entropy()

Asked in screens shaped like: DeepMind, Google

Shannon entropy of a discrete distribution p (nats, natural log): -sum p_i log(p_i). Skip zeros. Round to 4 decimals.

Examples

  • Example 1

    Input: { "p": [0.5, 0.5] }
    Output: 0.6931

Constraints

  • p sums to 1 and is non-negative

Python editor

Loading Python engine…

Ctrl/Cmd + Enter submits

Run your code to see stdout.

Pro problem

Hard problems unlock with Pro

Easy pads stay free. Medium and hard SQL and Python problems — editor, tests, hints, and solutions — open after you upgrade to Pro or coaching.

See plansPractice free easy problems
↑↓ navigate openesc close