#137Decision tree — node impurity (Gini & entropy)Easy

Decision tree — node impurity (Gini & entropy)

Background

When a decision tree decides where to split, it measures how mixed a node's labels are. Two classic measures are Gini impurity and entropy. A node with a single class is pure (impurity 0); a node with an even mix of classes is maximally impure. Every CART/ID3 split is chosen to reduce this impurity the most.

Problem statement

Implement node_impurity(labels) returning the tuple (gini, entropy) for the class labels at a node. With pkp_k the fraction of samples in class kk:

Gini=1kpk2,Entropy=kpklog2pk\text{Gini} = 1 - \sum_k p_k^2, \qquad\qquad \text{Entropy} = -\sum_k p_k \log_2 p_k

Entropy is in bits. Use the convention 0log20=00\cdot\log_2 0 = 0.

Input

  • labels — an array-like of class labels of any hashable type (ints, strings, booleans).

Output

  • A tuple (gini, entropy) of two floats.

Examples

Example 1

Input:  labels = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0]
Output: (0.48, 0.9709505944546686)

Explanation: 6 of 10 are class 1 and 4 are class 0, so p=[0.6,0.4]p = [0.6, 0.4]. Gini =1(0.62+0.42)=0.48= 1 - (0.6^2 + 0.4^2) = 0.48 and Entropy =(0.6log20.6+0.4log20.4)0.971= -(0.6\log_2 0.6 + 0.4\log_2 0.4) \approx 0.971 bits.

Constraints

  • Works for any number of classes and any hashable label type.
  • A pure node (all labels equal) returns (0.0, 0.0).
  • Gini lies in [0,1)[0, 1); entropy is 0\ge 0 and equals log2C\log_2 C for CC equally-likely classes.

Notes

  • Only classes that actually appear contribute, so the 0log00\log 0 case never arises if you build proportions from observed counts.
Python
Loading...

▶ Run executes the 2 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: 6 positive, 4 negative -> Gini
  • Reference example -> Entropy (bits)