I have a 2d numpy array., A
I want to apply np.bincount()
to each column of the matrix A
to generate another 2d array B
that is composed of the bincounts of each column of the original matrix A
.
My problem is that np.bincount() is a function that takes a 1d array-like. It's not an array method like B = A.max(axis=1)
for example.
Is there a more pythonic/numpythic way to generate this B
array other than a nasty for-loop?
import numpy as np
states = 4
rows = 8
cols = 4
A = np.random.randint(0,states,(rows,cols))
B = np.zeros((states,cols))
for x in range(A.shape[1]):
B[:,x] = np.bincount(A[:,x])