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=1∑Cpilog2(pi)Here, S is the collection of labels, C is the number of classes present, and pi is the fraction of labels belonging to class i. Use the convention
0log2(0)=0Return the entropy as a Python float. An empty node has entropy 0.
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
np.unique(y, return_counts=True) returns the number of samples in each class.
counts / len(y) converts class counts into probabilities.
np.log2(probabilities) applies the required logarithm base.
Sign in to take notes on this problem
Accepts: array
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=1∑Cpilog2(pi)Here, S is the collection of labels, C is the number of classes present, and pi is the fraction of labels belonging to class i. Use the convention
0log2(0)=0Return the entropy as a Python float. An empty node has entropy 0.
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
np.unique(y, return_counts=True) returns the number of samples in each class.
counts / len(y) converts class counts into probabilities.
np.log2(probabilities) applies the required logarithm base.
Sign in to take notes on this problem
Accepts: array