[Numpy-discussion] Setting small numbers to zero.

Alan McIntyre alan.mcintyre@gmail....
Wed Mar 17 10:55:34 CDT 2010


On Wed, Mar 17, 2010 at 8:51 AM, gerardob <gberbeglia@gmail.com> wrote:
> How can i modified all the values of a numpy array whose value is smaller
> than a given epsilon to zero?
>
> Example
> epsilon=0.01
> a = [[0.003,2][23,0.0001]]
>
> output:
> [[0,2][23,0]]

Give this a try:

>>> import numpy as np
>>> epsilon=0.01
>>> a=np.array([[0.003,2],[23,0.0001]])
>>> a
array([[  3.00000000e-03,   2.00000000e+00],
       [  2.30000000e+01,   1.00000000e-04]])
>>> a[np.abs(a) < epsilon] = 0
>>> a
array([[  0.,   2.],
       [ 23.,   0.]])


More information about the NumPy-Discussion mailing list