Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change numpy array to start from zero

I am trying to figure out a way to take a numpy array of integers, then change the entries such that the smallest is 0, the second smallest is 1, etc.

E.g.

Start with this

In [13]: a = numpy.array([[1, 2, 10],[1, 2, 99]])

In [14]: a
Out[14]: 
array([[ 1,  2, 10],
       [ 1,  2, 99]])

And get this:

array([[ 0,  1, 2],
       [ 0,  1, 3]])

I can start to see the way through with numpy.unique(), e.g.

In [19]: range(len(b))
Out[19]: [0, 1, 2, 3]

In [20]: b = numpy.unique(a)

In [21]: b
Out[21]: array([ 1,  2, 10, 99])

In [22]: c = range(len(b))

In [23]: c
Out[23]: [0, 1, 2, 3]

Seems like I should now be able to use b and c to translate from one array to the other. But what's the best (and quickest) way to do this?

like image 449
roblanf Avatar asked Sep 19 '26 05:09

roblanf


2 Answers

Don't know about quickest, but if you have scipy available, you can use scipy.stats.rankdata:

>>> a = np.array([[1, 2, 10],[1, 2, 99]])
>>> scipy.stats.rankdata(a,'dense').reshape(a.shape)-1
array([[ 0.,  1.,  2.],
       [ 0.,  1.,  3.]])

(The reshape is needed because it flattens the data first, and the -1 because it starts its ranks at 1.)

like image 92
DSM Avatar answered Sep 21 '26 05:09

DSM


the most straight forward way is using argsort()

a = numpy.array([0, 1, 1, 2])
u, ind = numpy.unique(a, return_inverse = True)
u = u.argsort().argsort()
ret = u[ind]
like image 27
Tal Darom Avatar answered Sep 21 '26 04:09

Tal Darom