EasyClassic ML

Compute Entropy for a Node

Classic ML

Easy

Problem

Given the class labels at a decision-tree node, compute the node's Shannon entropy. Entropy measures class uncertainty and is used when comparing possible decision-tree splits.

H(S) = -\sum_{i=1}^{C} p_i \log_2(p_i)

Here, S is the collection of labels, C is the number of classes present, and p_i is the fraction of labels belonging to class i. Use the convention

0 \log_2(0) = 0

Return the entropy as a Python float. An empty node has entropy 0.

Theory

Entropy measures the uncertainty or impurity in a set of labels. In decision trees, we use entropy to quantify how mixed the classes are at each node.

High entropy: Labels are evenly mixed, maximum uncertainty

Low entropy: Labels are mostly one class, low uncertainty

Zero entropy: All labels are the same class, perfect purity


The Entropy Formula

For a set with C classes, where p_i is the proportion of samples in class i:

H = -\sum_{i=1}^{C} p_i \log_2(p_i)

Convention: 0 \log_2(0) = 0 (the limit as p \to 0)

The negative sign makes entropy positive (since \log of fractions is negative).


Understanding the Formula

Each term -p_i \log_2(p_i) measures the "surprise" of seeing class i:

Entropy is the expected surprise when sampling from the distribution.


Binary Classification Examples

For 2 classes with proportions (p, 1-p):

H = -p \log_2(p) - (1-p) \log_2(1-p)

Example 1: Perfect purity

All samples class A: p = 1.0

H = -1 \cdot \log_2(1) - 0 \cdot \log_2(0) = -1 \cdot 0 - 0 = 0

Entropy is 0. No uncertainty.

Example 2: Maximum impurity

Half class A, half class B: p = 0.5

H = -0.5 \cdot \log_2(0.5) - 0.5 \cdot \log_2(0.5)

= -0.5 \cdot (-1) - 0.5 \cdot (-1) = 0.5 + 0.5 = 1

Entropy is 1 (maximum for binary). Maximum uncertainty.

Example 3: Moderate impurity

70% class A, 30% class B: p = 0.7

H = -0.7 \cdot \log_2(0.7) - 0.3 \cdot \log_2(0.3)

= -0.7 \cdot (-0.515) - 0.3 \cdot (-1.737)

= 0.36 + 0.52 = 0.88


Multi-Class Example

Node with 100 samples:

H = -0.5 \log_2(0.5) - 0.3 \log_2(0.3) - 0.2 \log_2(0.2)

= -0.5 \cdot (-1) - 0.3 \cdot (-1.737) - 0.2 \cdot (-2.322)

= 0.5 + 0.521 + 0.464 = 1.485

Maximum possible entropy for 3 classes is \log_2(3) \approx 1.585 (when all equal).


Entropy Bounds

Minimum entropy: 0 (all samples same class)

Maximum entropy: \log_2(C) (uniform distribution over C classes)


Entropy in Decision Trees

Decision trees split nodes to reduce entropy. The goal is to create child nodes that are purer than the parent.

Information Gain measures entropy reduction:

\text{IG} = H(\text{parent}) - \sum_{\text{child}} \frac{n_{\text{child}}}{n_{\text{parent}}} H(\text{child})

We choose the split that maximizes information gain (largest entropy reduction).


Step-by-Step: Computing Node Entropy

Given labels: [A, A, B, A, B, B, A, C, A, C]

Step 1: Count each class

Step 2: Compute proportions

Step 3: Compute each term

Step 4: Sum H = 0.5 + 0.521 + 0.464 = 1.485


Handling Edge Cases

All same class:

Labels: [A, A, A, A]

p_A = 1.0, no other classes

H = -1 \cdot \log_2(1) = -1 \cdot 0 = 0

Single sample:

Labels: [B]

p_B = 1.0

H = 0 (certain, no entropy)

Empty proportions:

If a class has 0 samples, p_i = 0 and 0 \log_2(0) = 0 by convention.


Why Log Base 2?

Log base 2 gives entropy in bits:

Other bases work too:

The choice of base affects the scale but not the relative comparisons.


Entropy vs. Gini Impurity

Both measure node impurity. Key differences:

Entropy:

Gini:

In practice, both work well. Gini is more common in implementations (e.g., scikit-learn default).


The Entropy Curve (Binary Case)

For binary classification, entropy as a function of p:

The curve is symmetric around p = 0.5 and concave (bulges upward).

This shape means:


Cross-Entropy Connection

Entropy is related to cross-entropy loss in neural networks:

\text{Cross-Entropy} = -\sum_i y_i \log(\hat{p}_i)

When y is a one-hot label and \hat{p} is the predicted distribution, minimizing cross-entropy pushes predictions toward the true label.

Shannon entropy is cross-entropy of a distribution with itself.

Examples

Example 1

Input
y = [1, 1, 1, 1]
Output
0.0
Explanation
A pure node has no class uncertainty.

Example 2

Input
y = [0, 1, 0, 1]
Output
1.0

Hints

  1. np.unique(y, return_counts=True) returns the number of samples in each class.
  2. counts / len(y) converts class counts into probabilities.
  3. np.log2(probabilities) applies the required logarithm base.

Requirements

Constraints

Starter Code

import numpy as np

def entropy_node(y: list[int]) -> float:
    """
    Returns the Shannon entropy as a Python float.
    """
    # Write code here
    pass

Test Cases

CaseMatches
Pure nodepublic
Binary balancedpublic