如何从python中的numpy 2D矩阵中删除行?

时间:2021-02-01 15:29:03

I am trying to remove the rows which have their mean equal to 0. I have tried this:

我试图删除其均值等于0的行。我试过这个:

import numpy as np

a = np.zeros((4,4))

for i in range(len(a)):

    if (i%2)==0 : a[i]= np.arange(4*i,4*(i+1))

    array([[  0.,   1.,   2.,   3.],
           [  0.,   0.,   0.,   0.],
           [  8.,   9.,  10.,  11.],
           [  0.,   0.,   0.,   0.]])        

np.ma.MaskedArray(a, mask=(np.mean(a,0)==0))
np.ma.compress_rows(a)

I would like to get this:

我想得到这个:

a =([[  0.,   1.,   2.,   3.],
     [  8.,   9.,  10.,  11.]])

1 个解决方案

#1


Using index and conditions

使用索引和条件

In [187]: a[~(a.mean(axis=1)==0)]
Out[187]:
array([[  0.,   1.,   2.,   3.],
       [  8.,   9.,  10.,  11.]])

#1


Using index and conditions

使用索引和条件

In [187]: a[~(a.mean(axis=1)==0)]
Out[187]:
array([[  0.,   1.,   2.,   3.],
       [  8.,   9.,  10.,  11.]])