Shannon entropyEasy

Shannon entropy

Background

Shannon entropy measures the average uncertainty (in bits) of a discrete distribution. A fair coin has 1 bit of entropy; a certain outcome has 0. Entropy is the foundation of information theory and shows up everywhere in ML — it is the impurity criterion for decision-tree splits and the basis of cross-entropy loss.

Problem statement

Implement shannon_entropy(p) for a discrete probability distribution:

H(p)=i:pi>0pilog2piH(p) = -\sum_{i:\,p_i>0} p_i \log_2 p_i

Skip zero-probability terms (treat 0log0=00\log 0 = 0). The result is in bits.

Input

  • pnp.ndarray (K,), a probability distribution (non-negative, sums to 1).

Output

A float: the entropy in bits.

Examples

Example 1

Input:  p = [0.5, 0.5]
Output: 1.0

Explanation: a fair binary choice needs exactly one bit: (0.5log20.5+0.5log20.5)=1-(0.5\log_2 0.5 + 0.5\log_2 0.5) = 1.

Constraints

  • Use log2\log_2 so the answer is in bits.
  • Ignore entries where pi=0p_i = 0 (their contribution is 0, and log0\log 0 is undefined).
  • Entropy is maximized (log2K\log_2 K) by the uniform distribution and minimized (0) by a point mass.

Notes

  • Switching to natural log gives entropy in nats; the only difference is the log base.
  • Entropy is the expected "surprise" log2pi-\log_2 p_i; rare events carry more bits of information.
Python
Loading...

This problem ships 5 hidden tests. They run in your browser via Pyodide — no backend, no submission queue. Press ▶ Run tests to execute.

  • Fair coin has 1 bit
  • Certain outcome has 0 entropy
  • Uniform over K has log2(K) bits
  • Zero-probability terms are skipped (no NaN)
  • Uniform maximizes entropy over the same support size