Replace Negative Values in an Numpy Array

Replace negative values in an numpy array

You are halfway there. Try:

In [4]: a[a < 0] = 0

In [5]: a
Out[5]: array([1, 2, 3, 0, 5])

Replace numpy ndarry negative elements with 0 (zero)

You can use np.clip for this, clipping between zero and infinity:

arr = np.array([[2, -7, 5], [-6, 2, 0], [1, -4, 2], [-2, 6, 8]])

np.clip(arr, a_min = 0, a_max = np.inf)

array([[2., 0., 5.],
[0., 2., 0.],
[1., 0., 2.],
[0., 6., 8.]])

Otherwise, you can use something like this (note this changes the array in place):

arr[arr <= 0] = 0

>>> arr
array([[2, 0, 5],
[0, 2, 0],
[1, 0, 2],
[0, 6, 8]])

Python removing all negative values in array

In [2]: x[x >= 0]
Out[2]: array([ 0. , 1.2, 2.2, 3.1, 4.4, 8.3, 9.9, 10. , 14. , 16.2])

regarding efficiently modifying entry values in an numpy array without writing iteration explicitly

You can use boolean indexing:

A[A<0]=0

What is the fastest way to replace negative values with 0 and values greater than 1 with 1 in an array using Python?

You want to use np.clip:

>>> import numpy as np
>>> list_values = [-0.01, 0, 0.5, 0.9, 1.0, 1.01]
>>> arr = np.array(list_values)
>>> np.clip(arr, 0.0, 1.0)
array([0. , 0. , 0.5, 0.9, 1. , 1. ])

This is likely the fastest approach, if you can ignore the cost of converting to an array. Should be a lot better for larger lists/arrays.

Involving pandas in this operation isn't the way to go unless you eventually want a pandas data structure.

How to replace all negative numbers in an array with zero

You can mask the entire array by using a boolean mask, this will be much more efficient than iterating like you currently are:

In [41]:
array = np.random.randn(5,3)
array

Out[41]:
array([[-1.09127791, 0.51328095, -0.0300959 ],
[ 0.62483282, -0.78287353, 1.43825556],
[ 0.09558515, -1.96982215, -0.58196525],
[-1.23462258, 0.95510649, -0.76008193],
[ 0.22431534, -0.36874234, -1.46494516]])

In [42]:
array[array < 0] = 0
array

Out[42]:
array([[ 0. , 0.51328095, 0. ],
[ 0.62483282, 0. , 1.43825556],
[ 0.09558515, 0. , 0. ],
[ 0. , 0.95510649, 0. ],
[ 0.22431534, 0. , 0. ]])

Make negative values of numpy array positive

Simply use the builtin abs function:

>>> a = np.array( [6, -1, -3, -5])
>>> a
array([ 6, -1, -3, -5])

>>> abs(a)
array([6, 1, 3, 5])

How to transform negative elements to zero without a loop?

a = a.clip(min=0)

TypeError in numpy array when trying to replace negative numbers with 0

If you want to replace all the negative numbers by zero just do:

my_array = np.clip(my_array, 0, np.inf)

If you only want to do it in a specific column:

my_array[:, col] = np.clip(my_array[:, col], 0, np.inf)

Note: you can transpose your numpy array with my_array.T instead of zip(*my_array), and the zip produces the tuples that you found out...

When you need to transpose the results from np.loadtxt(), just pass unpack=True and you get the same result:

my_array = np.loadtxt(cw.my_Fname, delimiter=',', skiprows=12,            
usecols=necessary_data_columns, unpack=True)


Related Topics



Leave a reply



Submit