Does anyone know how to use a variable of type array to access a position in an array of numpy?

-1

I have A = np.array ([[1,2], 3]) and I want to use [0,1] to make A [0,1] being [0,1] a variable. Would get 2. Does anyone know how to do this?

    
asked by anonymous 05.12.2018 / 12:13

1 answer

1

Given:

A=np.array([[1,2],[3,4]]) 

(The example you passed creates a one-dimensional array where the first item is a list), You can put multidimensional indexes that are in other variables inside brackets to retrieve a value - but these indexes must necessarily be a Python tuple - they can not be other sequence types: / p>

b = [0, 1]
A[tuple(b)]

The output is 2 .

Or directly:

b = 0, 1
A[b]

(In this case, b is already a tuple - when there is no ambiguity, the parentheses around the tuple are optional and b = 0, 1 is the same as b = (0, 1) )

Taking advantage of the question, if you are interested in getting slices of arrays in the same way, for which, literal notation is made using : :

In [69]: A = np.array(range(25))

In [70]: A.shape=(5,5)

In [71]: A
Out[71]: 
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])


In [73]: A[1:4,1:4]
Out[73]: 
array([[ 6,  7,  8],
       [11, 12, 13],
       [16, 17, 18]])

You should create the tuple in the same way, and use objects of type slice to have the same effect as the inicio:fim[:passo] notation:

In [74]: b = slice(1,4), slice(1, 4)

In [75]: A[b]
Out[75]: 
array([[ 6,  7,  8],
       [11, 12, 13],
       [16, 17, 18]])

If you use the notation of ... (elllipsis) for some axis, the name ... corresponds to the object Ellipsis (similar to None: there is only one, etc ...), and you can either use this name when own ... in your tuple:

b = slice(1,4), ... 
# ou
b = slice(1,4), Ellipsis

Output:

In [81]: A[b]
Out[81]: 
array([[ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])

And finally, just to cover all aspects, in some cases even if you can have the indexes as a tuple, we need to have an object that is callable as a function and return an element of the Array - the syntax of [ ] does not that's enough. (yes, you will usually be programming something quite advanced to reach that much) - in these cases, you can directly use the __getitem__ array method:

sum(map(A.__getitem__, [(0, 0), (1,1), (2, 2), (3, 3), (4, 4)]) )

It will retrieve the elements in the positions indicated in the sequence and add their values, for example. (Attention - this is an example, there are several more readable and better ways to do this - in this case, for example: sum(A.diagonal()) ). And finally, for those who do not feel like calling the dunder methods directly - in this case __getitem__ , you can use the getitem function of the operator module, passing the array as the first parameter:

from operator import getitem
from functools import partial

sum(map(partial(getitem, A), [(0, 0), (1,1), (2, 2), (3, 3), (4, 4)]) )

I think this covers the whole topic.

    
05.12.2018 / 12:37