SciPy

scipy.stats.binned_statistic_dd

scipy.stats.binned_statistic_dd(sample, values, statistic='mean', bins=10, range=None, expand_binnumbers=False)[source]

Compute a multidimensional binned statistic for a set of data.

This is a generalization of a histogramdd function. A histogram divides the space into bins, and returns the count of the number of points in each bin. This function allows the computation of the sum, mean, median, or other statistic of the values within each bin.

Parameters:
sample : array_like

Data to histogram passed as a sequence of D arrays of length N, or as an (N,D) array.

values : (N,) array_like or list of (N,) array_like

The data on which the statistic will be computed. This must be the same shape as sample, or a list of sequences - each with the same shape as sample. If values is such a list, the statistic will be computed on each independently.

statistic : string or callable, optional

The statistic to compute (default is ‘mean’). The following statistics are available:

  • ‘mean’ : compute the mean of values for points within each bin. Empty bins will be represented by NaN.
  • ‘median’ : compute the median of values for points within each bin. Empty bins will be represented by NaN.
  • ‘count’ : compute the count of points within each bin. This is identical to an unweighted histogram. values array is not referenced.
  • ‘sum’ : compute the sum of values for points within each bin. This is identical to a weighted histogram.
  • ‘min’ : compute the minimum of values for points within each bin. Empty bins will be represented by NaN.
  • ‘max’ : compute the maximum of values for point within each bin. Empty bins will be represented by NaN.
  • function : a user-defined function which takes a 1D array of values, and outputs a single numerical statistic. This function will be called on the values in each bin. Empty bins will be represented by function([]), or NaN if this returns an error.
bins : sequence or int, optional

The bin specification must be in one of the following forms:

  • A sequence of arrays describing the bin edges along each dimension.
  • The number of bins for each dimension (nx, ny, … = bins).
  • The number of bins for all dimensions (nx = ny = … = bins).
range : sequence, optional

A sequence of lower and upper bin edges to be used if the edges are not given explicitly in bins. Defaults to the minimum and maximum values along each dimension.

expand_binnumbers : bool, optional

‘False’ (default): the returned binnumber is a shape (N,) array of linearized bin indices. ‘True’: the returned binnumber is ‘unraveled’ into a shape (D,N) ndarray, where each row gives the bin numbers in the corresponding dimension. See the binnumber returned value, and the Examples section of binned_statistic_2d.

New in version 0.17.0.

Returns:
statistic : ndarray, shape(nx1, nx2, nx3,…)

The values of the selected statistic in each two-dimensional bin.

bin_edges : list of ndarrays

A list of D arrays describing the (nxi + 1) bin edges for each dimension.

binnumber : (N,) array of ints or (D,N) ndarray of ints

This assigns to each element of sample an integer that represents the bin in which this observation falls. The representation depends on the expand_binnumbers argument. See Notes for details.

Notes

Binedges: All but the last (righthand-most) bin is half-open in each dimension. In other words, if bins is [1, 2, 3, 4], then the first bin is [1, 2) (including 1, but excluding 2) and the second [2, 3). The last bin, however, is [3, 4], which includes 4.

binnumber: This returned argument assigns to each element of sample an integer that represents the bin in which it belongs. The representation depends on the expand_binnumbers argument. If ‘False’ (default): The returned binnumber is a shape (N,) array of linearized indices mapping each element of sample to its corresponding bin (using row-major ordering). If ‘True’: The returned binnumber is a shape (D,N) ndarray where each row indicates bin placements for each dimension respectively. In each dimension, a binnumber of i means the corresponding value is between (bin_edges[D][i-1], bin_edges[D][i]), for each dimension ‘D’.

New in version 0.11.0.

Previous topic

scipy.stats.binned_statistic_2d

Next topic

scipy.stats.f_oneway