简体   繁体   English

计算 numpy 数组中一行中重复元素的数量

[英]Count number of repeated elements in a row in a numpy array

I'm looking for a quick way to do the following: Say I have an array我正在寻找一种快速的方法来执行以下操作:假设我有一个数组

X = np.array([1,1,1,2,2,2,2,2,3,3,1,1,0,0,0,5])

Instead of a simple frequency of elements I'm looking for the frequency in a row.我正在寻找连续的频率,而不是简单的元素频率。 So first 1 repeats 3 times, than 2 5 times, than 3 2 times, etc. So if freq is my function than:所以前 1 重复 3 次,比 2 重复 5 次,比 3 重复 2 次,等等。所以如果freq是我的 function 比:

Y = freq(X)
Y = np.array([[1,3],[2,5],[3,2],[1,2],[0,3],[5,1]])

For example, I can write this with loops like this:例如,我可以用这样的循环来写这个:

def freq(X):
    i=0        
    Y=[]
    while i<len(X):
        el = X[i]
        el_count=0
        while X[i]==el:
            el_count +=1
            i+=1
            if i==len(X):
                break            
        Y.append(np.array([el,el_count]))

    return np.array(Y)

I'm looking for a faster and nicer way to do this.我正在寻找一种更快更好的方法来做到这一点。 Thanks!谢谢!

Here's one NumPy way for performance efficiency -这是提高性能效率的一种 NumPy 方式 -

In [14]: m = np.r_[True,X[:-1]!=X[1:],True]

In [21]: counts = np.diff(np.flatnonzero(m))

In [22]: unq = X[m[:-1]]

In [23]: np.c_[unq,counts]
Out[23]: 
array([[1, 3],
       [2, 5],
       [3, 2],
       [1, 2],
       [0, 3],
       [5, 1]])

You can use itertools.groupby to perform the operation without invoking numpy .您可以使用itertools.groupby来执行操作,而无需调用numpy

import itertools

X = [1,1,1,2,2,2,2,2,3,3,1,1,0,0,0,5]

Y = [(x, len(list(y))) for x, y in itertools.groupby(X)]

print(Y)
# [(1, 3), (2, 5), (3, 2), (1, 2), (0, 3), (5, 1)]

If sorted output is OK, there is numpy.unique如果排序 output 是可以的,有numpy.unique

X = [1,1,1,2,2,2,2,2,3,3,1,1,0,0,0,5]

import numpy as np
(uniq, freq) = (np.unique(X, return_counts=True))
print(np.column_stack((uniq,freq)))
[[0 3]
 [1 5]
 [2 5]
 [3 2]
 [5 1]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM