scipy.stats.pmean#
- scipy.stats.pmean(a, p, *, axis=0, dtype=None, weights=None, nan_policy='propagate', keepdims=False)[source]#
Calculate the weighted power mean along the specified axis.
The weighted power mean of the array \(a_i\) associated to weights \(w_i\) is:
\[\left( \frac{ \sum_{i=1}^n w_i a_i^p }{ \sum_{i=1}^n w_i } \right)^{ 1 / p } \, ,\]and, with equal weights, it gives:
\[\left( \frac{ 1 }{ n } \sum_{i=1}^n a_i^p \right)^{ 1 / p } \, .\]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
- aarray_like
Input array, masked array or object that can be converted to an array.
- pint or float
Exponent.
- axisint or None, default: 0
If 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.- dtypedtype, optional
Type of the returned array and of the accumulator in which the elements are summed. If dtype is not specified, it defaults to the dtype of a, unless a has an integer dtype with a precision less than that of the default platform integer. In that case, the default platform integer is used.
- weightsarray_like, optional
The weights array can either be 1-D (in which case its length must be the size of a along the given axis) or of the same shape as a. 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, aValueError
will be raised.
- keepdimsbool, default: False
If 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
- pmeanndarray, see dtype parameter above.
Output array containing the power mean values.
See also
numpy.average
Weighted average
gmean
Geometric mean
hmean
Harmonic mean
Notes
The power mean is computed over a single dimension of the input array,
axis=0
by default, or all values in the array ifaxis=None
. float64 intermediate and return values are used for integer inputs.New in version 1.9.
Beginning in SciPy 1.9,
np.matrix
inputs (not recommended for new code) are converted tonp.ndarray
before the calculation is performed. In this case, the output will be a scalar ornp.ndarray
of appropriate shape rather than a 2Dnp.matrix
. Similarly, while masked elements of masked arrays are ignored, the output will be a scalar ornp.ndarray
rather than a masked array withmask=False
.References
- 1
“Generalized Mean”, Wikipedia, https://en.wikipedia.org/wiki/Generalized_mean
- 2
Norris, N., “Convexity properties of generalized mean value functions”, The Annals of Mathematical Statistics, vol. 8, pp. 118-120, 1937
- 3
Bullen, P.S., Handbook of Means and Their Inequalities, 2003
Examples
>>> from scipy.stats import pmean, hmean, gmean >>> pmean([1, 4], 1.3) 2.639372938300652 >>> pmean([1, 2, 3, 4, 5, 6, 7], 1.3) 4.157111214492084 >>> pmean([1, 4, 7], -2, weights=[3, 1, 3]) 1.4969684896631954
For p=-1, power mean is equal to harmonic mean:
>>> pmean([1, 4, 7], -1, weights=[3, 1, 3]) 1.9029126213592233 >>> hmean([1, 4, 7], weights=[3, 1, 3]) 1.9029126213592233
For p=0, power mean is defined as the geometric mean:
>>> pmean([1, 4, 7], 0, weights=[3, 1, 3]) 2.80668351922014 >>> gmean([1, 4, 7], weights=[3, 1, 3]) 2.80668351922014