Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete specific values from numpy ndarray

Tags:

python

numpy

I have a numpy ndarray that looks like:

[[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[225 224 228],
[163 164 174],
[205 212 229],
[116 130 153],
[ 81 101 132],
[ 34  56  92],
[  2  16  35],
[ 33  44  64],
[ 38  49  71],
[ 63  75  99],
[ 76  88 116],
[ 45  62  95],
[ 29  50  88],
[ 18  40  82],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0],
[  0   0   0]]

I want to delete all the zero elements i.e. [0,0,0]. How can I do this?

like image 261
nish Avatar asked Feb 04 '23 23:02

nish


1 Answers

You can use np.delete() and np.where() to get the elements where the condition is satisfied as :

del_arr = np.delete(arr, np.where(arr == [0, 0, 0]), axis=0)
print del_arr
like image 123
ZdaR Avatar answered Feb 08 '23 14:02

ZdaR