Write a function entropy(y) that computes the Shannon entropy of a label array. Entropy measures the uncertainty or disorder in a dataset and is commonly used in decision tree algorithms to determine the best split.
**Formula:** H = -sum(p_i * log2(p_i)) where p_i is the proportion of class i. By convention, 0 * log2(0) = 0.
Example:
Input: y = [0, 1, 0, 1]
Output: 1.0
**Explanation:** Two classes each with proportion 0.5. H = -(0.5 * log2(0.5) + 0.5 * log2(0.5)) = -(0.5 * -1 + 0.5 * -1) = 1.0
Constraints:
Test Cases
Test Case 1
Input:
[0, 0, 0, 0]Expected:
0.0Test Case 2
Input:
[0, 1, 0, 1]Expected:
1.0Test Case 3
Input:
[0, 1, 2, 3]Expected:
2.0+ 2 hidden test cases