set very low values to zero in numpy

前端 未结 6 560
傲寒
傲寒 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 04:18

    To set elements that are less than eps to zero:

    a[np.abs(a) < eps] = 0
    

    There could be a specialized function that is more efficient.

    If you want to suppress printing of small floats instead:

    import numpy as np
    a = np.array([1+1e-10j])
    print a # -> [ 1. +1.00000000e-10j]
    
    np.set_printoptions(suppress=True)
    print a # -> [ 1.+0.j]
    

提交回复
热议问题