bin_array#
- bin_array(arr, binfactor, function)[source]#
Bin an N-D array by a given factor, using a reduction function.
Let
arr.shape = (s1, s2, ...)andbinfactor = (b1, b2, ...)(same length), the new shape will be(s1/b1, s2/b2, ...).- Parameters:
arr (
numpy.ndarray) – Input array. Eachs_imust be a multiple of the correspondingb_i.binfactor (
intortupleofint) – Bin factor. If an integer, it is used for all axes. If its length is lower thanarr.ndim, it is prepended with 1’s.function (callable) – Reduction function accepting an
axisargument, e.g.numpy.meanornumpy.sum.
- Returns:
numpy.ndarray– Binned array of shape(s1/b1, s2/b2, ...).
See also
unbin_arrayThe opposite operation.
restrideThe underlying binning view.
Examples
>>> bin_array(np.arange(4*6).reshape(4, 6), 2, np.mean).shape (2, 3) >>> bin_array(np.arange(4*6).reshape(4, 6), (2,), np.mean).shape (4, 3) >>> bin_array(np.arange(4*6).reshape(4, 6), (2, 1), np.mean).shape (2, 6)