[SciPy-user] Count
Stefan van der Walt
stefan@sun.ac...
Thu Feb 21 17:38:44 CST 2008
On Thu, Feb 21, 2008 at 05:07:59PM -0500, David Warde-Farley wrote:
> Hi,
>
> One solution is to do a boolean comparison and then call sum() on the
> resulting boolean array. It'll treat the True's as 1's and so you end
> up with the number of occurrences.
>
> i.e.
>
> In [2]: x = array([2,2,2,2,3,4,5,6,7,8])
>
> In [3]: x == 2
> Out[3]: array([ True, True, True, True, False, False, False, False,
> False, False], dtype=bool)
>
> In [4]: sum(x == 2)
> Out[4]: 4
>
>
> There might be other ways, of course.
If you need to know the occurrence of all values, you can calculate
the histogram:
In [32]: x = np.array([2,2,2,2,3,4,5,6,7,8])
In [33]: np.histogram(x,np.unique(x))
Out[33]: (array([4, 1, 1, 1, 1, 1, 1]), array([2, 3, 4, 5, 6, 7, 8]))
Regards
Stefan
More information about the SciPy-user
mailing list