Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vectorizing numpy bincount

Tags:

python

numpy

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])
like image 677
user3556757 Avatar asked Nov 14 '16 15:11

user3556757


People also ask

What is Bincount in numpy?

bincount() method counts the occurrence of each element. Each bin value is the occurrence of its index. One can also set the bin size accordingly.

What is bin count?

The BIN Count function counts the frequency by incrementing the BIN count corresponding to the BIN Sorting result by 1 each time the measurement ends. The frequency of each BIN can be viewed on the display when necessary.


Video Answer


1 Answers

Using the same philosophy as in this post, here's a vectorized approach -

m = A.shape[1]    
n = A.max()+1
A1 = A + (n*np.arange(m))
out = np.bincount(A1.ravel(),minlength=n*m).reshape(m,-1).T
like image 69
Divakar Avatar answered Oct 14 '22 21:10

Divakar