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

前端 未结 5 1620
遇见更好的自我
遇见更好的自我 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:18

    You want to be using np.where. See the documentation.

    angles = np.where(norms != 0, dots/norms, -2)
    

    Angles will consist of downs/norms whenever norms != 0, and will be -2 otherwise. You will still get the RuntimeWarning, as np.where will still calculate the entire vector dots/norms internally, but you can safely ignore it.

提交回复
热议问题