Home > Back-end >  Python 3 gives NaN for operation that Python 2 handles
Python 3 gives NaN for operation that Python 2 handles

Time:02-15

The calculation I'm trying with scipy.stats.hypergeom is giving nan when run in Python 3.9.9 but works fine in Python 2.7.18. I need it to run it in Python 3 because the rest of my program is for that version.

I've tried casting all of the numbers to the same float type and rounding the numbers but nothing seems to make it work for Python 3. I've included my code and the outputs I get for the different versions.

Code

from scipy.stats import hypergeom

k   = 526.8499999999999
nmk = 409.823
n   = 936.6729999999999
m   = 30079.555399999997
N   = 59416.2896

p1 = hypergeom.cdf(k, N, m, n)
p2 = hypergeom.cdf(nmk, N, m, n)

print("p1 = "   str(p1)   " ["   str(type(p1))   "]")
print("p2 = "   str(p2)   " ["   str(type(p2))   "]")

Python 2.7.18

p1 = 0.999720238917 [<type 'numpy.float64'>]
p2 = 9.96805760427e-06 [<type 'numpy.float64'>]

Python 3.9.9

p1 = nan [<class 'numpy.float64'>]
p2 = nan [<class 'numpy.float64'>]

Edit: The scipy version I'm using is 0.13.0b1 for Python 2 and 1.8.0 for Python 3.

CodePudding user response:

Downgrading scipy from version 1.8.0 to 1.7.3 solved this issue.

  • Related