所以我有一个二维数组data
,看起来像这样:
我想计算后两列在一定条件下的行数。例如,在数组的这个特定切片中,我只有
1 | 2
但是鉴于第三行是range(1,4)
和第四行range(0,3)
,我可以具有以下所有组合:
1 | 0
1 | 1
1 | 2
2 | 0
2 | 1
2 | 2
3 | 0
3 | 1
3 | 2
我想选择其中每个条件都为真的行。但是我不确定该怎么做?在过去的两个小时中,我一直在研究这一问题,并且提出了使用for循环,列表理解等内容的方法。但是它变得越来越复杂,而且这些方法都没有真正起作用。有什么好方法可以在numpy甚至是纯python中做到这一点吗?
任何帮助将不胜感激,谢谢!
这有效:
import numpy as np
# data array
data = np.array([[4,3,1,2],[4,3,5,1],[1,2,1,0]])
# array of acceptable combinations
cond = np.array([[1,0],[1,2]])
# index of rows matching the conditions
idx=np.array([any(np.equal(cond,row).all(1)) for row in data[:,2:]])
# selected rows
data[idx]
# array([[4, 3, 1, 2],
# [1, 2, 1, 0]]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句