Kyss Tao
Kyss Tao

Reputation: 531

Manipulating indices to 2d numpy array

I can index a 2d numpy array with a tuple or even a list of tuples

a = numpy.array([[1,2],[3,4]])
i = [(0,1),(1,0)] # edit: bad example, should have taken [(0,1),(0,1)]
print a[i[0]], a[i]

(Gives 2 [2 3])

However, I can not manipulate the tuples with vector arithmetic, i.e.

k = i[0]+i[1]

does not give the desired (1,1) but concatenates.

On the other hand using numpy arrays for the indices, the arithmetic works, but the indexing does not work.

i = numpy.array(i)
k = i[0]+i[1]      # ok
print a[k]

gives the array [[3 4], [3 4]] instead of the desired 4.

Is there a way to do vector arithmetic on the indices but also be able to index a numpy array with them (without deriving a class from tuple and overloading all the operators)?

This question looked promising at first but I could not figure out if I can apply it to my situation.

Edit (comment on accepted answer):

... and working on arrays of indices then works as well using map

arr = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
ids = numpy.array([(0,1),(1,0)])
ids += (0,1) # shift all indices by 1 column
print arr[map(tuple,ids.T)]

(confusing to me why I need the transpose, though. Would have run into this problem above as well, and was just fortunate with [(0,1),(0,1)])

Upvotes: 3

Views: 1482

Answers (3)

YXD
YXD

Reputation: 32511

Yes. Convert the NumPy array to a tuple when you need to index:

a[tuple(k)]

Test:

>>> a = numpy.array([[1,2],[3,4]])
>>> i = numpy.array([(0,1),(1,0)])
>>> k = i[0] + i[1]
>>> a[tuple(k)]
4

Upvotes: 2

Brendan Wood
Brendan Wood

Reputation: 6440

I believe the most straightforward way to do this would be to create a subclass of tuple and redefine its __add__ operator to do what you want. Here is how to do that: Python element-wise tuple operations like sum

Upvotes: 1

carla
carla

Reputation: 2251

try the following, it works for me:

import numpy

def functioncarla(a,b):
    return a+b

a = numpy.array([[1,2],[3,4]])
i = [(0,1),(1,0)]

#k = i[0]+i[1]
aux = map(functioncarla, i[0], i[1])
k = tuple(aux)
print 'k = ', k

print 'a[k] = ', a[k]

Upvotes: 0

Related Questions