[Numpy-discussion] permute and sum?

josef.pktd@gmai... josef.pktd@gmai...
Mon Feb 28 18:50:53 CST 2011


On Mon, Feb 28, 2011 at 6:50 PM, Angus McMorland <amcmorl@gmail.com> wrote:
> Hi all,
>
> I want to create a transpose of a vector, such that if the same index
> is given in the 'axes' list (as per np.transpose), then the sum of the
> original values sharing the same index is placed in the corresponding
> output index.
>
> For example:
>
> In: data  = np.array([5, 7, 9, 6, 2])
> In: order = np.array([0, 2, 0, 3, 1])
> In: permute_and_sum(data, order)
> Out: array([14, 2, 7, 6])

Not sure I fully understand, but isn't this
np.bincount(order, weights=data)

Josef

> I can obviously do this using the following,
>
> def permute_and_sum(data, order):
>   result = np.zeros(np.max(order) + 1)
>   for val, dest in zip(data, order):
>       result[dest] += val
>   return result
>
> but it seems like there's bound to be a much more elegant method I'm
> not currently seeing. Can anyone point me in the right direction?
>
> Thanks,
>
> Angus.
> --
> AJC McMorland
> Post-doctoral research fellow
> Neurobiology, University of Pittsburgh
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>


More information about the NumPy-Discussion mailing list