-
-
Save minesh1291/650e189e1bae3050b5d158b08de7ce09 to your computer and use it in GitHub Desktop.
Mean Absolute Percentage Error (MAPE) metric for python sklearn. Written in response to a question on Cross Validated: http://stats.stackexchange.com/questions/58391/mean-absolute-percentage-error-mape-in-scikit-learn/62511#62511
This file contains hidden or 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
| from sklearn.utils import check_arrays | |
| def mean_absolute_percentage_error(y_true, y_pred): | |
| """ | |
| Use of this metric is not recommended; for illustration only. | |
| See other regression metrics on sklearn docs: | |
| http://scikit-learn.org/stable/modules/classes.html#regression-metrics | |
| Use like any other metric | |
| >>> y_true = [3, -0.5, 2, 7]; y_pred = [2.5, -0.3, 2, 8] | |
| >>> mean_absolute_percentage_error(y_true, y_pred) | |
| Out[]: 24.791666666666668 | |
| """ | |
| y_true, y_pred = check_arrays(y_true, y_pred) | |
| ## Note: does not handle mix 1d representation | |
| #if _is_1d(y_true): | |
| # y_true, y_pred = _check_1d_array(y_true, y_pred) | |
| return np.mean(np.abs((y_true - y_pred) / y_true)) * 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment