Concatenating data along one axis in numpy
I have a large 2-D array arr
that I would like to fit on the second axis using numpy. Since it np.histogram
aligns the array where I am using the for loop:
import numpy as np arr = np.random.randn(100, 100) nbins = 10 binned = np.empty((arr.shape[0], nbins)) for i in range(arr.shape[0]): binned[i,:] = np.histogram(arr[i,:], bins=nbins)[0]
I feel like there should be a more direct and more efficient way to do this in numpy, but I haven't been able to find one.
+6
source to share
2 answers
You can use np.apply_along_axis
:
x = np.array([range(20), range(1, 21), range(2, 22)]) nbins = 2 >>> np.apply_along_axis(lambda a: np.histogram(a, bins=nbins)[0], 1, x) array([[10, 10], [10, 10], [10, 10]])
The main benefit (if any) is that it is a little shorter, but I wouldn't expect much performance benefit. It is arguably marginally more efficient in collecting the results of each row.
+4
source to share