numpy索引具有数组和切片的多维数组 [英] Numpy indexing multidimensional arrays with array and slice
问题描述
我的疑惑是关于这个示例在numpy文档中.
My doubt is about this example in the numpy docs.
y = np.arange(35).reshape(5,7)
这是我要澄清的操作:
y[np.array([0,2,4]),1:3]
根据文档:
实际上,切片被转换为索引数组np.array([[1,2,]](形状(1,2)),并与索引数组一起广播以产生形状为(3,2 )."
According to the docs:
"In effect, the slice is converted to an index array np.array([[1,2]]) (shape (1,2)) that is broadcast with the index array to produce a resultant array of shape (3,2)."
这不起作用,所以我假设它不等效
This does not work, so I am assuming it is not equivalent
y[np.array([0,2,4]), np.array([1,2])]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-140-f4cd35e70141> in <module>()
----> 1 y[np.array([0,2,4]), np.array([1,2])]
ValueError: shape mismatch: objects cannot be broadcast to a single shape
此广播的形状数组(3,2)看起来如何?
How does this broadcasted array of shape (3,2) looks like?
推荐答案
广播更像是:
In [280]: y[np.array([0,2,4])[...,None], np.array([1,2])]
Out[280]:
array([[ 1, 2],
[15, 16],
[29, 30]])
我在[0,2,4]
中添加了一个尺寸,使其成为2d. broadcast_arrays
可用于查看广播的数组的外观:
I added a dimension to [0,2,4]
making it 2d. broadcast_arrays
can be used to see what the broadcasted arrays look like:
In [281]: np.broadcast_arrays(np.array([0,2,4])[...,None], np.array([1,2]))
Out[281]:
[array([[0, 0],
[2, 2],
[4, 4]]),
array([[1, 2],
[1, 2],
[1, 2]])]
np.broadcast_arrays([[0],[2],[4]], [1,2])
与没有array
包装器的情况相同. np.meshgrid([0,2,4], [1,2], indexing='ij')
是产生这些索引数组的另一种方法.
np.broadcast_arrays([[0],[2],[4]], [1,2])
is the samething without the array
wrappers. np.meshgrid([0,2,4], [1,2], indexing='ij')
is another way of producing these indexing arrays.
(由meshgrid
或broadcast_arrays
产生的列表可以用作y[_]
的参数.)
(the lists produced by meshgrid
or broadcast_arrays
could be used as the argument for y[_]
.)
所以说[1,2]
与索引数组一起广播是正确的,但是它省略了有关调整尺寸的内容.
So it's right to say [1,2]
is broadcast with the index array, but it omits the bit about adjusting dimensions.
早些时候,他们有这个例子:
A little earlier they have this example:
y[np.array([0,2,4])]
等效于y[np.array([0,2,4]), :]
.它选择3行,并从中选择所有项目. 1:3
情况可以看作是对此的扩展,选择3行,然后2列.
which is equivalent to y[np.array([0,2,4]), :]
. It picks 3 rows, and all items from them. The 1:3
case can be thought of as an extension of this, picking 3 rows, and then 2 columns.
y[[0,2,4],:][:,1:3]
如果广播过于混乱,这可能是思考索引的一种更好的方法.
This might be a better way of thinking about the indexing if broadcasting is too confusing.
还有另一个文档页面可能会更好地处理此问题
There's another docs page that might handle this better
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html
在此文档中,基本索引涉及切片和整数
In this docs, basic indexing involves slices and integers
y[:,1:3], y[1,:], y[1, 1:3]
高级索引涉及一个数组(或列表)
Advanced indexing involves an array (or list)
y[[0,2,4],:]
这产生与y[::2,:]
相同的结果,除了列表大小写会产生一个副本(切片(基本)视图).
This produces the same result as y[::2,:]
, except the list case produces a copy, the slice (basic) a view.
y[[0,2,4], [1,2,3]]
是纯高级索引数组索引的情况,结果是3个项目,分别位于(0,1)
,(2,2)
和(4,3)
.
y[[0,2,4], [1,2,3]]
is a case of pure advance index array indexing, the result is 3 items, ones at (0,1)
, (2,2)
, and (4,3)
.
y[[0,2,4], 1:3]
是本文档调用Combining advanced and basic indexing
的一种情况,从'[0,2,4]'开始'高级',从'1:3'开始基本.
y[[0,2,4], 1:3]
is a case that this docs calls Combining advanced and basic indexing
, 'advanced' from `[0,2,4]', basic from '1:3'.
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#combining-advanced-and-basic-indexing
查看更复杂的索引数组可能会增加一些见识.
Looking at a more complex index array might add some insight.
In [222]: i=[[0,2],[1,4]]
与另一个列表一起使用,它是纯"高级的,并且广播结果:
Used with another list, it is 'pure' advanced, and the result is broadcasted:
In [224]: y[i, [1,2]]
Out[224]:
array([[ 1, 16],
[ 8, 30]])
索引数组是:
In [234]: np.broadcast_arrays(i, [1,2])
Out[234]:
[array([[0, 2],
[1, 4]]),
array([[1, 2],
[1, 2]])]
[1,2]
列表刚刚扩展为一个(2,2)数组.
The [1,2]
list is just expanded to a (2,2) array.
将它与切片一起使用是这种混合高级/基本示例,结果为3d (2,2,2)
.
Using it with a slice is an example of this mixed advanced/basic, and the result is 3d (2,2,2)
.
In [223]: y[i, 1:3]
Out[223]:
array([[[ 1, 2],
[15, 16]],
[[ 8, 9],
[29, 30]]])
与广播等效的是
y[np.array(i)[...,None], [1,2]]
这篇关于numpy索引具有数组和切片的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!