Gini Impurity

MediumMachine LearningMathArray

Description

Given an array labels of class labels, return the Gini impurity 1 - sum(p_i²) where p_i is the proportion of each class, rounded to 4 decimal places.

Examples

Input:labels = [0,0,1,1]
Output:0.5
Explanation:

Subtracting the summed squared class proportions from one gives an impurity of 0.5.

Input:labels = [1,1,1]
Output:0
Explanation:

Subtracting the summed squared class proportions from one gives an impurity of 0.

Input:labels = [0,1,2,0]
Output:0.625
Explanation:

Subtracting the summed squared class proportions from one gives an impurity of 0.625.

Constraints

  • 1 ≤ labels.length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.