Python numpy 2D 数组索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16396141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python numpy 2D array indexing
提问by haq
I am quite new to python and numpy. Can some one pls help me to understand how I can do the indexing of some arrays used as indices. I have the following six 2D arrays like this-
我对 python 和 numpy 很陌生。有人可以帮助我了解如何对用作索引的某些数组进行索引。我有以下六个这样的二维数组-
array([[2, 0],
[3, 0],
[3, 1],
[5, 0],
[5, 1],
[5, 2]])
I want to use these arrays as indices and put the value 10 in the corresponding indices of a new empty matrix. The output should look like this-
我想将这些数组用作索引,并将值 10 放入新空矩阵的相应索引中。输出应该是这样的——
array([[ 0, 0, 0],
[ 0, 0, 0],
[10, 0, 0],
[10, 10, 0],
[ 0, 0, 0],
[10, 10, 10]])
So far I have tried this-
到目前为止,我已经尝试过这个 -
from numpy import*
a = array([[2,0],[3,0],[3,1],[5,0],[5,1],[5,2]])
b = zeros((6,3),dtype ='int32')
b[a] = 10
But this gives me the wrong output. Any help pls.
但这给了我错误的输出。任何帮助请。
采纳答案by unutbu
In [1]: import numpy as np
In [2]: a = np.array([[2,0],[3,0],[3,1],[5,0],[5,1],[5,2]])
In [3]: b = np.zeros((6,3), dtype='int32')
In [4]: b[a[:,0], a[:,1]] = 10
In [5]: b
Out[5]:
array([[ 0, 0, 0],
[ 0, 0, 0],
[10, 0, 0],
[10, 10, 0],
[ 0, 0, 0],
[10, 10, 10]])
Why it works:
为什么有效:
If you index bwith twonumpy arrays in an assignment,
如果在赋值中b使用两个numpy 数组进行索引,
b[x, y] = z
then think of NumPy as moving simultaneously over each element of xand each element of yand each element of z(let's call them xval, yvaland zval), and assigning to b[xval, yval] the value zval. When zis a constant, "moving over zjust returns the same value each time.
然后将 NumPy 视为同时移动(让我们称它们为, and ) 的每个元素x和每个元素的y每个元素,并将值分配给 b[xval, yval] 。When是一个常数,“每次移动只会返回相同的值。zxvalyvalzvalzvalzz
That's what we want, with xbeing the first column of aand ybeing the second column of a. Thus, choose x = a[:, 0], and y = a[:, 1].
这就是我们想要的,x作为 的第一列a和y第二列a。因此,选择x = a[:, 0], 和y = a[:, 1]。
b[a[:,0], a[:,1]] = 10
Why b[a] = 10does not work
为什么b[a] = 10不起作用
When you write b[a], think of NumPy as creating a new array by moving over each element of a, (let's call each one idx) and placing in the new array the value of b[idx]at the location of idxin a.
当你写b[a],认为NumPy的,如通过的每一个元素移动在创造一个新的数组a,(我们称之为每一个idx)和新的阵列中放置的价值b[idx]在的位置idx中a。
idxis a value in a. So it is an int32. bis of shape (6,3), so b[idx]is a row of bof shape (3,). For example, when idxis
idx是 中的一个值a。所以它是一个int32。 b是形状 (6,3),所以b[idx]是b形状 (3,) 的行。例如,当idx是
In [37]: a[1,1]
Out[37]: 0
b[a[1,1]]is
b[a[1,1]]是
In [38]: b[a[1,1]]
Out[38]: array([0, 0, 0])
So
所以
In [33]: b[a].shape
Out[33]: (6, 2, 3)
So let's repeat: NumPy is creating a new array by moving over each element of aand placing in the new array the value of b[idx]at the location of idxin a. As idxmoves over a, an array of shape (6,2) would be created. But since b[idx]is itself of shape (3,), at each location in the (6,2)-shaped array, a (3,)-shaped value is being placed. The result is an array of shape (6,2,3).
因此,让我们再次重申:NumPy的是通过创建的每个元素移动在新的阵列a和新阵列中放置的价值b[idx]在的位置idx中a。随着idx移动到a,将创建一个形状为 (6,2) 的数组。但是由于b[idx]它本身的形状为 (3,),因此在 (6,2) 形数组中的每个位置,都会放置一个 (3,) 形值。结果是一个形状为 (6,2,3) 的数组。
Now, when you make an assignment like
现在,当你做这样的任务时
b[a] = 10
a temporary array of shape (6,2,3) with values b[a]is created, then the assignment is performed. Since 10 is a constant, this assignment places the value 10 at each location in the (6,2,3)-shaped array.
Then the values from the temporary array are reassigned back to b.
See reference to docs. Thus the values in the (6,2,3)-shaped array are copied back to the (6,3)-shaped barray. Values overwrite each other. But the main point is you do not obtain the assignments you desire.
创建一个带有值的临时形状 (6,2,3) 数组b[a],然后执行赋值。由于 10 是一个常数,因此此赋值将值 10 放置在 (6,2,3) 形数组中的每个位置。然后将临时数组中的值重新分配回b. 请参阅文档参考。因此,(6,2,3)形数组中的值被复制回(6,3)形b数组。值会相互覆盖。但重点是你没有得到你想要的任务。

