mario119
mario119

Reputation: 353

Delete some rows of a numpy array

I have a numpy array like below


cf = 
[[ 0.06605101 -0.37910558]
 [ 0.01950959 -0.13871163]
 [-0.07609168  0.35762712]
 [-0.10962792  0.53259178]
 [-0.20441798  1.02187988]
 [-0.27493986  1.3927189 ]
 [-0.32651418  1.66157985]
 [ 0.1344195  -0.73359827]
 [ 0.          0.        ]
 [ 0.          0.        ]
 [ 0.          0.        ]
 [ 0.          0.        ]
 [ 0.          0.        ]
 [ 0.          0.        ]
 [ 0.          0.        ]
 [ 0.          0.        ]
 [ 0.          0.        ]
 [ 0.          0.        ]
 [-0.01140529  0.02146107]
 [-0.14210564  0.70305015]
 [ 0.19425714 -1.04428677]
 [ 0.21070736 -1.13055805]
 [ 0.24264512 -1.29770194]
 [ 0.2739207  -1.45405194]
 [ 0.34871618 -1.84201387]
 [ 0.41549682 -2.18784216]
 [ 0.48779434 -2.56516974]
 [ 0.61753187 -3.22472257]
 [ 0.62543066 -3.29968867]
 [ 0.67363223 -3.51593344]
 [ 0.67156065 -3.50685949]
 [ 0.67066598 -3.5027474 ]
 [ 0.61698089 -3.20216463]
 [ 0.33951472 -1.80812563]
 [ 0.16105593 -0.88319653]]

But I would like to delete rows that values are [ 0. 0. ].

To do that, my code is

for idx in range(cf.shape[0]):
    if cf[idx,0] == 0 and cf[idx,1] == 0 :
        np.delete(cf,idx,0)

But cf is nothing changed. What is the problem..? Are the [ 0. 0. ] values not exactly zero?

Upvotes: 1

Views: 80

Answers (1)

adrianp
adrianp

Reputation: 1019

Take advantage of numpy's vectorized methods. Say your array is a:

trimmed = cf[(cf != 0).any(axis=1)]

Upvotes: 3

Related Questions