[SciPy-user] counting pairs of values

Chris Colbert sccolbert@gmail....
Thu Jul 16 09:31:24 CDT 2009


I don't know if its any more elegant than yours though:

>>> a
array([[1, 2],
       [3, 4],
       [5, 5],
       [1, 2]])
>>> len(np.where(a==[1, 2])[0])/2
2
>>> len(np.where(a==[3, 4])[0])/2
1
>>> len(np.where(a==[5, 5])[0])/2
1



2009/7/16 Ernest Adrogué <eadrogue@gmx.net>:
> Hi,
>
> I've got a series of observations that consist of pairs of
> values (x, y) and I'm interested in counting the number of
> occurrences of arbitrary pairs of values.
>
> For example, if "a" is a 2-dimensional array such as
>
> array([[1, 2],
>       [3, 4],
>       [5, 5],
>       [1, 2]])
>
> I want to find out how many (1, 2), or how many (3, 3) there
> are.
>
> Currently I'm doing this:
>
> numpy.logical_and(a[:,0] == 1, a[:,1] == 2).sum()
>
> which seems to work well enough, but I wonder if there's a
> more elegant/correct way of doing it.
>
>
> Ernest
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>


More information about the SciPy-user mailing list