Click4Ai

76.

Easy

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:

  • y is a 1D numpy array of integer class labels
  • Return a non-negative float (0.0 for pure sets)
  • Handle the case where p_i = 0 (should contribute 0 to the sum, not NaN)
  • Test Cases

    Test Case 1
    Input: [0, 0, 0, 0]
    Expected: 0.0
    Test Case 2
    Input: [0, 1, 0, 1]
    Expected: 1.0
    Test Case 3
    Input: [0, 1, 2, 3]
    Expected: 2.0
    + 2 hidden test cases