Detect Underfitting Practice Problem
This data science coding problem helps you practice Model Generalization, detect underfitting, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Model Generalization.
- Problem ID: 160
- Problem key: 160-detect-underfitting
- URL: https://datacrack.app/solve/160-detect-underfitting
- Difficulty: easy
- Topic: Model Generalization
- Module: Introduction to Machine Learning
Problem Statement
# 🧩 Detect Underfitting
---
### 🎯 Goal
Detect when a model performs poorly on both training data and validation data.
---
### 📖 Introduction
A model **underfits** when it is too simple to learn the important patterns in the data.
Unlike overfitting, underfitting usually does not show a large train-validation gap.
The common pattern is:
- training score is low
- validation score is also low
- the gap between them is small
This means the model is not even fitting the training data well.
---
### 💻 Task
Implement `detect_underfitting`.
Steps:
1. Compute the generalization gap:
$$
\text{gap} = \text{train score} - \text{validation score}
$$
2. A model is underfitting if:
- `train_score < min_acceptable_score`
- `validation_score < min_acceptable_score`
- `abs(generalization_gap) <= gap_threshold`
3. Return a dictionary with all numeric values rounded to 6 decimal places.
---
### 📥 Input / 📤 Output
**Input**
- `train_score` (`float`): score on training data, where higher is better
- `validation_score` (`float`): score on validation data, where higher is better
- `min_acceptable_score` (`float`): minimum score considered acceptable
- `gap_threshold` (`float`): maximum gap allowed for the model to still be considered consistently weak
**Output**
- `dict`: containing `train_score`, `validation_score`, `generalization_gap`, and `is_underfitting`
---
### 🧩 Starter Code
```python
def detect_underfitting(train_score, validation_score, min_acceptable_score=0.7, gap_threshold=0.1):
"""
Detect whether a model is underfitting using train and validation scores.
"""
# TODO 1: Compute the generalization gap
# TODO 2: Check whether both scores are low and close together
# TODO 3: Return the diagnostic dictionary
pass
```
---
### 💡 Example
```python
detect_underfitting(0.52, 0.50, min_acceptable_score=0.7, gap_threshold=0.1)
```
**Expected Output**
```python
{
"train_score": 0.52,
"validation_score": 0.5,
"generalization_gap": 0.02,
"is_underfitting": True
}
```
---
### 🧭 Hint
Underfitting is not just low validation performance. The training score should also be low.
Starter Code
def detect_underfitting(train_score, validation_score, min_acceptable_score=0.7, gap_threshold=0.1):
"""
Detect whether a model is underfitting using train and validation scores.
"""
# TODO 1: Compute the generalization gap
# TODO 2: Check whether both scores are low and close together
# TODO 3: Return the diagnostic dictionary
pass