0

I have a given numpy array, data. I want to change its values in the ascending order from 0. I could solve it by manually making the look up table, LUT.

import numpy as np

data = np.array([[2, 6, 16, 39, 43],
                        [43, 6, 16, 39, 2]])


LUT={2:0,
    6:1,
    16:2,
    39:3,
    43:4} 

def changer(X):   
    res = np.copy(X)    
    for i, j in LUT.items():
        res[X==i] = j
    return res       

result = changer(data)
print (result)

[[0 1 2 3 4]
 [4 1 2 3 0]]

The result is correct as I expected. However, sometimes, I am lazy to make LUT manually. So, how could I get the same results programmatically?

edit

I tried to make dictionary, LUT as follows.

list = [2,6,16,39,43]

LUT = {}
for i in enumerate(list):
    LUT.update({list[i]: i})

But, 

D.update({list[i]: i})
TypeError: list indices must be integers, not tuple
Community
  • 1
  • 1
  • It is not a good idea to hard code any look up tables in high level code into python! Except with some edge case scenarios this will hurt you. Please explain what you are trying to do and why are you trying to do so. – not_a_bot_no_really_82353 Dec 10 '18 at 18:46
  • 1
    Possible duplicate of [Rank items in an array using Python/NumPy](https://stackoverflow.com/questions/5284646/rank-items-in-an-array-using-python-numpy) – Ben.T Dec 10 '18 at 18:48
  • Are you looking for something like this?`dict(enumerate([2, 6, 16, 39, 43]))` – mad_ Dec 10 '18 at 18:49
  • @mad_ yes. Could you see my edit? –  Dec 10 '18 at 18:52
  • @mad_ how can i inverse, dict(enumerate([2, 6, 16, 39, 43]))? –  Dec 10 '18 at 18:54
  • By converting values to keys `dict(zip(d.values(),d.keys()))` – mad_ Dec 10 '18 at 18:55
  • thanks @mad_ it solved my problem . –  Dec 10 '18 at 19:03

4 Answers4

0

Use np.unique with the return_inverse flag:

data = np.array([[2, 6, 16, 39, 43],
                 [43, 6, 16, 39, 2]])

unq, ranked = np.unique(data, return_inverse=True)

unique without axis set flattens, so we need to reshape

ranked = ranked.reshape(data.shape)
ranked
# array([[0, 1, 2, 3, 4],
#        [4, 1, 2, 3, 0]])
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
0

If I understand what you want, np_lu should be automatically generated from your data.

In [24]: np_lu=sorted(set(data.ravel()))
In [25]: np_lu
Out[25]: [2, 6, 16, 39, 43]

Create an array of indices 2->0, 6->1, ... 43->4

In [29]: indx=np.array(np_lu)
In [33]: d=np.zeros(indx.max()+1)
In [34]: d[indx]=np.arange(len(indx))
In [35]: d
Out[35]:
array([ 0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,
    0.,  0.,  0.,  2.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
    0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,
    3.,  0.,  0.,  0.,  4.])

In [36]: data
Out[36]:
array([[ 2,  6, 16, 39, 43],
       [43,  6, 16, 39,  2]])

Create a result array, res.

In [37]: res=np.zeros_like(data)

Fill res with the data in d, indexed by data.

In [41]: res=d[data]
In [42]: res
Out[42]:
array([[ 0.,  1.,  2.,  3.,  4.],
       [ 4.,  1.,  2.,  3.,  0.]])

This will only work for data of int type. There's probably a lot of tidying up that can be done in my code.

Tls Chris
  • 3,564
  • 1
  • 9
  • 24
0

I think this is really np.argsort:

Returns the indices that would sort an array.

import numpy as np
data = np.array([[2, 6, 16, 39, 43],
                        [43, 6, 16, 39, 2]])
data.argsort(1).tolist()

Output:

[[0, 1, 2, 3, 4], [4, 1, 2, 3, 0]]

And to go backwards giving 1D array, l, and 2D array of indexes, iarr:

l = np.array([2, 6, 16, 39, 43])
iarr = np.array([[0, 1, 2, 3, 4], [4, 1, 2, 3, 0]])

We can use broadcasting:

l[iarr]

Output:

array([[ 2,  6, 16, 39, 43],
       [43,  6, 16, 39,  2]])
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
-1

if i get right your needs , all you need is:

import numpy as np
lst = np.array([[i for i in range(len(data[0]))],[i for i in range(len(data[1])-1,-1,-1)]])
masasa
  • 260
  • 1
  • 9
  • Nope. Not even close. OP is looking for functionality similar to lookup. Your code just iterates and print index of items in some fashion. – mad_ Dec 10 '18 at 19:17
  • he wanted to use lookup as a method to resolve his problems, looking over his desired outcome , i think this nails it... – masasa Dec 10 '18 at 19:18
  • Can you confirm if the output matches? This does not give the desired results. Thanks – mad_ Dec 10 '18 at 19:22
  • @mad_ i ran it on his initial data set (array with 2 lists)with my commands and it worked – masasa Dec 10 '18 at 19:26