Regression: Averaging Practice Problem
This data science coding problem helps you practice Bagging Ensembles, regression: averaging, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Bagging Ensembles.
- Problem ID: 214
- Problem key: 214-regression-averaging
- URL: https://datacrack.app/solve/214-regression-averaging
- Difficulty: easy
- Topic: Bagging Ensembles
- Module: Supervised Learning
Problem Statement
# 🧩 Regression: Averaging
---
### 🎯 Goal
Aggregate regression predictions from multiple models by averaging them, then compare individual errors with the averaged prediction error.
---
### 📖 Introduction
Problem 2 showed how classification predictions are combined using majority voting.
For regression, models predict numbers instead of class labels. So the ensemble combines regression predictions by averaging.
For example:
```python
model_predictions = [8, 10, 12]
true_value = 10
```
The ensemble prediction is the average:
$$
\frac{8+10+12}{3}=10
$$
This shows the basic intuition behind variance reduction in bagging.
Individual models can be unstable. One model may predict too low, another may predict too high, and averaging can make the final prediction more stable than relying on one model.
Averaging does not always make every prediction perfect, but it often reduces how much the final prediction jumps around.
---
### 💻 Task
Implement `bagging_regression_average(model_predictions, true_value)`.
Your function should:
- Return the original individual predictions.
- Calculate the average prediction.
$$
average=\frac{\text{sum of predictions}}{\text{number of predictions}}
$$
- Calculate each individual squared error.
$$
error=(prediction-true\ value)^2
$$
- Calculate the bagged squared error using the averaged prediction.
$$
bagged\ error=(average-true\ value)^2
$$
- Round numeric results to 6 decimals.
- If `model_predictions` is empty, return `None` for `average_prediction` and `bagged_squared_error`.
---
### 📥 Input / 📤 Output
**Input:** regression predictions from multiple models and the true value.
**Output:**
```python
{
"individual_predictions": [...],
"average_prediction": ...,
"individual_squared_errors": [...],
"bagged_squared_error": ...
}
```
---
### 🧩 Starter Code
```python
def bagging_regression_average(model_predictions, true_value):
# Your code here
pass
```
---
### 💡 Example
```python
bagging_regression_average([8, 10, 12], 10)
```
Expected output:
```python
{
"individual_predictions": [8, 10, 12],
"average_prediction": 10.0,
"individual_squared_errors": [4, 0, 4],
"bagged_squared_error": 0.0
}
```
---
### ⚠️ Common Mistakes
- Using majority voting for regression predictions.
- Averaging squared errors instead of averaging predictions first.
- Returning only the average without the error comparison.
- Assuming averaging always makes the final prediction perfect.