Gini Impurity for a split
Beginner classical-mldecision-treessplitting-criteria
Implement:
def gini_impurity(labels: np.ndarray) -> float:
"""
labels: 1-D array of class labels (any integers, not necessarily
0..k-1 or contiguous), one per sample in a single tree node.
Returns:
the Gini impurity of this set of labels, a float in [0, 1).
"""
- Works for any number of classes, not just binary.
- Labels don't need to be
0..k-1, use whatever distinct values are actually present.
- An empty
labels array has no impurity by convention, return 0.0.
- A node where every label is identical is perfectly pure,
0.0.