Evaluation Summary for Regression Practice Problem
This data science coding problem helps you practice Evaluation Metrics for Regression, evaluation summary for regression, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Evaluation Metrics for Regression.
- Problem ID: 137
- Problem key: 137-evaluation-summary-for-regression
- URL: https://datacrack.app/solve/137-evaluation-summary-for-regression
- Difficulty: easy
- Topic: Evaluation Metrics for Regression
- Module: Introduction to Machine Learning
Problem Statement
## 🧩 Evaluation Summary for Regression
### 🎯 Goal
Create a comprehensive evaluation function that calculates all key regression metrics (MAE, MSE, RMSE, R²) at once.
---
### 💻 Task Description
Instead of running separate functions for each metric, it is common practice to create a summary function.
1. Calculate **MAE**.
2. Calculate **MSE**.
3. Calculate **RMSE** (square root of MSE).
4. Calculate **R²**.
5. Return them as a dictionary, with each metric rounded to 6 decimal places.
---
### 📥 Input / 📤 Output
**Input**
- `y_true` (`list[float]`): true target values
- `y_pred` (`list[float]`): model predictions
**Output**
- `dict`: A dictionary containing the following keys, each rounded to 6 decimal places:
* `"mae"`: Mean Absolute Error
* `"mse"`: Mean Squared Error
* `"rmse"`: Root Mean Squared Error
* `"r2"`: R-squared score
---
### 🧩 Starter Code
```python
import numpy as np
def evaluate_regression(y_true, y_pred):
"""
Returns a dictionary of evaluation metrics.
"""
pass
```
---
### 💡 Example
```python
y_true = [3, -1, 2, 7]
y_pred = [2.5, 0.5, 2, 8]
evaluate_regression(y_true, y_pred)
```
**Expected Output**
```
{'mae': 0.75, 'mse': 0.875, 'rmse': 0.93541, 'r2': 0.89705}
```
---
Starter Code
import numpy as np
def evaluate_regression(y_true, y_pred):
"""
Returns a dictionary of evaluation metrics.
"""
pass