[英]How to properly index a big matrix in python
I have a big numpy array with certain entries. 我有一个大的numpy数组与某些条目。 Let's say a dummy example is:
假设一个虚拟的例子是:
arr = np.array([[[1.0, 2.0, 3.0],[1.5, 1.8, 3.2]],
[[1.3, 1.7, 1.9],[1.4, 1.9, 2.1]],
[[1.8, 2.2, 2.5],[2.0, 2.2, 2.8]]])
I would like to know all the indexes where the entries of arr
fall within some range, say 1.5
and 2.4
. 我想知道
arr
条目落在某个范围内的所有索引,比如1.5
和2.4
。 And I would like to fill another matrix of the same shape as arr
with 1
at indexes where value of arr
falls within the range, otherwise with 0
. 我想补相同的形状的另一矩阵
arr
与1
在其中的价值指数arr
落入该范围内,以其他方式与0
。 That is, I would like to get a matrix like: 也就是说,我想得到一个矩阵,如:
mask = np.array([[[0, 1, 0], [1, 1, 0]],
[[0, 1, 1], [0, 1, 1]],
[[1, 1, 0], [1, 1, 0]]])
Is there any simple numpy
trick to do this? 有没有简单的
numpy
技巧来做到这一点? I know it is straightforward to do it with a for loop
, but since my arr
is pretty big in size, I would want it to be reasonably fast. 我知道使用
for loop
来做这件事很简单,但由于我的arr
非常大,我希望它的速度相当快。 Thanks 谢谢
You can use masking and np.where
: First create a conditional mask combining your two boundary conditions and then pass it to np.where
. 您可以使用masking和
np.where
:首先创建一个条件掩码,组合您的两个边界条件,然后将其传递给np.where
。 The matrix will be assigned 1
where this condition holds True else 0
if its False 矩阵将被赋值为
1
,此条件保持为True否则为0
如果为False
Minimal working answer 最小的工作答案
import numpy as np
arr = np.array([[[1.0, 2.0, 3.0],[1.5, 1.8, 3.2]],
[[1.3, 1.7, 1.9],[1.4, 1.9, 2.1]],
[[1.8, 2.2, 2.5],[2.0, 2.2, 2.8]]])
mask = ((arr>1.5) & (arr<2.4))
arr = np.where(mask, 1, 0)
print (arr)
Output 产量
array([[[0, 1, 0],
[0, 1, 0]],
[[0, 1, 1],
[0, 1, 1]],
[[1, 1, 0],
[1, 1, 0]]])
Create a boolean mask meeting your conditions. 创建符合条件的布尔掩码。 Adding 0 to boolean values will convert them to numeric results:
将0添加到布尔值会将它们转换为数字结果:
import numpy as np
arr = np.array([[[1.0, 2.0, 3.0],[1.5, 1.8, 3.2]],
[[1.3, 1.7, 1.9],[1.4, 1.9, 2.1]],
[[1.8, 2.2, 2.5],[2.0, 2.2, 2.8]]])
arr_out = ((arr>=1.5) & (arr<=2.4)) + 0
print(arr_out)
Alternatively: 或者:
arr_out = np.array(((arr>=1.5) & (arr<=2.4)), dtype=np.uint8)
print(arr_out)
Or, as suggested by @hpaulj: 或者,正如@hpaulj所建议的那样:
arr_out = ((arr>=1.5) & (arr<=2.4)).astype(int)
print (arr_out)
Output: 输出:
[[[0 1 0]
[1 1 0]]
[[0 1 1]
[0 1 1]]
[[1 1 0]
[1 1 0]]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.