Given labels from left and right child nodes after a binary split, compute the weighted Gini impurity of the split.
Gini impurity is a fundamental measure used in decision trees to evaluate the quality of a split. It quantifies how "impure" or mixed the classes are in a node, with 0 being perfectly pure (all same class) and higher values indicating more mixture.
Gini Impurity Formulas:
For a single node:
Gini(t)=1−i=1∑Cpi2For a weighted split:
Ginisplit=NNL⋅Gini(tL)+NNR⋅Gini(tR)Where pi is the proportion of class i, NL,NR are left/right child sizes, and N=NL+NR.
y_left: array-like - Class labels in left child nodey_right: array-like - Class labels in right child nodeInput: y_left=[0,0,0], y_right=[1,1,1]
Output: 0.0
Input: y_left=[0,1], y_right=[0,1]
Output: 0.5
Use np.unique() with return_counts=True to get class frequencies.
Handle empty nodes by returning 0 impurity (perfectly pure by convention).
Weight each child's Gini by its proportion of total samples.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Given labels from left and right child nodes after a binary split, compute the weighted Gini impurity of the split.
Gini impurity is a fundamental measure used in decision trees to evaluate the quality of a split. It quantifies how "impure" or mixed the classes are in a node, with 0 being perfectly pure (all same class) and higher values indicating more mixture.
Gini Impurity Formulas:
For a single node:
Gini(t)=1−i=1∑Cpi2For a weighted split:
Ginisplit=NNL⋅Gini(tL)+NNR⋅Gini(tR)Where pi is the proportion of class i, NL,NR are left/right child sizes, and N=NL+NR.
y_left: array-like - Class labels in left child nodey_right: array-like - Class labels in right child nodeInput: y_left=[0,0,0], y_right=[1,1,1]
Output: 0.0
Input: y_left=[0,1], y_right=[0,1]
Output: 0.5
Use np.unique() with return_counts=True to get class frequencies.
Handle empty nodes by returning 0 impurity (perfectly pure by convention).
Weight each child's Gini by its proportion of total samples.
Sign in to take notes on this problem
Accepts: array
Accepts: array