Decision Tree on a Real Dataset Practice Problem
This data science coding problem helps you practice Decision Trees, decision tree on a real dataset, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Decision Trees.
- Problem ID: 205
- Problem key: 205-decision-tree-on-a-real-dataset
- URL: https://datacrack.app/solve/205-decision-tree-on-a-real-dataset
- Difficulty: medium
- Topic: Decision Trees
- Module: Supervised Learning
Problem Statement
# 🧩 Decision Tree on a Real Dataset
---
### 🎯 Goal
Train and evaluate sklearn's `DecisionTreeClassifier` on the Iris dataset.
This problem connects the earlier decision-tree concepts to a real library model.
---
### 📖 Introduction
The earlier problems showed the ingredients of decision trees:
- impurity scores
- entropy and information gain
- splitting a dataset
- choosing a good split
- traversing a trained tree
Now sklearn handles the training algorithm for us.
The Iris dataset contains flower measurements and species labels. A decision tree learns threshold questions such as:
```text
petal width (cm) <= 0.8
```
Then it uses those questions to classify new flowers.
---
### 💻 Task
Implement `iris_decision_tree_classifier(max_depth=3, test_size=0.2, random_state=42)`.
Your function should:
- Load the Iris dataset.
- Split the data into training and test sets using:
- `test_size=test_size`
- `random_state=random_state`
- `stratify=y`
- Train `DecisionTreeClassifier` using:
- `max_depth=max_depth`
- `random_state=random_state`
- Predict on the test set.
- Compute test accuracy.
- Return model details and feature importances.
Round accuracy to 4 decimals and feature importances to 6 decimals.
---
### 📥 Input / 📤 Output
**Input**
- `max_depth`: maximum depth allowed for the decision tree
- `test_size`: proportion of the dataset used for testing
- `random_state`: seed used to make the train/test split and model training reproducible
**Output**
Return a dictionary with:
- `accuracy`: test accuracy rounded to 4 decimals
- `tree_depth`: depth of the trained tree
- `n_leaves`: number of leaf nodes in the trained tree
- `predictions`: model predictions on the test set as a list
- `feature_importances`: dictionary mapping each feature name to its importance, rounded to 6 decimals
---
### 🧩 Starter Code
```python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
def iris_decision_tree_classifier(max_depth=3, test_size=0.2, random_state=42):
"""
Train and evaluate a DecisionTreeClassifier on the Iris dataset.
"""
data = load_iris()
X = data.data
y = data.target
# Your code here
pass
```
---
### 💡 Example
```python
iris_decision_tree_classifier(max_depth=3, random_state=42)
```
Expected Output starts like:
```python
{
"accuracy": 0.9667,
"tree_depth": 3,
"n_leaves": 5,
"predictions": [...],
"feature_importances": {...}
}
```
---
### ⚠️ Common Mistakes
- Evaluating on the training set instead of the test set.
- Forgetting `stratify=y`, which helps keep the class proportions similar in the training and test sets.
- Confusing `max_depth` with number of trees. This model trains one tree.