具有高级混合索引的 Numpy 子数组分配 [英] Numpy sub-array assignment with advanced, mixed indexing

查看:26
本文介绍了具有高级混合索引的 Numpy 子数组分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试分配数组的某些元素时,收到一条非常奇怪的错误消息.我正在使用切片和一组索引的组合.请参阅以下简单示例.

I am getting a very odd error message when I try to assign some of the elements of an array. I am using a combination of a slice and a set of indices. See the following simple example.

 import scipy as sp

 a = sp.zeros((3, 4, 5))
 b = sp.ones((4, 5))

 I = sp.array([0, 1, 3])

 b[:, I] = a[0, :, I]

此代码引发以下ValueError:

ValueError: shape mismatch: 形状 (3,4) 的值数组无法广播到形状 (3,4) 的索引结果

ValueError: shape mismatch: value array of shape (3,4) could not be broadcast to indexing result of shape (3,4)

--

使用切片和序列的组合时要小心.整数.正如在 github 上指出的那样:

Be careful when using a combination of a slice and seq. of integers. As pointed out on github:

x = rand(3, 5, 7)

print(x[0, :, [0,1]].shape)
# (2, 5)

print(x[0][:, [0,1]].shape)
# (5, 2)

这就是 numpy 的设计方式,但是 x[0][:, I] 与 x[0, :, I] 不同还是有点令人困惑.由于这是我希望我选择在代码中使用 x[0][:, I] 的行为.

This is how numpy is designed to work, but it is nevertheless a bit confusing that x[0][:, I] is not the same as x[0, :, I]. Since this is the behavior I want I choose to use x[0][:, I] in my code.

推荐答案

看起来在将代码复制到问题中时出现了一些错误.

Looks like there are some errors in copying your code to question.

但我怀疑索引存在一个已知问题:

But I suspect there's a known problem with indexing:

In [73]: a=np.zeros((2,3,4)); b=np.ones((3,4)); I=np.array([0,1])

制作 I 2 个元素.索引 b 给出了预期的 (3,2) 形状.切片中的 3 行,I 索引中的 2 列

Make I 2 elements. Indexing b gives the expected (3,2) shape. 3 rows from the slice, 2 columns from I indexing

In [74]: b[:,I].shape
Out[74]: (3, 2)

但是使用 3d a 我们得到了转置.

But with 3d a we get the transpose.

In [75]: a[0,:,I].shape
Out[75]: (2, 3)

并且赋值会产生错误

In [76]: b[:,I]=a[0,:,I]
...
ValueError: array is not broadcastable to correct shape

它将 I 定义的 2 元素维度放在第一位,然后将 : 中的 3 元素放在第二位.这是前面讨论过的混合高级索引的一个案例 - 并且还有一个错误问题.(我得查一下).

It's putting the 2 element dimension defined by I first, and the 3 element from : second. It's a case of mixed advanced indexing that has been discussed earlier - and there's a bug issue as well. (I'll have to look those up).

您可能正在使用更新的 numpy(或 scipy)并收到不同的错误消息.

You are probably using a newer numpy (or scipy) and getting a different error message.

据记载,使用两个数组或列表进行索引,并在中间切片,将切片放在最后,例如

It's documented that indexing with two arrays or lists, and slice in the middle, puts the slice at the end, e.g.

In [86]: a[[[0],[0],[1],[1]],:,[0,1]].shape
Out[86]: (4, 2, 3)

同样的事情发生在 a[0,:,[0,1]].但有一个很好的论据认为它不应该是这样.

The same thing is happening with a[0,:,[0,1]]. But there's a good argument that it shouldn't be this way.

至于修复,您可以转置一个值,或更改索引

As to a fix, you could transpose a value, or change the indexing

In [88]: b[:,I]=a[0:1,:,I]

In [90]: b[:,I]=a[0,:,I].T

In [91]: b
Out[91]: 
array([[ 0.,  0.,  1.,  1.],
       [ 0.,  0.,  1.,  1.],
       [ 0.,  0.,  1.,  1.]])

In [92]: b[:,I]=a[0][:,I]

https://github.com/numpy/numpy/issues/7030

https://github.com/numpy/numpy/pull/6256

这篇关于具有高级混合索引的 Numpy 子数组分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆