Adjusted R2 Practice Problem
This data science coding problem helps you practice Evaluation Metrics, adjusted r2, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Evaluation Metrics.
- Problem ID: 20
- Problem key: 20-adjusted-r2
- URL: https://datacrack.app/solve/20-adjusted-r2
- Difficulty: easy
- Topic: Evaluation Metrics
- Module: Introduction to Machine Learning
Problem Statement
## 🧩 Adjusted R² Score
### 🎯 Goal
Compute the **Adjusted R²** score to fairly evaluate a regression model while accounting for the number of features used.
---
### 📥 Input / 📤 Output
**Input**
- `y_true` (`list[float]`): true target values
- `y_pred` (`list[float]`): predicted values from the model
- `n_features` (`int`): number of features used by the model
**Output**
- `float`: the Adjusted R² score
---
### 💻 Task Description
1. Compute the R² score
2. Use it to compute the **Adjusted R²**:
$$
R^2_{\text{adj}} = 1 - (1 - R^2)\frac{n - 1}{n - p - 1}
$$
where:
- \( n \) = number of samples
- \( p \) = number of features
Adjusted R² penalizes models that add features without meaningful improvement.
---
### 🧩 Starter Code
```python
import numpy as np
def adjusted_r2_score(y_true, y_pred, n_features):
"""
Returns the Adjusted R² score.
"""
pass
```
⸻
💡 Example
```python
y_true = [3, -1, 2, 5]
y_pred = [2, 0, 2, 4]
n_features = 1
adjusted_r2_score(y_true, y_pred, n_features)
```
**Expected Output**
```python
0.76
```
⸻
Starter Code
import numpy as np
def adjusted_r2_score(y_true, y_pred, n_features):
"""
Returns the Adjusted R² score.
"""
pass