Return Rows (Array) where the first 2 columns are bigger than 0.1

时间:2021-01-29 20:10:29

An easy Example for what i want:

我想要的简单示例:

I have a Matrix with many columns where i want to keep rows, if the cells in the first 2 columns are (both!!) bigger than 0.1

如果前两列中的单元格(都是!!)大于0.1,我有一个包含许多列的矩阵,我希望保留行

something like that:

类似的东西:

import numpy as np
A=np.array([[1,0,1],[1,2,5],[0,3,5],[0,0,2]])
B=np.zeros(4,3)
wildcard = np.vstack((B, A[A[:,0 and 1] > 0.1]))

obviously the "and" does not work, but that is what i want- both values in each line should be checked before they are returned to a new array where just the rows with these conditions remain.

显然“和”不起作用,但这就是我想要的 - 在返回到新数组之前,应检查每一行中的两个值,其中只保留具有这些条件的行。

1 个解决方案

#1


2  

instead of using and you can use &. Extra parenthesis are required because & binds tighter than >

而不是使用,你可以使用&。需要额外的括号,因为&绑定比>更严格

a[(a[:,0] > .1) & (a[:,1] > .1)]

The method proposed by Ophion in the comments works as well but is slower.

Ophion在评论中提出的方法也适用但速度较慢。

In [85]: b = np.random.random((100000,3))

In [86]: %timeit np.all(b[:,:2] > 0.1, axis = 1)
100 loops, best of 3: 2.99 ms per loop

In [87]: %timeit (b[:,0] > .1) & (b[:,1] > .1)
1000 loops, best of 3: 542 us per loop

#1


2  

instead of using and you can use &. Extra parenthesis are required because & binds tighter than >

而不是使用,你可以使用&。需要额外的括号,因为&绑定比>更严格

a[(a[:,0] > .1) & (a[:,1] > .1)]

The method proposed by Ophion in the comments works as well but is slower.

Ophion在评论中提出的方法也适用但速度较慢。

In [85]: b = np.random.random((100000,3))

In [86]: %timeit np.all(b[:,:2] > 0.1, axis = 1)
100 loops, best of 3: 2.99 ms per loop

In [87]: %timeit (b[:,0] > .1) & (b[:,1] > .1)
1000 loops, best of 3: 542 us per loop