-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
Added mean_absolute_percentage_error in metrics fixes #10708 #15007
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
Changes from all commits
Commits
Show all changes
120 commits
Select commit
Hold shift + click to select a range
eb70678
Added mean_absolute_percentage_error in metrics
ashutosh1919 0fca06e
Added mean_absolute_percentage_error in metrics
ashutosh1919 7191b88
Added mean_absolute_percentage_error in metrics
ashutosh1919 140afe2
Added MAPE
ashutosh1919 41c1bd1
Added MAPE
ashutosh1919 a401965
Added MAPE
ashutosh1919 39a7af0
Added MAPE
ashutosh1919 0aa9b53
Added MAPE
ashutosh1919 b8f5187
MAPE implementation changed
ashutosh1919 f83fdf4
MAPE implementation changed
ashutosh1919 6b2ead2
Removed Clip and applied np.maximum
ashutosh1919 2c7c8a5
MAPE Added in Docs
ashutosh1919 65afa12
Changed model_evaluation descriptions and other changes
ashutosh1919 99d080d
Resolving error
ashutosh1919 dc988ae
model_evaluation table changed
ashutosh1919 8274e28
model_evaluation table changed
ashutosh1919 647ec2c
model_evaluation table changed
ashutosh1919 4f7ea9c
Merge remote-tracking branch 'upstream/master'
ashutosh1919 832ac40
Merge branch 'master' of https://github.com/ashutosh1919/scikit-learn
ashutosh1919 0f53d4f
Changed Doc line
ashutosh1919 ffe95c9
Merge remote-tracking branch 'upstream/master'
ashutosh1919 da326fe
Sync with remote
ashutosh1919 cdb5d09
metrics init file changed
ashutosh1919 3ec0dd8
test_regression resolved
ashutosh1919 cf54616
test_regression resolved
ashutosh1919 65b14ce
Resolving Forecasting text
ashutosh1919 b4d7336
Render error
ashutosh1919 f9e6c01
Merge remote-tracking branch 'upstream/master'
ashutosh1919 76a4bf9
Resolving render errors
ashutosh1919 98bed82
Render doc error
ashutosh1919 9e0a347
Resolving render error
ashutosh1919 545bacf
Resolving render doc
ashutosh1919 ba0b63b
Resolving render doc
ashutosh1919 b731255
Resolving render doc
ashutosh1919 197f576
Update doc/modules/model_evaluation.rst
ashutosh1919 5869dda
Update sklearn/metrics/_regression.py
ashutosh1919 1adc635
Update sklearn/metrics/_regression.py
ashutosh1919 5f25b68
Applying suggested changes
ashutosh1919 b3eaca1
Merge remote-tracking branch 'upstream/master'
ashutosh1919 c28a2f5
Applying suggested changes
ashutosh1919 5444ac6
Applying suggested changes
ashutosh1919 5dbe07d
Applying suggested changes
ashutosh1919 90f7533
Applying suggested changes
ashutosh1919 5897097
Applying suggested changes
ashutosh1919 00665b4
Merge remote-tracking branch 'upstream/master'
ashutosh1919 3962b9a
Update doc/modules/model_evaluation.rst
ashutosh1919 aa8f6ec
Update doc/modules/model_evaluation.rst
ashutosh1919 fdc197f
Update doc/modules/model_evaluation.rst
ashutosh1919 94b6b5b
Update doc/modules/model_evaluation.rst
ashutosh1919 595a0c2
Update doc/modules/model_evaluation.rst
ashutosh1919 a64e5ff
Update doc/modules/model_evaluation.rst
ashutosh1919 1097911
Update doc/modules/model_evaluation.rst
ashutosh1919 9782f34
Doc Too long line error resolved
ashutosh1919 27be674
Merge remote-tracking branch 'upstream/master'
ashutosh1919 dbc2c4a
datatype changed and made compatible to y_true
ashutosh1919 e4cd050
Added scorer
ashutosh1919 c7a8b5f
eps datatype changed to np.float64
ashutosh1919 d406081
test_regression.py is changed to more meaningful test cases
ashutosh1919 5db5f5e
test_regression.py is changed to more meaningful test cases
ashutosh1919 f4cfc22
Resolving errors related to scorer tests in test_common.py and _score…
ashutosh1919 b681dcb
Updated v0.23.rst in whats_new
ashutosh1919 d4fcc39
resolving errors of mape scorer
ashutosh1919 9261cee
resolving errors of mape scorer
ashutosh1919 a161f79
resolving errors of mape scorer
ashutosh1919 d102f86
resolving errors of mape scorer
ashutosh1919 1970a30
modified test case in model_evaluation.rst
ashutosh1919 2b4128d
modified doc and code as per second batch comments
ashutosh1919 c6c7ba9
Resolving r2_scorer object error
ashutosh1919 2a59c17
Resolving r2_scorer object error
ashutosh1919 af63de8
Merge remote-tracking branch 'upstream/master'
ashutosh1919 67f857e
Resolving r2_scorer object error
ashutosh1919 cb0a635
Resolving r2_scorer object error
ashutosh1919 d104f2c
Conflict Resolved
ashutosh1919 a03affe
Added changes of conflict
ashutosh1919 f9fe4a4
Update sklearn/metrics/_regression.py
ashutosh1919 723f116
Update sklearn/metrics/_regression.py
ashutosh1919 156e633
Update sklearn/metrics/_regression.py
ashutosh1919 92af06b
Update sklearn/metrics/_regression.py
ashutosh1919 dd709a9
Modified Files to optimize changes
ashutosh1919 9151167
Merge remote-tracking branch 'upstream/master'
ashutosh1919 ea99cff
Modified Files to optimize changes
ashutosh1919 2c06403
Changed description of contributors info
ashutosh1919 019659d
Merge branch 'master' into master
ogrisel 291720e
DOC improve diabetes dataset description (#16534)
maikia 6578fb4
TST add test of fit attributes (#16286)
agramfort b91501e
ENH Minimal Generalized linear models implementation (L2 + lbfgs) (#1…
rth 4905ac3
FIX Adress decomposition.PCA mle option problem (#16224)
lschwetlick fdbff6c
DOC add 0.22.2 in website news (#16631)
jeremiedbb 43293a6
TST Enable california_housing pandas test in cron job (#16547)
rth 0271b76
EXA align lorenz curves between the two examples with GLMs (#16640)
rth f7dfe4d
DOC update n_jobs description in DBSCAN (#16615)
adrinjalali 2131504
FIX Pass sample_weight when predicting on stacked folds (#16539)
68d1fef
BLD Turns off memory_profiler in examples to fix CircleCI (#16629)
thomasjpfan a5a82ab
BLD Updates osx vm image in azure pipelines (#16647)
thomasjpfan 9c17a60
FIX: normalizer l_inf should take maximum of absolute values (#16633)
maurapintor 16f4208
ENH Add check for non binary variables in OneHotEncoder. (#16585)
cmarmo d7fbef0
DOC Update LICENSE Year (#16660)
merrcury 8c8383b
BUG Fix issue with KernelPCA.inverse_transform (#16655)
lrjball 895cb6a
[MRG] DOC Add example about interpretation of coefficients of linear …
cmarmo c464e92
MNT Remove unused imports (#16665)
alexhenrie 72f39d9
MNT Restores behavior of conditioning on linting for most instances (…
thomasjpfan 7cf9e1e
BUG Fixes HistGradientBoosting when warm_start is on + early_stopping…
thomasjpfan b2b4dbb
BUG fix the math issue in latex compilation (#16673)
glemaitre ba3fcdb
BUG remove $ math env due to latex error (#16674)
glemaitre 864d028
DOC add example to tree.ExtraTreeClassifier (#16671)
nilichen dfdda83
PEP8 in test_encoders.py
ogrisel 3686d55
MNT Removes unused private attributes (#16675)
thomasjpfan 77fb39d
CI Check for unused imports when linting (#16678)
rth e087ea7
DOC wording in linear model interpretation example (#16680)
GaelVaroquaux 140dae4
API make __init__ params in cross_decomposition kw-only (#16682)
adrinjalali dd437aa
DOC Adds example to OAS (#16681)
marenwestermann 7ec9c61
DOC Add note on bias induced by dropping categories in OneHotE… (#16679)
ogrisel d3e7041
API make __init__ params in compose module kw-only (#16542)
adrinjalali a64758e
Merge remote-tracking branch 'upstream/master'
ashutosh1919 9b1cdc9
Removed x100 from MAPE and modified tests too
ashutosh1919 014a005
Merge remote-tracking branch 'upstream/master'
ashutosh1919 4aeb1dc
Merge remote-tracking branch 'upstream/master'
ashutosh1919 297ce25
Changed range
ashutosh1919 5a8476a
Merge remote-tracking branch 'upstream/master' into ashutosh1919-master
rth 7b2c86f
DOC Add absolute_percentage_error to doc/modules/model_evaluation.rst
rth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
# Michael Eickenberg <[email protected]> | ||
# Konstantin Shmelkov <[email protected]> | ||
# Christian Lorentzen <[email protected]> | ||
# Ashutosh Hathidara <[email protected]> | ||
# License: BSD 3 clause | ||
|
||
import numpy as np | ||
|
@@ -41,6 +42,7 @@ | |
"mean_squared_error", | ||
"mean_squared_log_error", | ||
"median_absolute_error", | ||
"mean_absolute_percentage_error", | ||
"r2_score", | ||
"explained_variance_score", | ||
"mean_tweedie_deviance", | ||
|
@@ -192,6 +194,81 @@ def mean_absolute_error(y_true, y_pred, *, | |
return np.average(output_errors, weights=multioutput) | ||
|
||
|
||
def mean_absolute_percentage_error(y_true, y_pred, | ||
sample_weight=None, | ||
multioutput='uniform_average'): | ||
"""Mean absolute percentage error regression loss | ||
|
||
ashutosh1919 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Note here that we do not represent the output as a percentage in range | ||
[0, 100]. Instead, we represent it in range [0, 1/eps]. Read more in the | ||
:ref:`User Guide <mean_absolute_percentage_error>`. | ||
|
||
Parameters | ||
---------- | ||
y_true : array-like of shape (n_samples,) or (n_samples, n_outputs) | ||
Ground truth (correct) target values. | ||
|
||
y_pred : array-like of shape (n_samples,) or (n_samples, n_outputs) | ||
Estimated target values. | ||
|
||
sample_weight : array-like of shape (n_samples,), default=None | ||
Sample weights. | ||
|
||
multioutput : {'raw_values', 'uniform_average'} or array-like | ||
Defines aggregating of multiple output values. | ||
Array-like value defines weights used to average errors. | ||
If input is list then the shape must be (n_outputs,). | ||
|
||
'raw_values' : | ||
Returns a full set of errors in case of multioutput input. | ||
|
||
'uniform_average' : | ||
Errors of all outputs are averaged with uniform weight. | ||
|
||
Returns | ||
------- | ||
loss : float or ndarray of floats in the range [0, 1/eps] | ||
If multioutput is 'raw_values', then mean absolute percentage error | ||
is returned for each output separately. | ||
If multioutput is 'uniform_average' or an ndarray of weights, then the | ||
weighted average of all output errors is returned. | ||
|
||
MAPE output is non-negative floating point. The best value is 0.0. | ||
ashutosh1919 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
But note the fact that bad predictions can lead to arbitarily large | ||
MAPE values, especially if some y_true values are very close to zero. | ||
ashutosh1919 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Note that we return a large value instead of `inf` when y_true is zero. | ||
|
||
Examples | ||
-------- | ||
>>> from sklearn.metrics import mean_absolute_percentage_error | ||
>>> y_true = [3, -0.5, 2, 7] | ||
>>> y_pred = [2.5, 0.0, 2, 8] | ||
>>> mean_absolute_percentage_error(y_true, y_pred) | ||
0.3273... | ||
>>> y_true = [[0.5, 1], [-1, 1], [7, -6]] | ||
>>> y_pred = [[0, 2], [-1, 2], [8, -5]] | ||
>>> mean_absolute_percentage_error(y_true, y_pred) | ||
0.5515... | ||
>>> mean_absolute_percentage_error(y_true, y_pred, multioutput=[0.3, 0.7]) | ||
ashutosh1919 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
0.6198... | ||
""" | ||
y_type, y_true, y_pred, multioutput = _check_reg_targets( | ||
y_true, y_pred, multioutput) | ||
check_consistent_length(y_true, y_pred, sample_weight) | ||
epsilon = np.finfo(np.float64).eps | ||
mape = np.abs(y_pred - y_true) / np.maximum(np.abs(y_true), epsilon) | ||
output_errors = np.average(mape, | ||
weights=sample_weight, axis=0) | ||
if isinstance(multioutput, str): | ||
if multioutput == 'raw_values': | ||
return output_errors | ||
elif multioutput == 'uniform_average': | ||
# pass None as weights to np.average: uniform mean | ||
multioutput = None | ||
|
||
return np.average(output_errors, weights=multioutput) | ||
|
||
|
||
@_deprecate_positional_args | ||
def mean_squared_error(y_true, y_pred, *, | ||
sample_weight=None, | ||
|
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.