Gini Reduction Practice Problem
This data science coding problem helps you practice Decision Trees, gini reduction, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Decision Trees.
- Problem ID: 209
- Problem key: 209-gini-reduction
- URL: https://datacrack.app/solve/209-gini-reduction
- Difficulty: medium
- Topic: Decision Trees
- Module: Supervised Learning
Problem Statement
# 🧩 Gini Reduction
---
### 🎯 Goal
Measure how much a decision-tree split improves a node using **Gini impurity**.
Gini reduction measures how much the split reduces impurity by comparing the parent node with the weighted impurity of the child nodes.
---
### 📖 Introduction
You already learned that **Gini impurity** measures how mixed a single node is.
You also learned that **Information Gain** measures split improvement using entropy.
Decision trees can use different criteria to decide whether a split is good. This problem introduces the Gini-based alternative: **Gini Reduction**.
A split is useful when it turns one mixed parent node into cleaner child nodes.
$$
Gini\ Reduction = Gini_{parent} - Gini_{children}
$$
The child Gini is weighted by child size:
$$
Gini_{children}=\frac{n_L}{n}Gini_L+\frac{n_R}{n}Gini_R
$$
where:
- $n_L$ is the number of samples in the left child.
- $n_R$ is the number of samples in the right child.
- $n$ is the number of samples in the parent.
---
### 💻 Task
Implement `gini_reduction(parent_counts, left_counts, right_counts)`.
For this app, use only **one top-level function**. If you want a helper for Gini impurity, define it **inside** `gini_reduction`.
Your function should return a dictionary with:
- `parent_gini`
- `left_gini`
- `right_gini`
- `weighted_child_gini`
- `gini_reduction`
Round every value to 6 decimals.
---
### 📥 Input / 📤 Output
**Input**
- `parent_counts`: class counts before the split
- `left_counts`: class counts in the left child
- `right_counts`: class counts in the right child
**Output**
- dictionary containing Gini values and Gini reduction
---
### 🧩 Starter Code
```python
def gini_reduction(parent_counts, left_counts, right_counts):
def gini_impurity(class_counts):
# Helper lives inside the main function
# Your Gini code here
pass
# Your Gini reduction code here
pass
```
---
### 💡 Example
```python
gini_reduction([3, 3], [3, 0], [0, 3])
```
Expected Output:
```python
{
"parent_gini": 0.5,
"left_gini": 0.0,
"right_gini": 0.0,
"weighted_child_gini": 0.0,
"gini_reduction": 0.5
}
```
---
### ⚠️ Common Mistakes
- Forgetting to weight left and right child Gini by child size.
- Subtracting in the wrong order.
- Returning only the reduction instead of the full dictionary.
- Defining a second top-level helper function instead of nesting it inside `gini_reduction`.Starter Code
def gini_reduction(parent_counts, left_counts, right_counts):
def gini_impurity(class_counts):
# Helper lives inside the main function
# Your Gini code here
pass
# Your Gini reduction code here
pass