import numpy as np
X = np.array([[0, 1, 0, 1],
[1, 1, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 1],
[0, 1, 1, 0],
[0, 1, 0, 1],
[1, 0, 0, 1]])
y = np.array([0, 1, 1, 0, 1, 0, 0])
counts = {}
for label in np.unique(y):
counts[label] = X[y == label].sum(axis=0)
print("{}".format(counts))
//output
{0: array([1, 2, 0, 4]), 1: array([1, 3, 3, 0])}
X[y==label]返回X中条件为true的行,sum(axis=0)做每一列的和,保存到字典中