Array mapping question
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
On Tue, Mar 16, 2010 at 8:00 AM, Jorge Scandaliaris
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@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Tue, Mar 16, 2010 at 9:33 AM, Jorge Scandaliaris
writes: If I understand correctly, then you want return_inverse (the original array recoded to using integers 0...len(ctab)-1
Josef
Right, thanks! I didn't see this cause I use numpy 1.3, where this is not available.
If I remember correctly it was unique1d in numpy 1.3 that had the return_inverse options. Check the functions in arraysetops for numpy 1.3. unique1d is in my help file for numpy 1.2 (which was the fastest for me to look up) Josef
Jorge
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
If I remember correctly it was unique1d in numpy 1.3 that had the return_inverse options.
Check the functions in arraysetops for numpy 1.3. unique1d is in my help file for numpy 1.2 (which was the fastest for me to look up)
You're right, again. unique1d is in numpy 1.3 Thanks, Jorge
participants (2)
-
Jorge Scandaliaris
-
josef.pktd@gmail.com