np.unique for one bi-dimensional array
Hi, would like to identify unique pairs of numbers in two arrays o in one bi-dimensional array, and count the observation a_clean=array([4,4,5,4,4,4]) b_clean=array([3,5,4,4,3,4]) and obtain (4,3,2) (4,5,1) (5,4,1) (4,4,2) I solved with tow loops but off course there will be a fast solution Any idea? what about using np.unique for one bi-dimensional array? In bash I usually unique command thanks in advance Giuseppe -- Giuseppe Amatulli Web: www.spatial-ecology.net
Hi, On Wed, Jul 25, 2012 at 1:09 AM, Giuseppe Amatulli < giuseppe.amatulli@gmail.com> wrote:
Hi,
would like to identify unique pairs of numbers in two arrays o in one bi-dimensional array, and count the observation
a_clean=array([4,4,5,4,4,4]) b_clean=array([3,5,4,4,3,4])
and obtain (4,3,2) (4,5,1) (5,4,1) (4,4,2)
I solved with tow loops but off course there will be a fast solution Any idea? what about using np.unique for one bi-dimensional array?
According the docs http://docs.scipy.org/doc/numpy/reference/generated/numpy.unique.html np.unique will flatten the input to 1D array. However, perhaps something like the following lines will help you: In []: lot= zip(a_clean, b_clean) In []: lot Out[]: [(4, 3), (4, 5), (5, 4), (4, 4), (4, 3), (4, 4)] In []: [[x, lot.count(x)] for x in set(lot)] Out[]: [[(4, 5), 1], [(5, 4), 1], [(4, 4), 2], [(4, 3), 2]] My 2 cents, -eat
In bash I usually unique command
thanks in advance Giuseppe
-- Giuseppe Amatulli Web: www.spatial-ecology.net _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
There are various ways to repack the pair of arrays into one array. The most universal is probably to use structured array (can repack more than a pair): x = np.array(zip(a, b), dtype=[('a',int), ('b',int)]) After repacking you can use unique and other numpy methods: xu = np.unique(x) zip(xu['a'], xu['b'], np.bincount(np.searchsorted(xu, x))) [(4, 3, 2), (4, 4, 2), (4, 5, 1), (5, 4, 1)] Val On Tue, Jul 24, 2012 at 5:09 PM, Giuseppe Amatulli < giuseppe.amatulli@gmail.com> wrote:
Hi,
would like to identify unique pairs of numbers in two arrays o in one bi-dimensional array, and count the observation
a_clean=array([4,4,5,4,4,4]) b_clean=array([3,5,4,4,3,4])
and obtain (4,3,2) (4,5,1) (5,4,1) (4,4,2)
I solved with tow loops but off course there will be a fast solution Any idea? what about using np.unique for one bi-dimensional array?
In bash I usually unique command
thanks in advance Giuseppe
-- Giuseppe Amatulli Web: www.spatial-ecology.net _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
Hi, would like to identify unique pairs of numbers in two arrays o in one bi-dimensional array, and count the observation a_clean=array([4,4,5,4,4,4]) b_clean=array([3,5,4,4,3,4]) and obtain (4,3,2) (4,5,1) (5,4,1) (4,4,2) I solved with tow loops but off course there will be a faster solution. I was checking also for np.unique but i did not find how to apply for a bi-dimensional array. or Concatenate the two arrays a_concatenate=array([4_3,4_5,5_4,4_4,4_3,4_4]), then np.unique, then split again. Any other/faster solutions? In bash I usually unique command Thanks in advance -- Giuseppe Amatulli Web: www.spatial-ecology.net
Hi,
would like to identify unique pairs of numbers in two arrays o in one bi-dimensional array, and count the observation
a_clean=array([4,4,5,4,4,4]) b_clean=array([3,5,4,4,3,4])
and obtain (4,3,2) (4,5,1) (5,4,1) (4,4,2)
I solved with tow loops but off course there will be a faster solution.
I was checking also
for np.unique but i did not find how to apply for a bi-dimensional array. or Concatenate the two arrays a_concatenate=array([4_3,4_5,5_4,4_4,4_3,4_4]), then np.unique, then split again.
Any other/faster solutions? In bash I usually unique command Thanks in advance
I'd try np.histogram2d, but probably only because I don't know np.unique. A.
участники (4)
-
Andreas Hilboll
-
eat
-
Giuseppe Amatulli
-
Val Kalatsky