Entropy
Shannon entropy measures the average uncertainty or expected surprise (information content) of a random variable's probability distribution. Zero entropy corresponds to a deterministic outcome, whereas entropy reaches its maximum under a uniform distribution; in machine learning it underpins decision tree information gain and cross-entropy loss.
Study Card: Entropy
Direct Answer
Shannon entropy measures the average uncertainty, randomness, or expected information content required to encode draws from a probability distribution. For a discrete random variable taking values with probability mass function , entropy is defined as:
with the standard convention . Using base measures entropy in bits (common in information theory and decision trees), while using the natural logarithm () measures entropy in nats (standard in deep learning cross-entropy loss). Discrete entropy satisfies : it equals when the outcome is completely certain () and attains its maximum when all outcomes are equally likely.
Key Terms
- Uncertainty: The unpredictability of a random variable’s outcome prior to observation.
- Self-Information (Surprisal): , the information gained upon observing an event of probability ; entropy is its expected value.
- Bits vs. Nats: Units of entropy determined by logarithm base ( yields bits; yields nats, where ).
- Node Impurity: In decision trees (ID3/C4.5), high entropy means mixed class labels at a node, while zero entropy means a pure single-class leaf.
Example
Consider two bags of marbles: Bag A holds 5 red and 5 blue marbles (), while Bag B holds 9 red and 1 blue marble ().
For Bag A (maximum binary uncertainty):
For Bag B (more predictable):
Code Implementation
import numpy as np
import matplotlib.pyplot as plt
def calculate_entropy(probabilities):
"""Calculate Shannon entropy (in bits) of a discrete probability distribution."""
probs = np.asarray(probabilities, dtype=float)
probs = probs[probs > 0] # Apply convention 0 * log2(0) = 0
return float(-np.sum(probs * np.log2(probs)))
# Example distributions
entropy_fair_coin = calculate_entropy([0.5, 0.5])
entropy_biased_coin = calculate_entropy([0.9, 0.1])
entropy_deterministic = calculate_entropy([1.0, 0.0])
print(f"Entropy of a fair coin: {entropy_fair_coin:.4f} bits")
print(f"Entropy of a biased coin (0.9): {entropy_biased_coin:.4f} bits")
print(f"Entropy of a deterministic coin: {entropy_deterministic:.4f} bits")
# Binary entropy curve H(p) over [0, 1]
p_values = np.linspace(0.0, 1.0, 101)
entropies = [calculate_entropy([p, 1.0 - p]) for p in p_values]
plt.figure(figsize=(8, 5))
plt.plot(p_values, entropies, linewidth=2)
plt.title('Binary Entropy Function H(p)')
plt.xlabel('Probability of positive outcome (p)')
plt.ylabel('Entropy (bits)')
plt.grid(True)
plt.show()
Related Concepts
- Cross-Entropy: , the primary classification loss function in neural networks and logistic regression.
- KL Divergence: , measuring extra encoding cost when using distribution instead of true distribution .
- Information Gain: Reduction in label entropy after splitting a dataset on feature : , used alongside Gini impurity to build decision trees.
- Differential Entropy: The continuous analog , which unlike discrete entropy can take negative values (e.g., for tightly concentrated continuous distributions).