Search code examples
pythonarraysnumpyindicescreation

numpy: Is there a way to create an array from a sequence of mappings w/o external loop?


To me, this sounds like a common use-case, but I couldn't find the proper function/thread for it, yet.

I have two numpy arrays, one is a sequence of triplets and the other the associated sequence of indices. I want to create a 1-dim array of equal sequence length, composed of the mapping items according to their index.

Example:

mapping = np.array(((25, 120, 240), (18, 177, 240), (0, 0, 0), (10, 120, 285)))
indices = np.array((0, 1, 0, 0))

print "mapping:", mapping
print "indices:", indices
print "mapped:", mapping[indices]

Which produces the following output:

mapping: [[ 25 120 240]
 [ 18 177 240]
 [  0   0   0]
 [  10 120 285]]
indices: [0 1 0 0]
mapped: [[ 25 120 240]
 [ 18 177 240]
 [ 25 120 240]
 [ 25 120 240]]

Of course, this approach takes the whole mapping array as one mapping, not as a list of mappings, returning only the 1st or 2nd inner mapping, according to the indices array. But what I was looking for is this:

mapped: [25 177 0 10]

... which is made from the 1st item of the 1st mapping, the 2nd of the 2nd mapping and the first of the 3rd and 4th mapping.

Is there a lean way to do it with numpy functionality alone, without external looping and without excess of memory usage for temporary arrays?


Solution

  • I think you are looking for this part of numpy's documentation on indexing.

    In [17]: mapping[(np.arange(indices.shape[-1]),indices)]
    Out[17]: array([ 25, 177,   0,   10])
    

    This create a temporary array (np.arange) but it is 1-dimensional and I couldn't think of anything better.