Using numpy.unique:
Use with AIimport numpy
a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
unique, counts = numpy.unique(a, return_counts=True)
>>> dict(zip(unique, counts))
{0: 7, 1: 4, 2: 1, 3: 2, 4: 1}
Non-numpy method using collections.Counter;
Use with AIimport collections, numpy
a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
counter = collections.Counter(a)
>>> counter
Counter({0: 7, 1: 4, 3: 2, 2: 1, 4: 1})
Answer from Ozgur Vatansever on Stack OverflowUsing numpy.unique:
Use with AIimport numpy
a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
unique, counts = numpy.unique(a, return_counts=True)
>>> dict(zip(unique, counts))
{0: 7, 1: 4, 2: 1, 3: 2, 4: 1}
Non-numpy method using collections.Counter;
Use with AIimport collections, numpy
a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
counter = collections.Counter(a)
>>> counter
Counter({0: 7, 1: 4, 3: 2, 2: 1, 4: 1})
What about using numpy.count_nonzero, something like
Use with AI>>> import numpy as np
>>> y = np.array([1, 2, 2, 2, 2, 0, 2, 3, 3, 3, 0, 0, 2, 2, 0])
>>> np.count_nonzero(y == 1)
1
>>> np.count_nonzero(y == 2)
7
>>> np.count_nonzero(y == 3)
3
In [1043]: np.unique(np.array([1,1,2,3,4,5,5,5,6,7,7,7,7]),return_counts=True)
Out[1043]: (array([1, 2, 3, 4, 5, 6, 7]), array([2, 1, 1, 1, 3, 1, 4]))
You can use np.bincount :
>>> import numpy as np
>>> a = np.array([1,1,2,3,4,5,5,5,6,7,7,7,7])
>>> b = np.bincount(a)
>>> b[np.unique(a)]
array([2, 1, 1, 1, 3, 1, 4])
And to get the other array :
>>> np.unique(a)
array([1, 2, 3, 4, 5, 6, 7])
In numpy you can use numpy.unique and then concatenate the unique elements with their counts.
import numpy as np
m = np.array([[0., 143.],
[0., 170.],
[0., 143.],
[1., 269.],
[0., 170.],
[1., 269.],
[0., 155.],
[1., 269.]])
rows, counts = np.unique(m, axis=0, return_counts=True)
result = np.concatenate([rows, counts[:, None]], axis=1)
or equivalently
result = np.column_stack(np.unique(m, axis=0, return_counts=True))
which both result in
[[ 0. 143. 2.]
[ 0. 155. 1.]
[ 0. 170. 2.]
[ 1. 269. 3.]]
Note that the output order of numpy.unique is not stable. If the order of the output needs to match the order of first occurrences then you can numpy.argsort the result with respect to the indices returned by numpy.unique.
rows, indices, counts = np.unique(m, axis=0, return_index=True, return_counts=True)
result = np.concatenate([rows, counts[:, None]], axis=1)[np.argsort(indices)]
which results in
[[ 0. 143. 2.]
[ 0. 170. 2.]
[ 1. 269. 3.]
[ 0. 155. 1.]]
There is torch.bincount, but it "only supports 1-d non-negative integral inputs".
Anyway, if you are to count elements, due to float precision errors it is better to work with integers.
For the > 1-d case, you can create a custom function:
def count(x):
unique_values = set(x)
counts = [
(
tuple(v.tolist()),
(x == v).all(axis=-1).sum().item()
)
for v in unique_values
]
return dict(counts)
Which gives counts as a dictionary, similar to standard library Counter, that you can reformat later to suit your needs:
>>> count(matrix.to(int))
{(1, 269): 3, (0, 170): 2, (0, 143): 2, (0, 155): 1}