小编典典

有效地计算NumPy中唯一子数组的出现次数?

python

我有一个形状数组,(128, 36, 8)我想找到最后一个维度中长度为8的唯一子数组的出现次数。

我知道np.uniquenp.bincount,但是这些似乎是针对元素而不是子数组的。我已经看到了这个问题,但这是关于查找特定子数组的首次出现,而不是查找所有唯一子数组的计数。


阅读 159

收藏
2021-01-16

共1个答案

小编典典

问题指出输入数组的形状(128, 36, 8),我们有兴趣在8最后一个维度中找到长度唯一的子数组。因此,我假设唯一性是沿着合并在一起的前两个维度。让我们假设A作为输入3D阵列。

获取唯一子数组的数量

# Reshape the 3D array to a 2D array merging the first two dimensions
Ar = A.reshape(-1,A.shape[2])

# Perform lex sort and get the sorted indices and xy pairs
sorted_idx = np.lexsort(Ar.T)
sorted_Ar =  Ar[sorted_idx,:]

# Get the count of rows that have at least one TRUE value 
# indicating presence of unique subarray there
unq_out = np.any(np.diff(sorted_Ar,axis=0),1).sum()+1

样品运行-

In [159]: A # A is (2,2,3)
Out[159]: 
array([[[0, 0, 0],
        [0, 0, 2]],

       [[0, 0, 2],
        [2, 0, 1]]])

In [160]: unq_out
Out[160]: 3

获取唯一子数组的出现次数

# Reshape the 3D array to a 2D array merging the first two dimensions
Ar = A.reshape(-1,A.shape[2])

# Perform lex sort and get the sorted indices and xy pairs
sorted_idx = np.lexsort(Ar.T)
sorted_Ar =  Ar[sorted_idx,:]

# Get IDs for each element based on their uniqueness
id = np.append([0],np.any(np.diff(sorted_Ar,axis=0),1).cumsum())

# Get counts for each ID as the final output
unq_count = np.bincount(id)

样品运行-

In [64]: A
Out[64]: 
array([[[0, 0, 2],
        [1, 1, 1]],

       [[1, 1, 1],
        [1, 2, 0]]])

In [65]: unq_count
Out[65]: array([1, 2, 1], dtype=int64)
2021-01-16