更改屏蔽np数组中的值

2024-09-27 07:30:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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中选择'rows'的掩码

^{pr2}$

我试图通过给定数组中的原始索引来改变数组中的值:

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保持不变。有什么解决办法的建议吗?在


Tags: np数组array建议rows产量arr掩码
1条回答
网友
1楼 · 发布于 2024-09-27 07:30:51

情况:花式索引与常规索引

这是因为使用布尔数组或一系列索引是“花哨”的索引。(“Fancy”是不能用切片表示的任何东西)它实际上不是一个“masked array”,它在numpy术语中是完全独立的(np.ma.masked_array)。在

花哨的索引可以复制。常规索引(即切片)生成视图。不共享数据,不共享数据。在

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

因为mask1是一个布尔数组,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

^{pr2}$

我该怎么办?在

一般来说,你应该避免陷入这种情况。你想要完成的事情通常可以通过另一种方式来实现。在

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

例如:

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]])

相关问题 更多 >

    热门问题