我有一个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'的掩码
我试图通过给定数组中的原始索引来改变数组中的值:
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
保持不变。有什么解决办法的建议吗?在
情况:花式索引与常规索引
这是因为使用布尔数组或一系列索引是“花哨”的索引。(“Fancy”是不能用切片表示的任何东西)它实际上不是一个“masked array”,它在numpy术语中是完全独立的(
np.ma.masked_array
)。在花哨的索引可以复制。常规索引(即切片)生成视图。不共享数据,不共享数据。在
让我们分解一下你的表达式
arr[mask1][0,0] = 999
。在因为
mask1
是一个布尔数组,arr[mask1]
将返回数据的副本。下一部分将修改该副本,而不是原始数组。换句话说:让我们将其与一个类似的(在本例中)切片表达式进行对比:
^{pr2}$arr[1:][0, 0] = 999
(它将修改原始的arr
)我该怎么办?在
一般来说,你应该避免陷入这种情况。你想要完成的事情通常可以通过另一种方式来实现。在
但是,如果您真的需要,您可以通过将您喜欢的索引转换为您想要修改的特定索引来实现。在
例如:
在您的确切情况下,这是过度杀戮(也就是说,您可以用
arr[1:][0, 0] = 999
来做同样的事情)。还有很多其他的情况可以简化。然而,为了得到一个完全通用的解决方案,我们需要与上面的例子类似的东西。在解释解决方法
让我们来分析一下这个例子的作用。首先,我们创建一个与数组形状相同的“平面”索引数组。(附加说明,请参见
np.unravel_index
获取更多信息。)在本例中:现在我们可以提取花式索引将提取的索引:
然后下一个切片
[0,0]
:现在我们有一个单一的“平面”索引回到原始数组中。我们可以使用
np.unravel_index
将其转换为完整索引:…但直接使用
arr.flat
更容易:相关问题 更多 >
编程相关推荐