# [Numpy-discussion] Array mapping question

josef.pktd at gmail.com josef.pktd at gmail.com
Tue Mar 16 08:54:21 EDT 2010

```On Tue, Mar 16, 2010 at 8:00 AM, Jorge Scandaliaris
<jorgesmbox-ml at yahoo.es> wrote:
> 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])

If I understand correctly, then you want return_inverse  (the original
array recoded to using integers 0...len(ctab)-1

help(np.unique)
...
return_inverse : bool, optional
If True, also return the indices of the unique array that can be used
to reconstruct `ar`.

Josef

>
> Am I overlooking a better way of doing this?
>
> Thanks,
>
> Jorge
>
>
>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

```