[Numpy-discussion] Fancier indexing

Mark Miller mark.miller@usu....
Thu May 22 11:15:17 CDT 2008


You're just trying to do this...correct?

>>> import numpy
>>> items = numpy.array([0,3,2,1,4,2],dtype=int)
>>> unique = numpy.unique(items)
>>> unique
array([0, 1, 2, 3, 4])
>>> counts=numpy.histogram(items,unique)
>>> counts
(array([1, 1, 2, 1, 1]), array([0, 1, 2, 3, 4]))
>>> counts[0]
array([1, 1, 2, 1, 1])
>>>

On Thu, May 22, 2008 at 9:08 AM, Keith Goodman <kwgoodman@gmail.com> wrote:

> On Thu, May 22, 2008 at 8:59 AM, Kevin Jacobs <jacobs@bioinformed.com>
> <bioinformed@gmail.com> wrote:
> > After poking around for a bit, I was wondering if there was a faster
> method
> > for the following:
> >
> > # Array of index values 0..n
> > items = numpy.array([0,3,2,1,4,2],dtype=int)
> >
> > # Count the number of occurrences of each index
> > counts = numpy.zeros(5, dtype=int)
> > for i in items:
> >   counts[i] += 1
> >
> > In my real code, 'items' contain up to a million values and this loop
> will
> > be in a performance critical area of code.  If there is no simple
> solution,
> > I can trivially code this using the C-API.
>
> How big is n? If it is much smaller than a million then loop over that
> instead.
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080522/b3b27b92/attachment.html 


More information about the Numpy-discussion mailing list