• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Understanding that entropy measures uncertainty is abstract; practitioners need to know exactly how to compute it on a real dataset to use it for attribute selection in decision trees.

Core Idea

Entropy calculation involves counting class frequencies, computing their proportions, applying the log₂ function to each proportion, multiplying by the proportion, summing these products, and negating the result.

How It Works

Step-by-step computation:

  1. Count instances per class: Tally how many training examples belong to each class label
  2. Compute proportions: Divide each class count by the total number of instances
  3. Apply log₂: Take the base-2 logarithm of each proportion
  4. Weight by proportion: Multiply each log value by its corresponding proportion
  5. Sum and negate: Add all weighted log values and multiply by -1

Worked example from source: Dataset X = {a, a, a, b, b, b, b, b}

  • Total instances: 8
  • Class a: 3 instances → P(a) = 3/8 = 0.375
  • Class b: 5 instances → P(b) = 5/8 = 0.625

H(X)=[0.375×log2(0.375)+0.625×log2(0.625)]H(X) = -[0.375 \times \log_2(0.375) + 0.625 \times \log_2(0.625)] =[0.375×(1.415)+0.625×(0.678)]= -[0.375 \times (-1.415) + 0.625 \times (-0.678)] =(0.5300.424)= -(-0.530 - 0.424) =0.954= 0.954

The entropy of 0.954 (near the maximum of 1.0 for binary) confirms this dataset is highly impure.

Visual Explanation

entropy_calculation raw Raw Dataset {a,a,a,b,b,b,b,b} count Count: a=3, b=5 Total=8 raw->count prob P(a)=0.375, P(b)=0.625 count->prob log log₂(0.375)=-1.415 log₂(0.625)=-0.678 prob->log weighted 0.375×(-1.415)=-0.53 0.625×(-0.678)=-0.424 log->weighted result H(X) = -(-0.53 - 0.424) = 0.954 weighted->result

Key Properties

  • Deterministic: Same input always produces the same entropy value
  • O(c) computation: Linear in the number of classes c (not in the number of instances)
  • Base-2 standard: Log base 2 gives entropy in bits; natural log gives nats; base 10 gives hartleys
  • Zero handling: When pᵢ = 0, the term pᵢ × log(pᵢ) is defined as 0

Connections

Edge Cases & Gotchas

  • Floating point precision: Very small probabilities can cause numerical issues with log
  • Single-class shortcut: If only one class exists, skip computation — entropy is 0
  • Large datasets: Counting can overflow with huge datasets; use incremental or streaming approaches
  • Negative zero: Some implementations may produce -0.0; normalize to 0.0 for consistency