[Numpy-discussion] Array mapping question

Jorge Scandaliaris jorgesmbox-ml@yahoo...
Tue Mar 16 07:00:59 CDT 2010


Hi,
I have a 1D array containing indexes to specific measurements. As this array is
a slice of a bigger one, the indexes don't necessarily start at 0 nor they are
sequential. For example, I can have an array A where

In [34]: A.shape
Out[34]: (4764,)
In [35]: ctab = np.unique(A)
In [36]: ctab
Out[36]: array([48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62],
dtype=int64)

I would like to map these indexes to a sequence starting from zero. The usual
look up table approach doesn't work here. I can solve this using a dictionary,
but then I am forced to using a loop or a list comprehension:

In [38]: cdic = dict(zip(ctab, range(ctab.size)))
In [39]: cdic
Out[39]: 
{48: 0,
 49: 1,
 50: 2,
 51: 3,
 52: 4,
 53: 5,
 54: 6,
 55: 7,
 56: 8,
 57: 9,
 58: 10,
 59: 11,
 60: 12,
 61: 13,
 62: 14}

A_remapped = np.asarray([cdic[x] for x in A])

Am I overlooking a better way of doing this?

Thanks,

Jorge







More information about the NumPy-Discussion mailing list