This question already has an answer here:
这个问题在这里已有答案:
- numpy: most efficient frequency counts for unique values in an array 14 answers
numpy:阵列14答案中唯一值的最有效频率计数
Given an numpy array a
, I can use np.unique(a) to generate the unique values included in a
. How can I get the count of each unique values included in a
?
给定一个numpy数组a,我可以使用np.unique(a)来生成包含在a中的唯一值。如何获得包含在每个唯一值中的计数?
1 个解决方案
#1
3
Since you mentioned np.unique
, it accepts a second argument called return_counts
:
既然你提到了np.unique,它就会接受一个名为return_counts的第二个参数:
u, c = np.unique(a, return_counts=True)
c[i]
is the corresponding count for u[i]
.
c [i]是u [i]的对应计数。
#1
3
Since you mentioned np.unique
, it accepts a second argument called return_counts
:
既然你提到了np.unique,它就会接受一个名为return_counts的第二个参数:
u, c = np.unique(a, return_counts=True)
c[i]
is the corresponding count for u[i]
.
c [i]是u [i]的对应计数。