+2 votes
in Programming Languages by (73.8k points)

I am getting "RuntimeWarning: invalid value encountered in long_scalars" error in the following code. What is wrong?

>>> a1
[1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1]
>>> np.sum(a1==1)/np.sum(a1==0)
__main__:1: RuntimeWarning: invalid value encountered in long_scalars

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

Since the variable 'a1' is not a Numpy array, both np.sum(a1==1) and np.sum(a1==0) are 0. You are doing 0/0 and that's why you are getting the error. To fix the error, convert 'a1' to a Numpy array.

See the following code:

>>> a1
[1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1]
>>> a1=np.array(a1)
>>> np.sum(a1==1)/np.sum(a1==0)
1.4


...