RuntimeWarning: Divide by Zero error: How to avoid? PYTHON, NUMPY

前端 未结 5 1612
遇见更好的自我
遇见更好的自我 2020-12-10 08:10

I am running in to RuntimeWarning: Invalid value encountered in divide

 import numpy
 a = numpy.random.rand((1000000, 100))
 b = numpy.random.rand((1,100))
          


        
5条回答
  •  执笔经年
    2020-12-10 08:29

    you can ignore warings with the np.errstate context manager and later replace nans with what you want:

    import numpy as np
    angle = np.arange(-5., 5.) 
    norm = np.arange(10.)
    with np.errstate(divide='ignore'):
        print np.where(norm != 0., angle / norm, -2)
    # or:
    with np.errstate(divide='ignore'):
        res = angle/norm
    res[np.isnan(res)] = -2
    

提交回复
热议问题