<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
On 04/13/2010 11:44 PM, Gökhan Sever wrote:
<blockquote
 cite="mid:z2z49d6b3501004132344ua838ef12i8ad13792357cbe0@mail.gmail.com"
 type="cite"><br>
  <br>
  <div class="gmail_quote">On Wed, Apr 14, 2010 at 1:34 AM, Warren
Weckesser <span dir="ltr"><<a moz-do-not-send="true"
 href="mailto:warren.weckesser@enthought.com">warren.weckesser@enthought.com</a>></span>
wrote:<br>
  <blockquote class="gmail_quote"
 style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
    <div class="im">Gökhan Sever wrote:<br>
><br>
><br>
> On Wed, Apr 14, 2010 at 1:10 AM, Peter Shinners <<a
 moz-do-not-send="true" href="mailto:pete@shinners.org">pete@shinners.org</a><br>
    </div>
    <div class="im">> <mailto:<a moz-do-not-send="true"
 href="mailto:pete@shinners.org">pete@shinners.org</a>>> wrote:<br>
><br>
>     I have an array that represents the number of times a value
has been<br>
>     given. I'm trying to find a direct numpy way to add into these
sums<br>
>     without requiring a Python loop.<br>
><br>
>     For example, say there are 10 possible values. I start with an<br>
>     array of<br>
>     zeros.<br>
><br>
    </div>
>      >>> counts = numpy.zeros(10, <a
 moz-do-not-send="true" href="http://numpy.int" target="_blank">numpy.int</a>
<<a moz-do-not-send="true" href="http://numpy.int" target="_blank">http://numpy.int</a>>)<br>
    <div class="im">><br>
>     Now I get an array with several values in them, I want to add
into<br>
>     counts. All I can think of is a for loop that will give my the<br>
>     results I<br>
>     want.<br>
><br>
><br>
>      >>> values = numpy.array((2, 8, 1))<br>
>      >>> for v in values:<br>
>     ...    counts[v] += 1<br>
>      >>> print counts<br>
>     [0 1 1 0 0 0 0 0 1 0]<br>
><br>
><br>
> This is easy:<br>
><br>
> I[3]: a<br>
> O[3]: array([ 0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.])<br>
><br>
> I[4]: a = np.zeros(10)<br>
><br>
> I[5]: b = np.array((2,8,1))<br>
><br>
> I[6]: a[b] = 1<br>
><br>
> I[7]: a<br>
> O[7]: array([ 0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.])<br>
><br>
> Let me think about the other case :)<br>
><br>
><br>
>     I also need to handle the case where a value is listed more
than once.<br>
>     So if values is (2, 8, 1, 2) then count[2] would equal 2.<br>
><br>
    <br>
    <br>
    </div>
numpy.bincount():<br>
    <br>
    <br>
In [1]: import numpy as np<br>
    <br>
In [2]: x = np.array([2,8,1,2,7,7,2,7,0,2])<br>
    <br>
In [3]: np.bincount(x)<br>
Out[3]: array([1, 1, 4, 0, 0, 0, 0, 3, 1])<br>
    <br>
    <br>
  </blockquote>
  <div><br>
I knew a function exists in numpy for this case too :) <br>
  <br>
This is also safer way to handle the given situation to prevent index
out of bounds errors.<br>
  </div>
  </div>
</blockquote>
<br>
Thanks guys. Numpy always surprises me. It's like you guys have
borrowed Guido's time machine.<br>
This is running far faster than I ever hoped.<br>
<br>
My next problem involves a bit of indexing and reordering. But I'm
gonna spend a night of my own time to see where I can get with it.<br>
</body>
</html>