[Numpy-discussion] how to tally the values seen

Gökhan Sever gokhansever at gmail.com
Wed Apr 14 02:44:18 EDT 2010


On Wed, Apr 14, 2010 at 1:34 AM, Warren Weckesser <
warren.weckesser at enthought.com> wrote:

> Gökhan Sever wrote:
> >
> >
> > On Wed, Apr 14, 2010 at 1:10 AM, Peter Shinners <pete at shinners.org
> > <mailto:pete at shinners.org>> wrote:
> >
> >     I have an array that represents the number of times a value has been
> >     given. I'm trying to find a direct numpy way to add into these sums
> >     without requiring a Python loop.
> >
> >     For example, say there are 10 possible values. I start with an
> >     array of
> >     zeros.
> >
> >      >>> counts = numpy.zeros(10, numpy.int <http://numpy.int>)
> >
> >     Now I get an array with several values in them, I want to add into
> >     counts. All I can think of is a for loop that will give my the
> >     results I
> >     want.
> >
> >
> >      >>> values = numpy.array((2, 8, 1))
> >      >>> for v in values:
> >     ...    counts[v] += 1
> >      >>> print counts
> >     [0 1 1 0 0 0 0 0 1 0]
> >
> >
> > This is easy:
> >
> > I[3]: a
> > O[3]: array([ 0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.])
> >
> > I[4]: a = np.zeros(10)
> >
> > I[5]: b = np.array((2,8,1))
> >
> > I[6]: a[b] = 1
> >
> > I[7]: a
> > O[7]: array([ 0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.])
> >
> > Let me think about the other case :)
> >
> >
> >     I also need to handle the case where a value is listed more than
> once.
> >     So if values is (2, 8, 1, 2) then count[2] would equal 2.
> >
>
>
> numpy.bincount():
>
>
> In [1]: import numpy as np
>
> In [2]: x = np.array([2,8,1,2,7,7,2,7,0,2])
>
> In [3]: np.bincount(x)
> Out[3]: array([1, 1, 4, 0, 0, 0, 0, 3, 1])
>
>
>
I knew a function exists in numpy for this case too :)

This is also safer way to handle the given situation to prevent index out of
bounds errors.



>
> Warren
>
> >     What is the most efficient way to do this?
> >     _______________________________________________
> >     NumPy-Discussion mailing list
> >     NumPy-Discussion at scipy.org <mailto:NumPy-Discussion at scipy.org>
> >     http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
> >
> >
> >
> > --
> > Gökhan
> > ------------------------------------------------------------------------
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Gökhan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100414/0b08736c/attachment.html>


More information about the NumPy-Discussion mailing list