在由切片和两个布尔数组索引的 numpy 数组中设置值
问题描述
我有两个 numpy 数组:
I have two numpy arrays:
a = np.arange(100*100).reshape(100,100)
b = np.random.rand(100, 100)
我还有一个切片元组来提取数组的某个部分:
I also have a tuple of slices to extract a certain piece of the array:
slice_ = (slice(5, 10), slice(5, 10))
然后我有一组布尔索引来选择这个切片的某个部分:
I then have a set of boolean indices to select a certain part of this slice:
indices = b[slice_] > 0.5
如果我想将这些索引设置为不同的值,我可以轻松做到:
If I want to set these indices to a different value I can do it easily:
a[slice_][indices] = 42
但是,如果我创建另一组布尔索引来选择索引数组的特定部分:
However, if I create another set of boolean indices to select a specific part of the indexed array:
high_indices = a[slice_][indices] > 700
然后尝试在这些索引处设置数组的值:
and then try and set the value of the array at these indices:
a[slice_][indices][high_indices] = 42 # Doesn't do anything!
我想也许我需要将两个索引数组合并在一起,但它们的形状不同:indices
的形状为 (5, 5)
和 high_indices
的形状为 (12,)
.
I thought maybe I needed to AND the two index arrays together, but they are different shape: indices
has a shape of (5, 5)
and high_indices
has a shape of (12,)
.
我想我在试图做一些相对简单的事情时让自己陷入了一个可怕的混乱之中.如何使用这两个布尔数组进行索引,以便可以设置数组的值?
I think I've got myself into a terrible muddle here trying to do something relatively simple. How can I index using these two boolean arrays in such a way that I can set the values of the array?
解决方案
切片一个 numpy 数组返回一个视图,但布尔索引返回一个数组的副本.因此,当您第一次在 a[slice_][indices][high_indices]
中使用布尔索引对其进行索引时,您会得到一个副本,并将值 42 分配给副本而不是数组 <代码>一个代码>.您可以通过链接布尔索引来解决问题:
Slicing a numpy array returns a view, but boolean indexing returns a copy of an array. So when you indexed it first time with boolean index in a[slice_][indices][high_indices]
, you got back a copy, and the value 42 is assigned to a copy and not to the array a
. You can solve the problem by chaining the boolean index:
a[slice_][(a[slice_] > 700) & (b[slice_] > 0.5)] = 42
相关文章