Skip to content
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

fowlkes_mallows_score returns RuntimeWarning when variables get too big #9515

Closed
manhdao opened this issue Aug 10, 2017 · 6 comments
Closed

fowlkes_mallows_score returns RuntimeWarning when variables get too big #9515

manhdao opened this issue Aug 10, 2017 · 6 comments

Comments

@manhdao
Copy link

@manhdao manhdao commented Aug 10, 2017

Description

sklearn\metrics\cluster\supervised.py:859 return tk / np.sqrt(pk * qk) if tk != 0. else 0.
This line produces RuntimeWarning: overflow encountered in int_scalars when (pk * qk) is bigger than 2**32, thus bypassing the int32 limit.

Steps/Code to Reproduce

Any code when pk and qk gets too big.

Expected Results

Be able to calculate tk / np.sqrt(pk * qk) and return a float.

Actual Results

it returns 'nan' instead.

Fix

I propose to use np.sqrt(tk / pk) * np.sqrt(tk / qk) instead, which gives same result and ensuring not bypassing int32

Versions

0.18.1

@jnothman
Copy link
Member

@jnothman jnothman commented Aug 10, 2017

@manhdao
Copy link
Author

@manhdao manhdao commented Aug 10, 2017

At the moment I'm comparing several clustering results with the fowlkes_mallows_score, so precision isn't my concern. Sorry i'm not in a position to rigorously test the 2 approaches.

@jnothman
Copy link
Member

@jnothman jnothman commented Aug 10, 2017

@amueller
Copy link
Member

@amueller amueller commented Aug 11, 2017

or code to reproduce?

@aliddell
Copy link
Contributor

@aliddell aliddell commented Mar 20, 2018

I just ran into this and it looks similar to another issue in the same module (which I also ran into). The PR converts to int64 instead. I tested both on 4.1M pairs of labels and the conversion to int64 is slightly faster with less variance:

%timeit sklearn.metrics.fowlkes_mallows_score(labels_true, labels_pred, sparse=False)
726 ms ± 3.83 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

for the int64 conversion vs.

%timeit sklearn.metrics.fowlkes_mallows_score(labels_true, labels_pred, sparse=False)
739 ms ± 7.57 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

for the float conversion.

diff --git a/sklearn/metrics/cluster/supervised.py b/sklearn/metrics/cluster/supervised.py
index a987778ae..43934d724 100644
--- a/sklearn/metrics/cluster/supervised.py
+++ b/sklearn/metrics/cluster/supervised.py
@@ -856,7 +856,7 @@ def fowlkes_mallows_score(labels_true, labels_pred, sparse=False):
     tk = np.dot(c.data, c.data) - n_samples
     pk = np.sum(np.asarray(c.sum(axis=0)).ravel() ** 2) - n_samples
     qk = np.sum(np.asarray(c.sum(axis=1)).ravel() ** 2) - n_samples
-    return tk / np.sqrt(pk * qk) if tk != 0. else 0.
+    return tk / np.sqrt(pk.astype(np.int64) * qk.astype(np.int64)) if tk != 0. else 0.


 def entropy(labels):

Shall I submit a PR?

@jnothman
Copy link
Member

@jnothman jnothman commented Mar 20, 2018

Sure, PR welcome. But perhaps we should allow contingency_matrix to return int64 by default (I'm not certain if we need a deprecation cycle) or make it an option, to avoid this pitfall in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants