TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Compute Entropy for a Node

Classic ML
Easy

Given the class labels at a decision-tree node, compute the node's Shannon entropy. Entropy measures class uncertainty and is used when comparing possible decision-tree splits.

H(S)=−∑i=1Cpilog⁡2(pi)H(S) = -\sum_{i=1}^{C} p_i \log_2(p_i)H(S)=−i=1∑C​pi​log2​(pi​)

Here, SSS is the collection of labels, CCC is the number of classes present, and pip_ipi​ is the fraction of labels belonging to class iii. Use the convention

0log⁡2(0)=00 \log_2(0) = 00log2​(0)=0

Return the entropy as a Python float. An empty node has entropy 0.

Loading visualization...

Examples

Input: y = [1, 1, 1, 1]

Output: 0.0

Explanation: A pure node has no class uncertainty.

Input: y = [0, 1, 0, 1]

Output: 1.0

Hint 1

np.unique(y, return_counts=True) returns the number of samples in each class.

Hint 2

counts / len(y) converts class counts into probabilities.

Hint 3

np.log2(probabilities) applies the required logarithm base.

Requirements

  • Support binary and multiclass labels
  • Use base-2 logarithms
  • Avoid evaluating the logarithm of zero
  • Return 0.0 for an empty input
  • Return a Python float

Constraints

  • y contains at most 1,000,0001{,}000{,}0001,000,000 integer class labels
  • Use NumPy only
Try Similar Problems
Gini ImpurityInformation GainDecision Tree SplitRandom Forest VoteKl Divergence

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Compute Entropy for a Node

Classic ML
Easy

Given the class labels at a decision-tree node, compute the node's Shannon entropy. Entropy measures class uncertainty and is used when comparing possible decision-tree splits.

H(S)=−∑i=1Cpilog⁡2(pi)H(S) = -\sum_{i=1}^{C} p_i \log_2(p_i)H(S)=−i=1∑C​pi​log2​(pi​)

Here, SSS is the collection of labels, CCC is the number of classes present, and pip_ipi​ is the fraction of labels belonging to class iii. Use the convention

0log⁡2(0)=00 \log_2(0) = 00log2​(0)=0

Return the entropy as a Python float. An empty node has entropy 0.

Loading visualization...

Examples

Input: y = [1, 1, 1, 1]

Output: 0.0

Explanation: A pure node has no class uncertainty.

Input: y = [0, 1, 0, 1]

Output: 1.0

Hint 1

np.unique(y, return_counts=True) returns the number of samples in each class.

Hint 2

counts / len(y) converts class counts into probabilities.

Hint 3

np.log2(probabilities) applies the required logarithm base.

Requirements

  • Support binary and multiclass labels
  • Use base-2 logarithms
  • Avoid evaluating the logarithm of zero
  • Return 0.0 for an empty input
  • Return a Python float

Constraints

  • y contains at most 1,000,0001{,}000{,}0001,000,000 integer class labels
  • Use NumPy only
Try Similar Problems
Gini ImpurityInformation GainDecision Tree SplitRandom Forest VoteKl Divergence

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.