简体   繁体   English

Python numpy 2D数组索引

[英]Python numpy 2D array indexing

I am quite new to python and numpy. 我对python和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- 我有以下六个这样的2D阵列 -

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. 我想使用这些数组作为索引,并将值10放在新空矩阵的相应索引中。 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]])

Why it works: 为什么会这样:

If you index b with two numpy arrays in an assignment, 如果在赋值中使用两个 numpy数组索引b

b[x, y] = 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 . 然后将NumPy想象为同时移动x每个元素和y每个元素以及z每个元素(让我们称之为xvalyvalzval ),并将值[ zval ]赋值给b [xval,yval]。 When z is a constant, "moving over z just returns the same value each time. z是常数时,“在z移动只是每次返回相同的值。

That's what we want, with x being the first column of a and y being the second column of a . 这就是我们想要的,与x是第一列ay是第二列a Thus, choose x = a[:, 0] , and y = a[:, 1] . 因此,选择x = a[:, 0] ,并且y = a[:, 1]

b[a[:,0], a[:,1]] = 10

Why b[a] = 10 does 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 idx in a . 当编写b[a]认为NumPy的,如通过移动超过的每个元素创建新阵列a ,(我们称之为每一个idx )和新的阵列中放置的值b[idx]在的位置idxa

idx is a value in a . idx是在一个值a So it is an int32. 所以这是一个int32。 b is of shape (6,3), so b[idx] is a row of b of shape (3,). b具有形状(6,3),因此b[idx]是一排b形状(3,)。 For example, when idx is 例如,当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 a and placing in the new array the value of b[idx] at the location of idx in a . 因此,让我们重复:NumPy的是通过移动过的每个元素创建新阵列a和新的阵列中放置的值b[idx]在的位置idxa As idx moves over a , an array of shape (6,2) would be created. idxa移动时,将创建一个形状(6,2)的数组。 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. 但由于b[idx]本身就是形状(3,),所以在(6,2)形阵列中的每个位置都放置一个(3,)形状的值。 The result is an array of shape (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. 创建具有值b[a]的临时形状数组(6,2,3),然后执行赋值。 Since 10 is a constant, this assignment places the value 10 at each location in the (6,2,3)-shaped array. 由于10是常量,因此该赋值将值10放在(6,2,3)形数组中的每个位置。 Then the values from the temporary array are reassigned back to b . 然后将临时数组中的值重新分配回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. 因此,(6,2,3)形数组中的值被复制回(6,3)形b数组。 Values overwrite each other. 值互相覆盖。 But the main point is you do not obtain the assignments you desire. 但重点是你没有获得你想要的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM