Indexing a numpy tuple array

Why can't I index ndarray using a list of tuple indexes, for example?

idx = [(x1, y1), ... (xn, yn)] X[idx] 

Instead, I need to do something bulky, like

 idx2 = numpy.array(idx) X[idx2[:, 0], idx2[:, 1]] # or more generally: X[tuple(numpy.vsplit(idx2.T, 1)[0])] 

Is there a simpler, more pythonic way?

+6
source share
1 answer

You can use a list of tuples, but the convention is different from what you want. numpy expects a list of row indices, followed by a list of column values. You apparently want to specify a list of pairs (x, y).

http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#integer-array-indexing The relevant section in the documentation is "indexing an integer array".


Here is an example looking for 3 points in a 2d array. (2 points in 2d can be misleading):

 In [223]: idx Out[223]: [(0, 1, 1), (2, 3, 0)] In [224]: X[idx] Out[224]: array([2, 7, 4]) 

Using your xy style of index pairs:

 In [230]: idx1 = [(0,2),(1,3),(1,0)] In [231]: [X[i] for i in idx1] Out[231]: [2, 7, 4] In [240]: X[tuple(np.array(idx1).T)] Out[240]: array([2, 7, 4]) 

X[tuple(zip(*idx1))] is another way to do the conversion. tuple() is optional in Python2. zip(*...) is a Python idiom that cancels the nesting of a list of lists.

You are on the right track:

 In [242]: idx2=np.array(idx1) In [243]: X[idx2[:,0], idx2[:,1]] Out[243]: array([2, 7, 4]) 

My tuple() bit more compact (and not necessarily more "pythonic"). Given a numpy convention, some kind of conversion is required.

(Should we check what works with n-dimensions and m-points?)

+14
source

Source: https://habr.com/ru/post/982411/


All Articles