Skip to content

feat: add ml.metrics.mean_absolute_error method #1910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bigframes/ml/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
auc,
confusion_matrix,
f1_score,
mean_absolute_error,
mean_squared_error,
precision_score,
r2_score,
Expand All @@ -36,6 +37,7 @@
"confusion_matrix",
"precision_score",
"f1_score",
"mean_absolute_error",
"mean_squared_error",
"pairwise",
]
14 changes: 14 additions & 0 deletions bigframes/ml/metrics/_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,17 @@ def mean_squared_error(
mean_squared_error.__doc__ = inspect.getdoc(
vendored_metrics_regression.mean_squared_error
)


def mean_absolute_error(
y_true: Union[bpd.DataFrame, bpd.Series],
y_pred: Union[bpd.DataFrame, bpd.Series],
) -> float:
y_true_series, y_pred_series = utils.batch_convert_to_series(y_true, y_pred)

return (y_pred_series - y_true_series).abs().sum() / len(y_true_series)


mean_absolute_error.__doc__ = inspect.getdoc(
vendored_metrics_regression.mean_absolute_error
)
7 changes: 7 additions & 0 deletions tests/system/small/ml/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,3 +818,10 @@ def test_mean_squared_error(session: bigframes.Session):
df = session.read_pandas(pd_df)
mse = metrics.mean_squared_error(df["y_true"], df["y_pred"])
assert mse == 0.375


def test_mean_absolute_error(session: bigframes.Session):
pd_df = pd.DataFrame({"y_true": [3, -0.5, 2, 7], "y_pred": [2.5, 0.0, 2, 8]})
df = session.read_pandas(pd_df)
mse = metrics.mean_absolute_error(df["y_true"], df["y_pred"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: empty line below the "mse = ...":
go/unit-testing-practices?polyglot=python#structure

assert mse == 0.5
27 changes: 27 additions & 0 deletions third_party/bigframes_vendored/sklearn/metrics/_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,30 @@ def mean_squared_error(y_true, y_pred) -> float:
float: Mean squared error.
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)


def mean_absolute_error(y_true, y_pred) -> float:
"""Mean absolute error regression loss.

**Examples:**

>>> import bigframes.pandas as bpd
>>> import bigframes.ml.metrics
>>> bpd.options.display.progress_bar = None

>>> y_true = bpd.DataFrame([3, -0.5, 2, 7])
>>> y_pred = bpd.DataFrame([2.5, 0.0, 2, 8])
>>> mae = bigframes.ml.metrics.mean_absolute_error(y_true, y_pred)
>>> mae
np.float64(0.5)

Args:
y_true (Series or DataFrame of shape (n_samples,)):
Ground truth (correct) target values.
y_pred (Series or DataFrame of shape (n_samples,)):
Estimated target values.

Returns:
float: Mean absolute error.
"""
raise NotImplementedError(constants.ABSTRACT_METHOD_ERROR_MESSAGE)