If you're applying this to an array that has many rows this function will give you some speed up at the cost of some temporary memory.
def hist_per_row(data, bins):
data = np.asarray(data)
assert np.all(bins[:-1] <= bins[1:])
r, c = data.shape
idx = bins.searchsorted(data)
step = len(bins) + 1
last = step * r
idx += np.arange(0, last, step).reshape((r, 1))
res = np.bincount(idx.ravel(), minlength=last)
res = res.reshape((r, step))
return res[:, 1:-1]
The res[:, 1:-1]
on the last line is to be consistent with numpy.histogram which returns an array with len len(bins) - 1
, but you could drop it if you want to count values that are less than and greater than bins[0]
and bins[-1]
respectively.