Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scipy.stats.spearmanr: Warning: divide by zero encountered in divide

Tags:

python

scipy

I use scipy.stats.spearmanr(a,b) and I get:

Warning: divide by zero encountered in divide

The operation ends correctly, but the warning is displayed. Both a and b are "normal" data, (no "zero-only" vectors etc.). Any idea what is the cause, or how to suspend the warning?

EDIT:

This is the offending line in spearmanr:

/usr/lib/python2.7/dist-packages/scipy/stats/stats.pyc in spearmanr(a, b, axis)
   2226     rs = np.corrcoef(ar,br,rowvar=axisout)
   2227 
-> 2228     t = rs * np.sqrt((n-2) / ((rs+1.0)*(1.0-rs)))
   2229     prob = distributions.t.sf(np.abs(t),n-2)*2
   2230 
like image 377
Jakub M. Avatar asked Nov 17 '25 09:11

Jakub M.


1 Answers

division by zero is by design, rs=1 on diagonal. This happens for any values.

However, in scipy 0.9 this error has been silenced locally within the spearmanr function. The corresponding sourceline contains

np.seterr(divide='ignore') # rs can have elements equal to 1

like image 57
Josef Avatar answered Nov 19 '25 23:11

Josef