bundles / scipy 1.17.1 / scipy / stats / _binned_statistic / binned_statistic_2d
function
scipy.stats._binned_statistic:binned_statistic_2d
Signature
def binned_statistic_2d ( x , y , values , statistic = mean , bins = 10 , range = None , expand_binnumbers = False ) Summary
Compute a bidimensional binned statistic for one or more sets of data.
Extended Summary
This is a generalization of a histogram2d 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 (or set of values) within each bin.
Parameters
x: (N,) array_likeA sequence of values to be binned along the first dimension.
y: (N,) array_likeA sequence of values to be binned along the second dimension.
values: (N,) array_like or list of (N,) array_likeThe data on which the statistic will be computed. This must be the same shape as
x, or a list of sequences - each with the same shape asx. Ifvaluesis such a list, the statistic will be computed on each independently.statistic: string or callable, optionalThe 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.
'std'compute the standard deviation within each bin. This is implicitly calculated with ddof=0.
'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.
valuesarray 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.
functiona 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: int or [int, int] or array_like or [array, array], optionalThe bin specification:
the number of bins for the two dimensions (nx = ny = bins),
the number of bins in each dimension (nx, ny = bins),
the bin edges for the two dimensions (x_edge = y_edge = bins),
the bin edges in each dimension (x_edge, y_edge = bins).
If the bin edges are specified, the number of bins will be, (nx = len(x_edge)-1, ny = len(y_edge)-1).
range: (2,2) array_like, optionalThe leftmost and rightmost edges of the bins along each dimension (if not specified explicitly in the
binsparameters): [[xmin, xmax], [ymin, ymax]]. All values outside of this range will be considered outliers and not tallied in the histogram.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 (2,N) ndarray, where each row gives the bin numbers in the corresponding dimension. See the binnumber returned value, and the
Examplessection.
Returns
statistic: (nx, ny) ndarrayThe values of the selected statistic in each two-dimensional bin.
x_edge: (nx + 1) ndarrayThe bin edges along the first dimension.
y_edge: (ny + 1) ndarrayThe bin edges along the second dimension.
binnumber: (N,) array of ints or (2,N) ndarray of intsThis assigns to each element of
samplean integer that represents the bin in which this observation falls. The representation depends on theexpand_binnumbersargument. SeeNotesfor details.
Notes
Binedges: All but the last (righthand-most) bin is half-open. 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). Note that the returned linearized bin indices are used for an array with extra bins on the outer binedges to capture values outside of the defined bin bounds. If 'True': The returned binnumber is a shape (2,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 (D_edge[i-1], D_edge[i]), where 'D' is either 'x' or 'y'.
Array API Standard Support
binned_statistic_2d has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ⛔ PyTorch ⛔ ⛔ JAX ⛔ ⛔ Dask ⛔ n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
from scipy import stats
✓x = [0.1, 0.1, 0.1, 0.6] y = [2.1, 2.6, 2.1, 2.1] binx = [0.0, 0.5, 1.0] biny = [2.0, 2.5, 3.0] ret = stats.binned_statistic_2d(x, y, None, 'count', bins=[binx, biny]) ret.statistic✓
ret.binnumber
✓ret = stats.binned_statistic_2d(x, y, None, 'count', bins=[binx, biny], expand_binnumbers=True) ret.binnumber✓
See also
- binned_statistic
- binned_statistic_dd
- numpy.digitize
- numpy.histogram2d
Aliases
-
scipy.stats.binned_statistic_2d