question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mean_squared_log_error - accepts targets with negative values

See original GitHub issue

Description

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:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
loretoparisicommented, Mar 16, 2020

[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 like

cat requirements.txt | xargs -n 1 -L 1 pip install

so 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 having

req == 0.21.3

the code now works not using the 0.22.1.

Hope this helps.

1reaction
jnothmancommented, Mar 17, 2020

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pre-process the data to calculate Root Mean Squared ...
There is no standard form that allows negative values because the log of a negative number is undefined. You either have to transform...
Read more >
Mean Squared Logarithmic Error cannot be used when targets ...
ValueError: Mean Squared Logarithmic Error cannot be used when targets contain negative values. even though my target vector has no negative values (house ......
Read more >
machine learning - Can the mean squared error be negative?
A standard Mean Squared Error function cannot be negative. The lowest possible value is 0, when there is no output error from any...
Read more >
Metrics - MSE, R^2, RMSLE - Data to Wisdom
It is the Root Mean Squared Error of the log-transformed predicted and log-transformed actual values. · RMSLE adds 1 to both actual and...
Read more >
Evaluation Metric for Regression Models - Analytics Vidhya
Root Mean Squared Logarithmic Error is calculated by applying log to the actual and the predicted values and then taking their differences.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found