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

查看:336
本文介绍了具有高级混合索引的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:形状不匹配:形状(3,4)的值数组无法广播到形状的索引结果(3,4)

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

-

Be使用切片和seq的组合时要小心。整数。正如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])

生成 2个元素。索引 b 给出预期的(3,2)形状。切片中的3行,中的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元素:第二。这是前面已经讨论过的混合高级索引的情况 - 并且还存在一个bug问题。 (我必须查看那些)。

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天全站免登陆