Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove all zero elements from a NumPy array?

I have a rank-1 numpy.array of which I want to make a boxplot. However, I want to exclude all values equal to zero in the array. Currently, I solved this by looping the array and copy the value to a new array if not equal to zero. However, as the array consists of 86 000 000 values and I have to do this multiple times, this takes a lot of patience.

Is there a more intelligent way to do this?

like image 839
ruben baetens Avatar asked May 08 '11 11:05

ruben baetens


People also ask

How do you clear an entire numpy array?

Using the NumPy function np. delete() , you can delete any row and column from the NumPy array ndarray . Specify the axis (dimension) and position (row number, column number, etc.). It is also possible to select multiple rows and columns using a slice or a list.

How do you replace all missing values with 0 in a numpy array?

If you want to replace NaN with 0 : a[np. isnan(a)] = 0 print(a) # [[11. 12.


2 Answers

For a NumPy array a, you can use

a[a != 0] 

to extract the values not equal to zero.

like image 144
Sven Marnach Avatar answered Sep 23 '22 04:09

Sven Marnach


This is a case where you want to use masked arrays, it keeps the shape of your array and it is automatically recognized by all numpy and matplotlib functions.

X = np.random.randn(1e3, 5) X[np.abs(X)< .1]= 0 # some zeros X = np.ma.masked_equal(X,0) plt.boxplot(X) #masked values are not plotted  #other functionalities of masked arrays X.compressed() # get normal array with masked values removed X.mask # get a boolean array of the mask X.mean() # it automatically discards masked values 
like image 41
Andrea Zonca Avatar answered Sep 22 '22 04:09

Andrea Zonca