Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
25 lines (21 sloc)
861 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Mean Absolute Scaled Error for time-series regression""" | |
import typing | |
import numpy as np | |
from h2oaicore.metrics import CustomScorer | |
class MyMeanAbsoluteScaledErrorScorer(CustomScorer): | |
_description = "My Mean Absolute Scaled Error for Time Series Regression." | |
_regression = True | |
_maximize = False | |
_perfect_score = 0 | |
_display_name = "MASE" | |
def score(self, | |
actual: np.array, | |
predicted: np.array, | |
sample_weight: typing.Optional[np.array] = None, | |
labels: typing.Optional[np.array] = None, | |
**kwargs) -> float: | |
if sample_weight is None: | |
sample_weight = np.ones(actual.shape[0]) | |
naive_errors = np.abs(actual * sample_weight).mean() | |
errors = np.abs((actual - predicted) * sample_weight) | |
return errors.mean() / naive_errors |