bin_array

Contents

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, ...) and binfactor = (b1, b2, ...) (same length), the new shape will be (s1/b1, s2/b2, ...).

Parameters:
  • arr (numpy.ndarray) – Input array. Each s_i must be a multiple of the corresponding b_i.

  • binfactor (int or tuple of int) – Bin factor. If an integer, it is used for all axes. If its length is lower than arr.ndim, it is prepended with 1’s.

  • function (callable) – Reduction function accepting an axis argument, e.g. numpy.mean or numpy.sum.

Returns:

numpy.ndarray – Binned array of shape (s1/b1, s2/b2, ...).

See also

unbin_array

The opposite operation.

restride

The 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)