bundles / scipy 1.17.1 / scipy / stats / _stats_py / _xp_mean
function
scipy.stats._stats_py:_xp_mean
source: /scipy/stats/_stats_py.py :10573
Signature
def _xp_mean ( x , / , axis = None , weights = None , keepdims = False , nan_policy = propagate , dtype = None , xp = None ) Summary
Compute the arithmetic mean along the specified axis.
Parameters
x: real arrayArray containing real numbers whose mean is desired.
axis: int or tuple of ints, default: NoneIf an int or tuple of ints, the axis or axes of the input along which to compute the statistic. The statistic of each axis-slice (e.g. row) of the input will appear in a corresponding element of the output. If
None, the input will be raveled before computing the statistic.weights: real array, optionalIf specified, an array of weights associated with the values in
x; otherwise1. Ifweightsandxdo not have the same shape, the arrays will be broadcasted before performing the calculation. See Notes for details.keepdims: boolean, optionalIf this is set to
True, the axes which are reduced are left in the result as dimensions with length one. With this option, the result will broadcast correctly against the input array.nan_policy: {'propagate', 'omit', 'raise'}, default: 'propagate'Defines how to handle input NaNs.
propagate: if a NaN is present in the axis slice (e.g. row) along which the statistic is computed, the corresponding entry of the output will be NaN.omit: NaNs will be omitted when performing the calculation. If insufficient data remains in the axis slice along which the statistic is computed, the corresponding entry of the output will be NaN.raise: if a NaN is present, aValueErrorwill be raised.
dtype: dtype, optionalType to use in computing the mean. For integer inputs, the default is the default float type of the array library; for floating point inputs, the dtype is that of the input.
Returns
out: arrayThe mean of each slice
Notes
Let represent element of data x and let represent the corresponding element of weights after broadcasting. Then the (weighted) mean is given by:
where is the number of elements along a slice. Note that this simplifies to the familiar when the weights are all 1 (default).
The behavior of this function with respect to weights is somewhat different from that of np.average. For instance, np.average raises an error when axis is not specified and the shapes of x and the weights array are not the same; xp_mean simply broadcasts the two. Also, np.average raises an error when weights sum to zero along a slice; xp_mean computes the appropriate result. The intent is for this function's interface to be consistent with the rest of scipy.stats.
Note that according to the formula, including NaNs with zero weights is not the same as omitting NaNs with nan_policy='omit'; in the former case, the NaNs will continue to propagate through the calculation whereas in the latter case, the NaNs are excluded entirely.
Aliases
-
scipy.stats._stats_py._xp_mean