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?
Advertisement
Answer
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.
JavaScript
x
10
10
1
X = np.random.randn(1e3, 5)
2
X[np.abs(X)< .1]= 0 # some zeros
3
X = np.ma.masked_equal(X,0)
4
plt.boxplot(X) #masked values are not plotted
5
6
#other functionalities of masked arrays
7
X.compressed() # get normal array with masked values removed
8
X.mask # get a boolean array of the mask
9
X.mean() # it automatically discards masked values
10