2017-03-02 216 views
1

删除特定值有一个numpy的ndarray看起来像:我如何从numpy的ndarray

[[ 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]] 

我想删除所有即[0,0,0]零个元素。我怎样才能做到这一点?

+1

的[我如何从删除行可能的复制基于多个条件的numpy数组?](http://stackoverflow.com/questions/25380255/how-do-i-remove-rows-from-a-numpy-array-based-on-multiple-conditions) – languitar

回答

2

您可以使用np.delete()np.where()来获取元素,其中的条件满足:

del_arr = np.delete(arr, np.where(arr == [0, 0, 0]), axis=0) 
print del_arr 
0

试试这个:

import numpy as np 

array = np.array([ 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]) 


zeros = np.all(np.equal(a, 0), axis=1) 
array[~zeros] 
0
data[(data[:,1] & data[:, 2] & data[:, 0]).astype(bool)]