[Numpy-discussion] how to tally the values seen
Wed Apr 14 01:10:59 CDT 2010
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
>>> counts = numpy.zeros(10, 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
>>> 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]
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 would equal 2.
What is the most efficient way to do this?
More information about the NumPy-Discussion