bundles / scipy 1.17.1 / scipy / stats / _stats_py / pmean
function
scipy.stats._stats_py:pmean
source: /scipy/stats/_stats_py.py :334
Signature
def pmean ( a , p , * , axis = 0 , dtype = None , weights = None , nan_policy = propagate , keepdims = False ) Summary
Calculate the weighted power mean along the specified axis.
Extended Summary
The weighted power mean of the array associated to weights is:
and, with equal weights, it gives:
When p=0, it returns the geometric mean.
This mean is also called generalized mean or Hölder mean, and must not be confused with the Kolmogorov generalized mean, also called quasi-arithmetic mean or generalized f-mean [3].
Parameters
a: array_likeInput array, masked array or object that can be converted to an array.
p: int or floatExponent. Must be finite.
axis: int or None, default: 0If an int, the axis 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.dtype: dtype, optionalType of the returned array and of the accumulator in which the elements are summed. If
dtypeis not specified, it defaults to the dtype ofa, unlessahas an integerdtypewith a precision less than that of the default platform integer. In that case, the default platform integer is used.weights: array_like, optionalThe weights array can either be 1-D (in which case its length must be the size of
aalong the givenaxis) or of the same shape asa. Default is None, which gives each value a weight of 1.0.nan_policy: {'propagate', 'omit', 'raise'}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.
keepdims: bool, default: FalseIf this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
Returns
pmean: ndarray, see `dtype` parameter above.Output array containing the power mean values.
Notes
The power mean is computed over a single dimension of the input array, axis=0 by default, or all values in the array if axis=None. float64 intermediate and return values are used for integer inputs.
The power mean is only defined if all observations are non-negative; otherwise, the result is NaN.
Beginning in SciPy 1.9, np.matrix inputs (not recommended for new code) are converted to np.ndarray before the calculation is performed. In this case, the output will be a scalar or np.ndarray of appropriate shape rather than a 2D np.matrix. Similarly, while masked elements of masked arrays are ignored, the output will be a scalar or np.ndarray rather than a masked array with mask=False.
Array API Standard Support
pmean 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 ⚠️ no JIT ⚠️ no JIT Dask ⚠️ computes graph n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
from scipy.stats import pmean, hmean, gmean
✓pmean([1, 4], 1.3) pmean([1, 2, 3, 4, 5, 6, 7], 1.3) pmean([1, 4, 7], -2, weights=[3, 1, 3])✗
pmean([1, 4, 7], -1, weights=[3, 1, 3]) hmean([1, 4, 7], weights=[3, 1, 3])✗
pmean([1, 4, 7], 0, weights=[3, 1, 3]) gmean([1, 4, 7], weights=[3, 1, 3])✗
See also
Aliases
-
scipy.stats.pmean