Python Numpy 2D数组索引 [英] Python numpy 2D array indexing
问题描述
我对python和numpy很陌生.请问有人可以帮助我了解如何对用作索引的某些数组进行索引.我有以下六个2D数组,像这样-
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-
array([[2, 0],
[3, 0],
[3, 1],
[5, 0],
[5, 1],
[5, 2]])
我想使用这些数组作为索引,并将值10放入新的空矩阵的相应索引中.输出应如下所示-
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-
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.
推荐答案
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]])
工作原理:
如果您在赋值中使用两个 numpy数组为b
编制索引,
If you index b
with two numpy arrays in an assignment,
b[x, y] = z
然后将NumPy视为同时在x
的每个元素和y
的每个元素以及z
的每个元素上移动(我们将它们称为xval
,yval
和zval
),并进行分配将值[c6]设置为b [xval,yval].当z
是常数时,每次移动z
时都将返回相同的值.
then think of NumPy as moving simultaneously over each element of x
and each element of y
and each element of z
(let's call them xval
, yval
and zval
), and assigning to b[xval, yval] the value zval
. When z
is a constant, "moving over z
just returns the same value each time.
这就是我们想要的,x
是a
的第一列,而y
是a
的第二列.因此,选择x = a[:, 0]
和y = a[:, 1]
.
That's what we want, with x
being the first column of a
and y
being the second column of a
. Thus, choose x = a[:, 0]
, and y = a[:, 1]
.
b[a[:,0], a[:,1]] = 10
为什么b[a] = 10
不起作用
Why b[a] = 10
does not work
在编写b[a]
时,请将NumPy视为通过移动a
的每个元素来创建一个新数组,(让我们分别调用每个idx
)并将b[idx]
的值放在新数组中idx
在a
中的位置.
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 idx
in a
.
idx
是a
中的值.所以它是一个int32. b
的形状为(6,3),所以b[idx]
是一行的b
形状为(3,).例如,当idx
是
idx
is a value in a
. So it is an int32. b
is of shape (6,3), so b[idx]
is a row of b
of shape (3,). For example, when idx
is
In [37]: a[1,1]
Out[37]: 0
b[a[1,1]]
是
In [38]: b[a[1,1]]
Out[38]: array([0, 0, 0])
所以
In [33]: b[a].shape
Out[33]: (6, 2, 3)
因此,让我们重复一遍:NumPy通过移动a
的每个元素并将b[idx]
的值放在a
中idx
的位置上来创建一个新的数组.当idx
在a
上移动时,将创建形状(6,2)的数组.但是,由于b[idx]
本身的形状为(3,),因此在(6,2)形数组中的每个位置都放置了(3,)形值.结果是形状为(6,2,3)的数组.
So let's repeat: NumPy is creating a new array by moving over each element of a
and placing in the new array the value of b[idx]
at the location of idx
in a
. As idx
moves 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).
现在,当您进行类似作业
Now, when you make an assignment like
b[a] = 10
创建具有值b[a]
的形状(6,2,3)的临时数组,然后执行赋值.由于10是常数,因此此赋值将值10放置在(6,2,3)形数组中的每个位置.
然后,将临时数组中的值重新分配回b
.
请参见对文档的引用.因此,将(6,2,3)形数组中的值复制回(6,3)形b
数组中.值彼此覆盖.但是要点是,您没有获得想要的作业.
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 b
array. Values overwrite each other. But the main point is you do not obtain the assignments you desire.
这篇关于Python Numpy 2D数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!