增量numpy的多-D反复数组索引 [英] Increment Numpy multi-d array with repeated indices

查看:141
本文介绍了增量numpy的多-D反复数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感兴趣的多维情况下,<一个href=\"http://stackoverflow.com/questions/2004364/increment-numpy-array-with-repeated-indices\">Increment numpy的数组索引重复的。

I'm interested in the multi-dimensional case of Increment Numpy array with repeated indices.

我有一个N维数组,一组N个索引数组,谁就是我想要增加值。该指数阵列可能有反复的条目。

I have an N-dimensional array and a set N index arrays, who's values I want to increment. The index arrays might have have repeated entries.

在不重复,该溶液是

a = arange(24).reshape(2,3,4)
i = array([0,0,1])
j = array([0,1,1])
k = array([0,0,3])
a[i,j,k] += 1

通过重复,(例如, J =阵列([0,0,2])),我无法做出numpy的递增重复。

With repeats, (ex. j=array([0,0,2]) ), I'm unable to make numpy increment the replicates.

推荐答案

这个怎么样:

import numpy as np
a = np.zeros((2,3,4))
i = np.array([0,0,1])
j = np.array([0,0,1])
k = np.array([0,0,3])

ijk = np.vstack((i,j,k)).T
H,edge = np.histogramdd(ijk,bins=a.shape)
a += H  

这篇关于增量numpy的多-D反复数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆