pythonnumpynumpy-indexing

From array of indicies, create an array of the values at those indicies


This is for a Machine Learning problem (in Python of course).

I have a 2 dimensional array, the rows are set of points, and the columns are indices into another 1 dimensional array of values for those points.

data = [[1,3,2], [3,3,1], [5,1,2]]
# yes there are duplicates in the labels
labels = [2,8,9,8,8,9]

What I need is to create a 2D array that is the original data array, but where the values in it are now the value from labels that the index represented.

new_data = [[8,8,9], [8,8,8], [9,8,9]]

I can do this with for loops obviously. I'm asking here in case numpy or something has a call that does this.


Solution

  • Use the indices as indices:

    np.array(labels)[np.array(data)]
    

    The output of an advanced (integer) index is the shape of the index array (data).