import numpy as np
a = np.random.randint(0, 5, (50, 50))
rand = np.random.rand(5)
rand
b = np.take(rand, a)
np.bincount(a, weights=b)
np.bincount(a.flat, weights=b.flat)
Vectorizing feature hashing in python