Gini Impurity Practice Problem
This data science coding problem helps you practice Decision Trees, gini impurity, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Decision Trees.
- Problem ID: 208
- Problem key: 208-gini-impurity
- URL: https://datacrack.app/solve/208-gini-impurity
- Difficulty: easy
- Topic: Decision Trees
- Module: Supervised Learning
Problem Statement
# 🧩 Gini Impurity
---
### 🎯 Goal
Calculate the impurity of one decision-tree node using **Gini impurity**.
Gini impurity measures how mixed the class labels are inside a decision-tree node. A value of 0 means the node contains samples from only one class, while higher values mean the node contains a greater mix of classes. In a binary classification node, the maximum Gini impurity is 0.5, which happens when the node is split evenly between the two classes, such as 50% Class 0 and 50% Class 1.
---
### 📖 Introduction
A decision tree makes predictions by asking a sequence of questions.
For example:
> Is the flower petal length less than 2 cm?
> Is the customer age greater than 30?
Each question separates the data into smaller groups. Each group is called a **node**.
A node contains some training samples and their labels. If all samples in the node have the same label, the node is **pure**.
If the samples have different labels, the node is **impure**.
Gini impurity measures how mixed the labels are inside a node. In a binary classification node, a Gini impurity of `0` means all samples belong to one class, while the maximum value is `0.5`, which happens when the node is evenly split between the two classes.
$$
Gini = 1 - \sum_{j=1}^{k} p_j^2
$$
where:
- $k$ is the number of classes in the node.
- $j$ is the class index, from `1` to `k`.
- $p_j$ is the proportion of samples that belong to class $j$.
For example, if `class_counts = [5, 5]`, then:
- $k = 2$
- $p_1 = \frac{5}{10} = 0.5$
- $p_2 = \frac{5}{10} = 0.5$
---
### 💻 Task
Implement `gini_impurity(class_counts)` using the Gini impurity formula:
$$
Gini = 1 - \sum_{j=1}^{k} p_j^2
$$
Your function should:
- Take a list of class counts.
- Compute the proportion of each class.
- Return the Gini impurity rounded to 6 decimals.
- Return `0.0` if the node has no samples.
---
### 📥 Input / 📤 Output
**Input**
- `class_counts`: list of counts, one count per class
**Output**
- float: Gini impurity rounded to 6 decimals
---
### 🧩 Starter Code
```python
def gini_impurity(class_counts):
# Your code here
pass
```
---
### 💡 Example
```python
gini_impurity([5, 5])
```
Expected Output:
```python
0.5
```
---
### ⚠️ Common Mistakes
- Dividing by the number of classes instead of the number of samples.
- Forgetting that pure nodes should return `0`.
- Not handling an empty node safely.Starter Code
def gini_impurity(class_counts):
# Your code here
pass