Skip to content
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

Added support for calculating common metrics for 2d inputs #13

Merged
merged 2 commits into from
Dec 20, 2023
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
11 changes: 9 additions & 2 deletions ocf_ml_metrics/metrics/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ def common_metrics(predictions: np.ndarray, target: np.ndarray, tag: str = "", *
"""
error_dict = {}

error_dict[tag + "mae"] = np.mean(np.abs(predictions - target))
error_dict[tag + "rmse"] = np.sqrt(np.mean(np.square(predictions - target)))
def _mean(input):
# 2+ dimensional input - compute mean but preserve 0th dimension, yields 1-d ndarray
if len(predictions.shape) > 1:
return np.mean(input, axis=0)
else:
return np.mean(input)

error_dict[tag + "mae"] = _mean(np.abs(predictions - target))
error_dict[tag + "rmse"] = np.sqrt(_mean(np.square(predictions - target)))

return error_dict

Expand Down
17 changes: 14 additions & 3 deletions tests/metrics/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,26 @@
from tests.consts_for_tests import N_METRICS


def test_common_error_metrics():
predictions = np.random.random((288, 1))
target = np.random.random((288, 1))
def test_common_error_metrics_1d_input():
predictions = np.random.random(8)
target = np.random.random(8)
errors = common_metrics(predictions=predictions, target=target)
for key in ["mae", "rmse"]:
assert key in errors.keys()
assert isinstance(errors[key], float)


def test_common_error_metrics_2d_input():
predictions = np.random.random((8, 16))
target = np.random.random((8, 16))
errors = common_metrics(predictions=predictions, target=target)
for key in ["mae", "rmse"]:
assert key in errors.keys()
assert isinstance(errors[key], np.ndarray)
assert errors[key].shape == (16,)
assert errors[key].dtype == np.float64


def test_compute_error_part_of_year():
predictions = np.zeros((12, 1))

Expand Down
Loading