Можно предложить два метода.
С np.add.at
-
heat = np.zeros(shape,dtype=int)
np.add.at(heat,(a[0],a[1]),1)
Или с tuple()
на основе более эстетического одного -
np.add.at(heat,tuple(a),1)
С bincount
-
idx = np.ravel_multi_index(a,shape)
np.bincount(idx,minlength=np.prod(shape)).reshape(shape)
Кроме того, мы можем вычислить shape
, используя максимальные пределы индексов в a
-
shape = a.max(axis=1)+1
Пробный прогон -
In [147]: a
Out[147]:
array([[0, 1, 2, 0, 1, 2],
[0, 1, 0, 0, 0, 2]])
In [148]: shape = (3,3)
In [149]: heat = np.zeros(shape,dtype=int)
...: np.add.at(heat,(a[0],a[1]),1)
In [151]: heat
Out[151]:
array([[2, 0, 0],
[1, 1, 0],
[1, 0, 1]])
In [173]: idx = np.ravel_multi_index(a,shape)
In [174]: np.bincount(idx,minlength=np.prod(shape)).reshape(shape)
Out[174]:
array([[2, 0, 0],
[1, 1, 0],
[1, 0, 1]])