You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
roc_auc_score computation is wrong for large samples #6842
Hi, we've recently found some strange inconsistency in the behavior of roc_auc_score. After long digging @tata-antares found out that it is wrongly computed for large data samples if weights passed are float32.
The value when dtype=float64 is correct, but in the case we investigated after passing float32 metric returned a regular number, which is very different from correct one (e.g 0.58 instead of 0.52). It is some occasion we detected this problem.
I can confirm getting "UndefinedMetricWarning: No negative samples in y_true, false positive value should be meaningless" but I'm not able to replicate the nan. Is there a reason you did not report the rest of your platform details?
Cumsum is losing precision, even when a float32 sum does not. This might be a numpy bug. If so, it lies in np.add.accumulate:
In [10]: np.add.accumulate(trivial_weight.astype('float32'))[-1]
Out[10]: 16777216.0
We can solve here by always passing dtype='float64' to cumsum, or doing so if the number of samples is high or something. There might also be a way to do cumsum in batches in a way that reduces the issue. Certainly we can identify the error by doing the cumsum and checking if its final value is equal to the sum.
@jnothman ah ok, sorry I didn't look super closely into it; I just remembered seeing those other two threads and xref'd them here. thanks for letting me know.
@jnothman
it's not a bug, it is an expected behavior, since this code
x = trivial_weight
for i in range(len(x) - 1):
x[i + 1] += x[i]
gives the same (wrong) result.
np.sum is a different story — one can change order of summation there in any possible way.
As for np.cumsum, this will drive to doubling number of computations (even for optimal implementation), so I don't think numpy team will do this (at least till the moment they need multithreading for np.cumsum).
Is there a way to test this? Would we better off with a (somewhat costly) helper to insure stability even in the float64 case? Like:
defstable_cumsum(arr):
out=np.cumsum(arr, dtype=np.float64)
expected=np.sum(arr, dtype=np.float64)
ifnotnp.allclose(out, expected):
raiseRuntimeError('cumsum was found to be unstable: its results do not correspond to sum')
returnout
Activity
nelson-liu commentedon May 29, 2016
maybe related to issues # 6711 and 3864?Nevermind it's not.arogozhnikov commentedon May 29, 2016
#3864 is due to clipping, may be related to #6711, but seems no weights there.
jnothman commentedon May 31, 2016
I can confirm getting "UndefinedMetricWarning: No negative samples in y_true, false positive value should be meaningless"
but I'm not able to replicate thenan. Is there a reason you did not report the rest of your platform details?jnothman commentedon May 31, 2016
Scratch that. I do get nan. Just wasn't reading output correctly.
jnothman commentedon May 31, 2016
This is what it comes down to:
Cumsum is losing precision, even when a float32 sum does not. This might be a numpy bug. If so, it lies in
np.add.accumulate:We can solve here by always passing
dtype='float64'to cumsum, or doing so if the number of samples is high or something. There might also be a way to do cumsum in batches in a way that reduces the issue. Certainly we can identify the error by doing thecumsumand checking if its final value is equal to thesum.jnothman commentedon May 31, 2016
@arogozhnikov, please let me know if you report a bug to numpy. Feel free to submit a PR here.
and @nelson-liu, this is entirely independent of those other bugs.
nelson-liu commentedon Jun 1, 2016
@jnothman ah ok, sorry I didn't look super closely into it; I just remembered seeing those other two threads and xref'd them here. thanks for letting me know.
arogozhnikov commentedon Jun 3, 2016
@jnothman
it's not a bug, it is an expected behavior, since this code
gives the same (wrong) result.
np.sumis a different story — one can change order of summation there in any possible way.As for
np.cumsum, this will drive to doubling number of computations (even for optimal implementation), so I don't think numpy team will do this (at least till the moment they need multithreading fornp.cumsum).jnothman commentedon Jun 4, 2016
I don't find your claim to be true:
jnothman commentedon Jun 4, 2016
whoops forgot astype. stupid.
jnothman commentedon Jun 4, 2016
you're right.... though that's really quite nasty.
jnothman commentedon Aug 31, 2016
Would we be happy with a fix that performs cumsum in float64?
amueller commentedon Aug 31, 2016
+1 for cumsum in float64.
arogozhnikov commentedon Aug 31, 2016
Yes, sounds good enough.
jnothman commentedon Aug 31, 2016
Is there a way to test this? Would we better off with a (somewhat costly) helper to insure stability even in the float64 case? Like: