bundles / scipy 1.17.1 / scipy / stats / _stats_py / iqr
function
scipy.stats._stats_py:iqr
source: /scipy/stats/_stats_py.py :3064
Signature
def iqr ( x , axis = None , rng = (25, 75) , scale = 1.0 , nan_policy = propagate , interpolation = linear , keepdims = False ) Summary
Compute the interquartile range of the data along the specified axis.
Extended Summary
The interquartile range (IQR) is the difference between the 75th and 25th percentile of the data. It is a measure of the dispersion similar to standard deviation or variance, but is much more robust against outliers [2].
The rng parameter allows this function to compute other percentile ranges than the actual IQR. For example, setting rng=(0, 100) is equivalent to numpy.ptp.
The IQR of an empty array is np.nan.
Parameters
x: array_likeInput array or object that can be converted to an array.
axis: int or None, default: NoneIf 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.rng: Two-element sequence containing floats in range of [0,100] optionalPercentiles over which to compute the range. Each must be between 0 and 100, inclusive. The default is the true IQR:
(25, 75). The order of the elements is not important.scale: scalar or str or array_like of reals, optionalThe numerical value of scale will be divided out of the final result. The following string value is also recognized:
'normal'Scale by .
The default is 1.0. Array-like
scaleof real dtype is also allowed, as long as it broadcasts correctly to the output such thatout / scaleis a valid operation. The output dimensions depend on the input array,x, theaxisargument, and thekeepdimsflag.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.
interpolation: str, optionalSpecifies the interpolation method to use when the percentile boundaries lie between two data points
iandj. The following options are available (default is 'linear'):'linear':
i + (j - i)*fraction, wherefractionis the fractional part of the index surrounded byiandj.'lower':
i.'higher':
j.'nearest':
iorjwhichever is nearest.'midpoint':
(i + j)/2.
For NumPy >= 1.22.0, the additional options provided by the
methodkeyword of numpy.percentile are also valid.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
iqr: scalar or ndarrayIf
axis=None, a scalar is returned. If the input contains integers or floats of smaller precision thannp.float64, then the output data-type isnp.float64. Otherwise, the output data-type is the same as that of the input.
Notes
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
iqr 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
import numpy as np from scipy.stats import iqr x = np.array([[10, 7, 4], [3, 2, 1]]) x✓
iqr(x) iqr(x, axis=0) iqr(x, axis=1) iqr(x, axis=1, keepdims=True)✗
See also
- numpy.std
- numpy.var
Aliases
-
scipy.stats.iqr