有效计数numpy的唯一子阵列出现的次数? [英] Efficiently count the number of occurrences of unique subarrays in NumPy?
问题描述
我有形状阵列(128,36,8)
,我想找到的最后一个长度为8的唯一子阵列出现的次数尺寸。
I have an array of shape (128, 36, 8)
and I'd like to find the number of occurrences of the unique subarrays of length 8 in the last dimension.
我所知道的 np.unique
和 np.bincount
,但这些似乎是元素,而比子阵。我见过这个问题,但它是寻找特定的第一次出现子阵,而不是将所有子阵独特的计数。
I'm aware of np.unique
and np.bincount
, but those seem to be for elements rather than subarrays. I've seen this question but it's about finding the first occurrence of a particular subarray, rather than the counts of all unique subarrays.
推荐答案
的问题指出,输入数组是形状(128,36,8)
和我们有意在最后一维发现长度 8
独特的子阵列。
所以,我假设的独特之处是沿着前两个维度被合并在一起。让我们假设 A
作为输入三维阵列。
The question states that the input array is of shape (128, 36, 8)
and we are interested in finding unique subarrays of length 8
in the last dimension.
So, I am assuming that the uniqueness is along the first two dimensions being merged together. Let us assume A
as the input 3D array.
获取独特的子阵数
# 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)
这篇关于有效计数numpy的唯一子阵列出现的次数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!