Shannon Entropy

MediumMachine LearningMathArray

Description

Given an array labels of class labels, return the Shannon entropy -sum(p_i·log2(p_i)) where p_i is the proportion of each class, rounded to 4 decimal places.

Examples

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

Summing the negative proportion-weighted log2 proportions gives an entropy of 1.

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

Summing the negative proportion-weighted log2 proportions gives an entropy of 0.

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

Summing the negative proportion-weighted log2 proportions gives an entropy of 2.

Constraints

  • 1 ≤ labels.length ≤ 10⁴

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.