3

I am learning numpy framework.This piece of code I don't understand.

import numpy as np
a =np.array([[0,1,2],[3,4,5],[6,7,8],[9,10,11]])
print(a)
row = np.array([[0,0],[3,3]])
col = np.array([[0,2],[0,2]])
b = a[row,col]
print("This is b array:",b)

This b array returns the corner values of a array, that is, b equals [[0,2],[9,11]].

gboffi
  • 22,939
  • 8
  • 54
  • 85
usmanharoon
  • 173
  • 2
  • 13
  • You're saying you don't understand the code, please be more specific. What are you trying to do? What don't you understand? Please read this before asking a question: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – funie200 May 06 '19 at 08:37

4 Answers4

4

When indexing is done using an array or "array-like", to access/modify the elements of an array, then it's called advanced indexing.

In [37]: a
Out[37]: 
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

In [38]: row
Out[38]: 
array([[0, 0],
       [3, 3]])

In [39]: col
Out[39]: 
array([[0, 2],
       [0, 2]])

In [40]: a[row, col]
Out[40]: 
array([[ 0,  2],
       [ 9, 11]])

That's what you got. Below is an explanation:

              Indices of  
`a[row, col]` row  column  
   ||   ||    ||   ||
   VV   VV    VV   VV
  a[0,  0]   a[0,  2]   
  a[3,  0]   a[3,  2]
    |__________|   |
   row-idx array   |
        |__________| 
        column-idx array
kmario23
  • 57,311
  • 13
  • 161
  • 150
  • Your visual presentation is stunning, can you tell me how you did it? With tools or completely manual typesetting? – Easy One Jun 05 '22 at 05:47
2

You're indexing a using two equally shaped 2d-arrays, hence you're output array will also have the same shape as col and row. To better understand how array indexing works you can check the docs, where as shown, indexing with 1d-arrays over the existing axis' of a given array works as follows:

result[i_1, ..., i_M] == x[ind_1[i_1, ..., i_M], ind_2[i_1, ..., i_M], ..., ind_N[i_1, ..., i_M]]

Where the same logic applies in the case of indexing with 2d-arrays over each axis, but instead you'd have a result array with up to i_N_M indices.

So going back to your example you are essentially selecting from the rows of a based on row, and from those rows you are selecting some columns col. You might find it more intuitive to translate the row and column indices into (x,y) coordinates:

(0,0), (0,2)   
(3,0), (3,2)   

Which, by accordingly selecting from a, results in the output array:

print(a[row,col])

array([[ 0,  2],
       [ 9, 11]])
yatu
  • 86,083
  • 12
  • 84
  • 139
0

You can understand it by making more tries, to see more examples.

If you have one dimensional index:

In [58]: np.arange(10)[np.array([1,3,4,6])]
Out[58]: array([1, 3, 4, 6])

In case of two dimensional index:

In [57]: np.arange(10)[np.array([[1,3],[4,6]])]
Out[57]: 
array([[1, 3],
       [4, 6]])

If you use 3 dimensional index:

In [59]: np.arange(10)[np.array([[[1],[3]],[[4],[6]]])]
Out[59]: 
array([[[1],
        [3]],

       [[4],
        [6]]])

As you can see, if you make hierarchy in indexing, you will get it in the output as well.

quantummind
  • 2,086
  • 1
  • 14
  • 20
0

Proceeding by steps:

import numpy as np
a = np.array([[0,1,2],[3,4,5],[6,7,8],[9,10,11]])

print(a)

gives 2d array a:

array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

Then:

row = np.array([[0,0],[3,3]])

assigns to 2d array row values [0,0] and [3,3]:

array([[0, 0],
       [3, 3]])

Then:

col = np.array([[0,2],[0,2]])

assigns to 2d array col values [0,2] and [0,2]:

array([[0, 2],
       [0, 2]])

Finally:

b = a[row,col]

assigns to b values given by a[0,0], a[0,2] for the first row, a[3,0], a[3,2] for the second row, that is:

 array([[ 0,  2],
       [ 9, 11]])

Where does b[0,0] <-- a[0,0] come from? It comes from the combination of row[0,0] which is 0 and col[0,0] which is 0.

What about b[0,1] <-- a[0,2]? It comes from the combination of row[0,1] which is 0 and col[0,1] which is 2.

And so forth.

sentence
  • 8,213
  • 4
  • 31
  • 40