Home > Net >  Categorize a numpy array of floats to corresponding integer index
Categorize a numpy array of floats to corresponding integer index

Time:10-02

I couldn't find a better way to phrase the question. I have this array A:

0.1, 0.0, -0.2, -0.1, 0.1, 0.5

I want to convert this to B:

3, 2, 0, 1, 3, 4

Where each value of B is the index of the original float value in np.unique(A).

Is there a convenient numpy (vectorized) way of doing it?

PS:

Another example, consider array C:

[ 0.025, 0.025, -0.025, -0.025, -0.025, 0.075, -0.025, -0.075, 0.075, -0.075]

Result of np.unique(C):

array([-0.075, -0.05 , -0.025, 0. , 0.025, 0.05 , 0.075])

I want to change C to D:

[4, 4, 2, 2, 2, 6, 2, 0, 6]

Notice that -0.075 is the smallest value in C so it gets value 0 in D. Likewise, 0.075 is the largest value in C so it gets the largest value 6 in D.

CodePudding user response:

To return the indices which would reconstruct the array A, i.e. the "inverse" of unique, use numpy.unique with the option return_inverse=True:

import numpy as np

A = np.array([0.1, 0.0, -0.2, -0.1, 0.1, 0.5])
A_uniq, B = np.unique(A, return_inverse=True)
print(B) # [3 2 0 1 3 4]
  • Related