interquartile_range

pyhelpers.ops.interquartile_range(num_dat, axis=None, rng=(25, 75), scale=1.0, outlier_fences=False, k=1.5, ignore_nan=True, **kwargs)[source]

Calculates the interquartile range (IQR) of numerical data.

This function may serve as an alternative to scipy.stats.iqr when scipy is not available.

Parameters:
  • num_dat (numpy.ndarray | list | tuple) – Numerical data.

  • axis (int | None) – Axis along which to calculate IQR; defaults to None.

  • rng (tuple) – Percentile range for calculating IQR; defaults to (25, 75).

  • scale (int | float) – Scaling factor for the IQR; defaults to 1.0.

  • outlier_fences (bool) – Whether to calculate and return outlier fences; defaults to False.

  • k (int | float) – Multiplier for IQR for calculating outlier fences; defaults to 1.5

  • ignore_nan (bool) – Whether to ignore NaN values; defaults to True.

  • kwargs – [Optional] Additional parameters passed to numpy.percentile.

Returns:

Scaled interquartile range.

Return type:

float

Raises:

ValueError – If input has insufficient non-NaN values.

Examples:

>>> from pyhelpers.ops import interquartile_range
>>> num_dat = [1, 2, 'nan', 3, 4]
>>> iqr = interquartile_range(num_dat, ignore_nan=True)
>>> iqr
np.float64(1.5)
>>> num_dat = list(range(100))
>>> iqr = interquartile_range(num_dat)
>>> iqr
np.float64(49.5)
>>> iqr, lower_fence, upper_fence = interquartile_range(num_dat, outlier_fences=True)
>>> iqr, lower_fence, upper_fence
(np.float64(49.5), np.float64(-49.5), np.float64(148.5))