A tree needs a way to tell whether the labels arriving at a node agree. Pure nodes need no more splitting; mixed nodes may benefit from it. Gini impurity summarizes that label mixture without assuming class labels are consecutive integers.
Implement gini_impurity(labels) by counting each distinct label, converting counts to proportions, and applying the impurity formula. Theory also defines the empty-node convention used by later split logic.
labels is a one-dimensional array of arbitrary integer class labels.float in [0, 1).0.0 for an empty array and for a pure node.np.unique(..., return_counts=True).Open one at a time. Each gives away a little more than the last.
Only class frequencies matter; label values such as 5 and 7 are no different from 0 and 1.
Divide counts by labels.size, square every proportion, sum them, then subtract from one.
No results yet
Run your code to check it against the examples, or submit to run every test.