Implement Gini Impurity, Entropy and Information Gain in Python

Quick Overview

Implement Gini impurity, base-2 entropy and information gain in Python for multiclass labels of any hashable type. It tests the impurity formulas, size-weighted gain, empty and pure node edge cases, and numerically safe code that never takes the log of zero.

Implement Gini Impurity, Entropy and Information Gain in Python

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `gini(labels)`, `entropy(labels)` (base 2) and `information_gain(parent, children, criterion="entropy")` for multiclass label arrays. Information gain is the parent impurity minus the size-weighted average child impurity (weights `len(child) / len(parent)`). All functions return a Python `float`. Example: ```python gini([0, 0, 0, 1, 1, 2]) # 22/36 ≈ 0.6111 entropy([0, 1, 2, 3, 0, 1, 2, 3]) # 2.0 information_gain([0, 0, 1, 1], [[0, 0], [1, 1]]) # 1.0 information_gain([0, 0, 1, 1], [[0, 0], [1, 1]], criterion="gini") # 0.5 ``` ```hint Watch the counting step Look at which classes your counting method produces. A class with a zero count turns into `log2(0)`, and some counting functions also assume the labels are small non-negative integers. ``` ### Constraints & Assumptions - Labels may be lists or arrays of any hashable values (non-contiguous ints, strings). - Empty input gives `0.0` for both impurities. An empty parent gives a gain of `0.0`. Empty children contribute nothing. - No NumPy warnings may be emitted (empty nodes, zero-probability classes). - `criterion` is `"entropy"` or `"gini"`. Anything else raises `ValueError`. - Gain must be non-negative (within `1e-12`) when the children partition the parent. ### What a Strong Answer Covers - Correct Gini and base-2 entropy formulas computed from class proportions. - Counting that works for arbitrary hashable labels and never evaluates the log of zero. - The size-weighted gain, with empty parent and empty child cases handled and a `ValueError` for an unknown criterion. - Return types and numerical edge cases such as a pure node. - Why the gain cannot be negative when the children partition the parent. ### Follow-up Questions - How would you add per-sample weights? - Why might information gain ratio be preferred over raw gain? - When would you choose Gini over entropy as the split criterion?

Overview: Implement Gini impurity, base-2 entropy and information gain in Python for multiclass labels of any hashable type. It tests the impurity formulas, size-weighted gain, empty and pure node edge cases, and numerically safe code that never takes the log of zero.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement gini(labels), entropy(labels) (base 2) and information_gain(parent, children, criterion="entropy") for multiclass label arrays. Information gain is the parent impurity minus the size-weighted average child impurity (weights len(child) / len(parent)). All functions return a Python float.

Example:

gini([0, 0, 0, 1, 1, 2])           # 22/36 ≈ 0.6111
entropy([0, 1, 2, 3, 0, 1, 2, 3])  # 2.0
information_gain([0, 0, 1, 1], [[0, 0], [1, 1]])                    # 1.0
information_gain([0, 0, 1, 1], [[0, 0], [1, 1]], criterion="gini")  # 0.5

Constraints & Assumptions

  • Labels may be lists or arrays of any hashable values (non-contiguous ints, strings).
  • Empty input gives 0.0 for both impurities. An empty parent gives a gain of 0.0 . Empty children contribute nothing.
  • No NumPy warnings may be emitted (empty nodes, zero-probability classes).
  • criterion is "entropy" or "gini" . Anything else raises ValueError .
  • Gain must be non-negative (within 1e-12 ) when the children partition the parent.

What a Strong Answer Covers Guidance

  • Correct Gini and base-2 entropy formulas computed from class proportions.
  • Counting that works for arbitrary hashable labels and never evaluates the log of zero.
  • The size-weighted gain, with empty parent and empty child cases handled and a ValueError for an unknown criterion.
  • Return types and numerical edge cases such as a pure node.
  • Why the gain cannot be negative when the children partition the parent.

Follow-up Questions Guidance

  • How would you add per-sample weights?
  • Why might information gain ratio be preferred over raw gain?
  • When would you choose Gini over entropy as the split criterion?