[Numpy-discussion] an indexing question

Jaime Fernández del Río jaime.frio at gmail.com
Wed Jan 8 14:32:19 EST 2014

On Wed, Jan 8, 2014 at 11:12 AM, Neal Becker <ndbecker2 at gmail.com> wrote:

> I have a 1d vector d.  I want compute the means of subsets of this vector.
> The subsets are selected by looking at another vector s or same shape as d.
> This can be done as:
> [np.mean (d[s == i]) for i in range (size)]
> But I think this could be done directly with numpy addressing, without
> resorting
> to list comprehension?

You could get it done with np.bincount:

d_sums = np.bincount(s, weights=d)
d_counts = np.bincount(s)
d_means = d_sums / d_counts


( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
de dominación mundial.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140108/bf6933b7/attachment.html>

More information about the NumPy-Discussion mailing list