计算 numpy 数组中的样本数

counting the number of samples in a numpy array

我有一个 numpy 样本数组,[0, 0, 2.5, -5.0, ...]。在我的例子中,所有样本都是 2.5 的倍数。我想知道每个样本出现了多少次。或多或少像 numpy.hist。在这种情况下,类似于:[[-5.0, 1], [0, 2], [2.5, 1], ...].

您可以使用

[[x,l.count(x)] for x in set(l)]

输出

[[0, 2], [2.5, 1], [-5.0, 1]]

你也可以使用计数器

>>> l = [0,0,2.5,-5.0]
>>> from collections import Counter
>>> Counter(l)
Counter({0: 2, 2.5: 1, -5.0: 1})