Skip to main content
 首页 » 编程设计

python之更改掩码 np 数组中的值

2024年10月24日13sharpest

我有一个 np 数组:

arr = np.array( 
[[1,2,3,4,5,6], 
[11,12,13,14,15,16], 
[21,22,23,24,25,26], 
[31,32,33,34,35,36], 
[41,42,43,44,45,46]]) 

以及从 arr

中选择“行”的掩码
mask = np.array([False,True,True,True,True]) 

我正在尝试通过为屏蔽数组提供相对索引来更改原始数组中的值:

arr[mask1][0,0] = 999 

预期输出:

[[  1   2   3   4   5   6] 
 [999  12  13  14  15  16] 
 [ 21  22  23  24  25  26] 
 [ 31  32  33  34  35  36] 
 [ 41  42  43  44  45  46]] 

但是,问题是arr 保持不变。任何解决方法建议?

请您参考如下方法:

发生了什么:花式索引与常规索引

发生这种情况是因为使用 bool 数组或索引序列是“奇特的”索引。 (“奇特”是指任何不能表示为切片的东西。)它实际上不是一个“掩码数组”,它在 numpy 术语中完全是一个独立的东西 (np.ma.masked_array)。

花哨的索引制作副本。定期索引(即切片)生成 View 。 View 共享数据,副本不共享数据。

让我们分解你的表达式 arr[mask1][0,0] = 999。

因为 mask1 是一个 bool 数组,arr[mask1] 将返回数据的副本。下一部分将修改该副本,而不是原始数组。换句话说:

# tmp_copy is a copy, not a view, in this case 
tmp_copy = arr[mask1] 
 
# tmp_copy is modified, but `arr` is not 
tmp_copy[0, 0] = 999  
 
# Because `tmp_copy` is an intermediate, it will be garbage collected. 
# The assignment to 999 effectively disappears 
del temp_copy 

让我们将其与类似的(在这种情况下)切片表达式进行对比:arr[1:][0, 0] = 999(这将修改原始的 arr)

# Because we're using a slice, a view will be created instead of a copy 
tmp_view = arr[1:] 
 
# Modifying the view will modify the original array as well 
tmp_view[0, 0] = 999 
 
# The view can be deleted, but the original has still been modified 
del tmp_view 

我该如何解决这个问题?

一般来说,您会避免让自己陷入这种情况。你想要完成的事情通常可以用另一种方式重铸。

但是,如果您真的需要,您可以通过将花哨的索引转换为您想要修改的特定索引来实现。

例如:

import numpy as np 
 
# Your data... 
arr = np.array([[1,2,3,4,5,6], 
                [11,12,13,14,15,16], 
                [21,22,23,24,25,26], 
                [31,32,33,34,35,36], 
                [41,42,43,44,45,46]]) 
mask = np.array([False,True,True,True,True]) 
 
# Make a temporary array of the "flat" indices of arr 
idx = np.arange(arr.size).reshape(arr.shape) 
 
# Now use this to make your assignment: 
arr.flat[idx[mask][0, 0]] = 999 

在您的确切情况下,这是矫枉过正(即,您可以对 arr[1:][0, 0] = 999 执行相同的操作)。还有很多其他情况可以简化。然而,为了获得完全通用的解决方案,我们需要类似于上面示例的东西。

解释解决方法

让我们分解一下该示例的作用。首先,我们创建一个与我们的数组形状相同的“平面”索引数组。 (旁注,请参阅 np.unravel_index 了解更多信息。)在这种情况下:

In [37]: idx 
Out[37]: 
array([[ 0,  1,  2,  3,  4,  5], 
       [ 6,  7,  8,  9, 10, 11], 
       [12, 13, 14, 15, 16, 17], 
       [18, 19, 20, 21, 22, 23], 
       [24, 25, 26, 27, 28, 29]]) 

现在我们可以提取花式索引将提取的索引:

In [38]: idx[mask] 
Out[38]: 
array([[ 6,  7,  8,  9, 10, 11], 
       [12, 13, 14, 15, 16, 17], 
       [18, 19, 20, 21, 22, 23], 
       [24, 25, 26, 27, 28, 29]]) 

然后下一个切片[0,0]:

In [39]: idx[mask][0,0] 
Out[39]: 6 

现在我们有一个返回原始数组的“平面”索引。我们可以使用 np.unravel_index 将其转换为完整索引:

In [40]: np.unravel_index(6, arr.shape) 
Out[40]: (1, 0) 

...但是直接使用 arr.flat 会更容易:

In [41]: arr.flat[6] = 999 
 
In [42]: arr 
Out[42]: 
array([[  1,   2,   3,   4,   5,   6], 
       [999,  12,  13,  14,  15,  16], 
       [ 21,  22,  23,  24,  25,  26], 
       [ 31,  32,  33,  34,  35,  36], 
       [ 41,  42,  43,  44,  45,  46]])