Gini impurity

Write codeMedium18 minMeta style

Your task

Compute the Gini impurity of a list of class labels (integers or strings). Gini impurity = 1 - sum over each class of (p_class)^2, where p_class is that class's fraction of the labels. A pure node (all one class) has impurity 0. The input list is non-empty. Round the result to 6 decimal places.

Examples

gini_impurity(labels: list[int]) -> float
Example 1
Inputgini_impurity([0, 0, 1, 1])
Output0.5