选择某些行(满足条件),但仅选择Python/Numpy中的某些列
我有一个包含4列的numpy数组,并且想要选择第1列,第3列和第4列,其中第二列的值满足特定条件(即固定值).我尝试首先仅选择行,但通过以下方式选择所有4列:
I have an numpy array with 4 columns and want to select columns 1, 3 and 4, where the value of the second column meets a certain condition (i.e. a fixed value). I tried to first select only the rows, but with all 4 columns via:
I = A[A[:,1] == i]
有效.然后我进一步尝试了(类似于我非常了解的matlab):
which works. Then I further tried (similarly to matlab which I know very well):
I = A[A[:,1] == i, [0,2,3]]
不起作用.怎么做?
示例数据:
>>> A = np.array([[1,2,3,4],[6,1,3,4],[3,2,5,6]])
>>> print A
[[1 2 3 4]
[6 1 3 4]
[3 2 5 6]]
>>> i = 2
# I want to get the columns 1, 3 and 4 for every row which has the value i in the second column. In this case, this would be row 1 and 3 with columns 1, 3 and 4:
[[1 3 4]
[3 5 6]]
我现在正在使用这个:
I = A[A[:,1] == i]
I = I[:, [0,2,3]]
但是我认为必须有一种更好的方法...(我习惯于MATLAB)
But I thought that there had to be a nicer way of doing it... (Im used to MATLAB)
>>> a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
>>> a
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> a[a[:,0] > 3] # select rows where first column is greater than 3
array([[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
>>> a[a[:,0] > 3][:,np.array([True, True, False, True])] # select columns
array([[ 5, 6, 8],
[ 9, 10, 12]])
# fancier equivalent of the previous
>>> a[np.ix_(a[:,0] > 3, np.array([True, True, False, True]))]
array([[ 5, 6, 8],
[ 9, 10, 12]])
有关晦涩的np.ix_()
的说明,请参见 https://stackoverflow.com/a/13599843/4323
For an explanation of the obscure np.ix_()
, see https://stackoverflow.com/a/13599843/4323
最后,我们可以通过给出列号列表而不是乏味的布尔掩码来简化:
Finally, we can simplify by giving the list of column numbers instead of the tedious boolean mask:
>>> a[np.ix_(a[:,0] > 3, (0,1,3))]
array([[ 5, 6, 8],
[ 9, 10, 12]])