mean_squared_log_error - accepts targets with negative values
See original GitHub issueDescription
I found problem, where mean_squared_log_error() function does not catch error.
Steps/Code to Reproduce
Imagine simple situation, where regression model predicts negative value:
import numpy as np
y_true = np.array([1, 2, 3])
y_pred = np.array([1, -2, 3])
mean_squared_log_error(y_true, y_pred)
This happened, when my regression model predicted 1 negative value (among thousands of positive values).
Expected correct behavior:
Expected behavior is, that exception: ValueError(“Mean Squared Logarithmic Error cannot be used when targets contain negative values.”) should be raised and correctly inform about the underlying problem.
Exact location, where is the bug
Problematic code is exactly in sklearn/metrics/regression.py: (around line 313)
if not (y_true >= 0).all() and not (y_pred >= 0).all():
raise ValueError("Mean Squared Logarithmic Error cannot be used when "
"targets contain negative values.")
The condition is not fully correct and evaluates to False for example above - which is wrong. It should evaluate to True, and raise the exception.
Suggested solution:
Just change the condition to:
if (y_true < 0).any() or (y_pred < 0).any():
raise ValueError("Mean Squared Logarithmic Error cannot be used when "
"targets contain negative values.")
so it catches the problem in case any of the y_true or y_pred contain negative value.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
[UPDATE] @amueller We have tested our code and found that actually scikit-learn has this issue again with
0.22.1
. What we did was then to force the installation of requirements one by one likeso that pip will be forced to install
sklearn<0.22.1
, hence the very previous one that was'0.21.3'
. In this way with the requirements havingthe code now works not using the 0.22.1.
Hope this helps.
@loretoparisi the post here is referring to a bug where an error was not being raised that should have been raised. You are showing now that this error is correctly raised. Therefore the bug has been fixed.