Skip to content

Commit 1b076ad

Browse files
committed
Add macro-averaged mean squared error with test
1 parent 5a5f6d7 commit 1b076ad

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

imblearn/metrics/_regression.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import numpy as np
2+
3+
def macro_mean_squared_error(y_true, y_pred):
4+
"""
5+
Compute the macro-averaged mean squared error.
6+
7+
Parameters
8+
----------
9+
y_true : array-like of shape (n_samples, n_outputs)
10+
True values.
11+
y_pred : array-like of shape (n_samples, n_outputs)
12+
Predicted values.
13+
14+
Returns
15+
-------
16+
float
17+
Macro-averaged MSE over all outputs.
18+
"""
19+
y_true = np.array(y_true)
20+
y_pred = np.array(y_pred)
21+
22+
# Compute MSE for each output
23+
mse_per_output = np.mean((y_true - y_pred) ** 2, axis=0)
24+
25+
# Return macro-average
26+
return np.mean(mse_per_output)

imblearn/tests/test_regression.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import numpy as np
2+
from imblearn.metrics._regression import macro_mean_squared_error
3+
4+
def test_macro_mse():
5+
y_true = np.array([[1, 2], [3, 4]])
6+
y_pred = np.array([[1, 1], [4, 4]])
7+
8+
result = macro_mean_squared_error(y_true, y_pred)
9+
expected = np.mean([np.mean([0,1]), np.mean([1,0])]) # manual calculation
10+
assert np.isclose(result, expected)

0 commit comments

Comments
 (0)