2015-07-19 33 views
3

例如,如果我有:如何计算元素的矢量的NUM与numpy的蟒蛇

a=np.array([[1,1,4,1,4,3,1]]) 

我们可以看到,我们有数字1的四倍,4号两次,3只的。

我想有以下结果:

array(4,4,2,4,2,1,4) 

正如你可以看到:每个单元由计数的它的元素所取代。

我该如何以最有效的方式做到这一点?

+0

[numpy:数组中唯一值的频率计数]的可能重复(http://stackoverflow.com/questions/10741346/numpy-frequency-counts-for-unique-values-in-an-array) –

回答

3

一个vectorized方法与np.uniquenp.searchsorted -

# Get unique elements and their counts 
unq,counts = np.unique(a,return_counts=True) 

# Get the positions of unique elements in a. 
# Use those positions to index into counts array for final output. 
out = counts[np.searchsorted(unq,a.ravel())] 

采样运行 -

In [86]: a 
Out[86]: array([[1, 1, 4, 1, 4, 3, 1]]) 

In [87]: out 
Out[87]: array([4, 4, 2, 4, 2, 1, 4]) 

按照从@Jaime的评论,你可以使用np.unique单独像这样 -

_, inv_idx, counts = np.unique(a, return_inverse=True, return_counts=True) 
out = counts[inv_idx] 
+0

您可以从'unique'获得与'_,inv,cnt = np.unique(a,return_inverse = True,return_counts = True)'相同的结果',然后'cnt [inv]'会给你OP后面的内容。 – Jaime

+0

@Jaime将这些添加为编辑。感谢您的意见! – Divakar

0

使用collections.Counter

from collections import Counter 
ctr = Counter(a.flat) 
result = np.array([ctr[i] for i in a.flat]) 

如果你希望你的result具有相同的尺寸a,使用reshape

result = result.reshape(a.shape) 
0

我试图既numpy的和计数器相结合:

from collections import Counter 
a=np.array([[1,1,4,1,4,3,1]]) 

# First I count the occurence of every element and stor eit in the dict-like counter 
# Then I take its get-method and vectorize it for numpy-array usage 
vectorized_counter = np.vectorize(Counter(a.flatten()).get) 

vectorized_counter(a) 

出:

array([[4, 4, 2, 4, 2, 1, 4]])