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

Compute Gini Impurity for a Split

Classic ML
Medium

Given the class labels sent to the left and right children of a decision-tree split, compute its weighted Gini impurity.

G(S)=1−∑c=1Cpc2G(S)=1-\sum_{c=1}^{C}p_c^2G(S)=1−c=1∑C​pc2​ Gsplit=NLNG(SL)+NRNG(SR)G_{\mathrm{split}}=\frac{N_L}{N}G(S_L)+\frac{N_R}{N}G(S_R)Gsplit​=NNL​​G(SL​)+NNR​​G(SR​)

Here, pcp_cpc​ is the proportion of class ccc in a node, NLN_LNL​ and NRN_RNR​ are the child sizes, and N=NL+NRN=N_L+N_RN=NL​+NR​. An empty child has impurity zero. Return the weighted impurity as a Python float.

Loading visualization...

Examples

Input: y_left = [0, 0, 0], y_right = [1, 1, 1]

Output: 0.0

Explanation: Both child nodes contain only one class, so both impurities are zero.

Input: y_left = [0, 1], y_right = [0, 1]

Output: 0.5

Hint 1

Use np.unique(labels, return_counts=True) to obtain class frequencies.

Hint 2

Compute each node with 1.0 - np.sum(probabilities ** 2) before weighting.

Requirements

  • Compute class proportions independently for each child
  • Weight each child impurity by its share of all samples
  • Support binary and multiclass labels
  • Return a Python float

Constraints

  • The two label lists may have different lengths
  • A child list may be empty
  • Use NumPy only
Try Similar Problems
Entropy NodeInformation GainDecision Tree SplitRandom Forest VoteNaive Bayes Bernoulli

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.
PrevNext

Compute Gini Impurity for a Split

Classic ML
Medium

Given the class labels sent to the left and right children of a decision-tree split, compute its weighted Gini impurity.

G(S)=1−∑c=1Cpc2G(S)=1-\sum_{c=1}^{C}p_c^2G(S)=1−c=1∑C​pc2​ Gsplit=NLNG(SL)+NRNG(SR)G_{\mathrm{split}}=\frac{N_L}{N}G(S_L)+\frac{N_R}{N}G(S_R)Gsplit​=NNL​​G(SL​)+NNR​​G(SR​)

Here, pcp_cpc​ is the proportion of class ccc in a node, NLN_LNL​ and NRN_RNR​ are the child sizes, and N=NL+NRN=N_L+N_RN=NL​+NR​. An empty child has impurity zero. Return the weighted impurity as a Python float.

Loading visualization...

Examples

Input: y_left = [0, 0, 0], y_right = [1, 1, 1]

Output: 0.0

Explanation: Both child nodes contain only one class, so both impurities are zero.

Input: y_left = [0, 1], y_right = [0, 1]

Output: 0.5

Hint 1

Use np.unique(labels, return_counts=True) to obtain class frequencies.

Hint 2

Compute each node with 1.0 - np.sum(probabilities ** 2) before weighting.

Requirements

  • Compute class proportions independently for each child
  • Weight each child impurity by its share of all samples
  • Support binary and multiclass labels
  • Return a Python float

Constraints

  • The two label lists may have different lengths
  • A child list may be empty
  • Use NumPy only
Try Similar Problems
Entropy NodeInformation GainDecision Tree SplitRandom Forest VoteNaive Bayes Bernoulli

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: array

You must run your code first.