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

Asked

Viewed 37 times

-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. I would get 2. Somebody knows how to do this?

1 answer

1

Datum:

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

(The example you passed creates a one-dimensional array in which the first item is a list), You can put multidimensional indexes that are in other variables within brackets to retrieve a value - only these indexes must necessarily be one tuple (tuple) from Python - cannot be other types of sequence:

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

The exit is 2.

Or directly:

b = 0, 1
A[b]

(In that 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 obtaining slices (Slices) of the arrays in the same way, for which the literal notation is made with the use of ::

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 must create the tuple in the same way and use objects like slice to have the same effect as the rating inicio:fim[:passo]:

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 an axis is used the notation of ... (elllipsis), the name ... corresponds to the object Ellipsis (similar to None: there is only one, etc...), and you can both use that name when yourself ... on your tuple:

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

Exit:

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 though you can have the indices as a tuple, we need to have an object that is called as a function and returns an element of the array - the syntax of [ ] is not enough. (yes, usually you will be programming something quite advanced to get to that) - in such cases, you can directly use the method __getitem__ array:

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

It will recover 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 don’t feel like calling Dunder methods directly - in this case the __getitem__, can be used the function getitem module operator, 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 that covers the whole topic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.