/
asymmetric_mae.py
executable file
·31 lines (24 loc) · 1.14 KB
/
asymmetric_mae.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""MAE with a penalty that differs for positive and negative errors"""
import typing
import numpy as np
from h2oaicore.metrics import CustomScorer
class CostMeanAbsoluteError(CustomScorer):
_description = "MAE function with a penalty that differs for positive and negative errors"
_regression = True
_maximize = False
_perfect_score = 0
_display_name = "Asymmetric MAE"
_supports_sample_weight = False
def score(self,
actual: np.array,
predicted: np.array,
sample_weight: typing.Optional[np.array] = None,
labels: typing.Optional[np.array] = None) -> float:
# Specify the per unit cost of over or underestimating
per_unit_cost_of_underestimating = 2.0
per_unit_cost_of_overestimating = 0.5
cost_function = np.abs(actual - predicted)
cost_function[predicted > actual] = cost_function[predicted > actual] * per_unit_cost_of_overestimating
cost_function[predicted < actual] = cost_function[predicted < actual] * per_unit_cost_of_underestimating
mean_cost = sum(cost_function) / len(cost_function)
return mean_cost