Mean Squared Error Practice Problem
This data science coding problem helps you practice Linear Regression, mean squared error, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Linear Regression.
- Problem ID: 5
- Problem key: 5-mean-squared-error
- URL: https://datacrack.app/solve/5-mean-squared-error
- Difficulty: easy
- Topic: Linear Regression
- Module: Introduction to Machine Learning
Problem Statement
# 🧩 Calculate the Mean Squared Error (MSE)
---
### 🎯 Goal
In Linear Regression, we need to measure how far the model’s predictions are from the true values.
One of the most common metrics for this is the **Mean Squared Error (MSE)**.
$$
MSE = \frac{1}{n}\sum_{i=1}^{n}(y_i - \hat{y}_i)^2
$$
A lower MSE means your model’s predictions are closer to the real data.
---
### 🔍 Explanation of Symbols
| Symbol | Meaning | Shape / Type |
|:-------:|:--------|:-------------|
| **$y_i$** | Actual (true) value of the $i^{th}$ sample | scalar |
| **$\hat{y}_i$** | Predicted value of the $i^{th}$ sample | scalar |
| **$n$** | Number of samples | integer |
---
### 📥 Input
- `y_true`: NumPy array of shape (n,) — true (target) values
- `y_pred`: NumPy array of shape (n,) — predicted values
### 📤 Output
- A single float: the mean squared error
---
### 💻 Task
Implement a Python function `mean_squared_error(y_true, y_pred)` that returns the Mean Squared Error computed from these two arrays.
---
### 🧩 Starter Code
```python
import numpy as np
def mean_squared_error(y_true, y_pred):
"""
Compute the Mean Squared Error (MSE) between two NumPy arrays.
Args:
y_true (np.ndarray): Actual values
y_pred (np.ndarray): Predicted values
Returns:
float: Mean Squared Error
"""
# 🧠 TODO: Implement the MSE formula using np.mean and element-wise operations
pass
```
---
### 💡 Example
```python
y_true = np.array([3, -0.5, 2, 7])
y_pred = np.array([2.5, 0.0, 2, 8])
mean_squared_error(y_true, y_pred)
```
#### Expected Output
```python
0.375
```
---
Starter Code
import numpy as np
def mean_squared_error(y_true, y_pred):
"""
Compute the Mean Squared Error (MSE) between two NumPy arrays.
Args:
y_true (np.ndarray): Actual values
y_pred (np.ndarray): Predicted values
Returns:
float: Mean Squared Error
"""
# 🧠 TODO: Implement the MSE formula using np.mean and element-wise operations
pass