Skip to content

FIX RuntimeWarning by dividing by zero in test_iforest_with_uniform_data #19622

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

Conversation

LSturtew
Copy link
Contributor

@LSturtew LSturtew commented Mar 5, 2021

Reference Issues/PRs

#19334

What does this implement/fix? Explain your changes.

The runtime error was in the test on a single row of data. The zero division error was due to the fact that the average path length between points was 0. It does not make sense to me to use the isolation forest algorithm on 1d data.

Any other comments?

@LSturtew LSturtew marked this pull request as ready for review March 5, 2021 11:57
@LSturtew LSturtew changed the title This was referenced 25 days ago Fix: RuntimeWarning by dividing by zero in test_iforest_with_uniform_data Fix: RuntimeWarning by dividing by zero in test_iforest_with_uniform_data Mar 5, 2021
@lorentzenchr
Copy link
Member

@LSturtew Thanks for opening this PR. I’m very glad that you found an issue to work on.

Does the RuntimeWarning stem from fit or predict?

@LSturtew
Copy link
Contributor Author

LSturtew commented Mar 5, 2021

It stems from predict.

@lorentzenchr
Copy link
Member

lorentzenchr commented Mar 9, 2021

I'd prefer to fix the cause of the runtime warning instead of removing the tests.

@LSturtew
Copy link
Contributor Author

@lorentzenchr I have updated the score calculations to fix the runtime warning.

Comment on lines 457 to 458
-np.divide(depths, denominator, np.ones_like(depths),
where=denominator != 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
-np.divide(depths, denominator, np.ones_like(depths),
where=denominator != 0)
# For a single training sample, denominator and depth are 0.
# Therefore, we set the score manually to 1.
-np.divide(depths, denominator, out=np.ones_like(depths),
where=denominator != 0)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the keyword argument explicitly, i.e. out=np.ones_like(...).

Copy link
Member

@jeremiedbb jeremiedbb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks @LSturtew !

Copy link
Member

@lorentzenchr lorentzenchr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM too. @LSturtew Thank you.

@lorentzenchr lorentzenchr changed the title Fix: RuntimeWarning by dividing by zero in test_iforest_with_uniform_data FIX RuntimeWarning by dividing by zero in test_iforest_with_uniform_data Mar 15, 2021
@lorentzenchr lorentzenchr merged commit 302106b into scikit-learn:main Mar 15, 2021
@LSturtew LSturtew deleted the fix_runtimewarning_test_iforest_with_uniform_data branch March 15, 2021 19:39
marrodion pushed a commit to marrodion/scikit-learn that referenced this pull request Mar 17, 2021
@glemaitre glemaitre mentioned this pull request Apr 22, 2021
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants