DIEA
>>> 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}非numpy方式:使用collections.Counter;>> import collections, numpy>>> a = numpy.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])>>> collections.Counter(a)Counter({0: 7, 1: 4, 3: 2, 2: 1, 4: 1})
泛舟湖上清波郎朗
关于使用什么numpy.count_nonzero,像什么>>> 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
白衣非少年
就个人而言,我会去: (y == 0).sum()和(y == 1).sum()例如import numpy as npy = np.array([0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1])num_zeros = (y == 0).sum()num_ones = (y == 1).sum()