set very low values to zero in numpy

前端 未结 6 561
傲寒
傲寒 2020-12-06 04:16

In numpy I have an array like

[0 +  0.5j, 0.25 + 1.2352444e-24j, 0.25+ 0j, 2.46519033e-32 + 0j] 

what is the fastest and easiest way to se

6条回答
  •  天涯浪人
    2020-12-06 04:34

    You can also use the numpy.isclose method:

    >>> np.isclose([1e10,1e-7], [1.00001e10,1e-8])
    array([True, False])
    

    By asking if it is close to zero, it should work:

    >>> np.isclose([1e10,0], [1.00001e-10,0])
    array([False, True])
    

    You can customise the atol (absolute tolerance, defaults to 1e-08) and the rtol (relative tolerance, defaults to 1e-05) parameters. You can then set rtol=0 to only use the absolute tolerance.

提交回复
热议问题