Skip to main content
`Ctrl`+`K`
scipy.stats.

# trimboth

scipy.stats.trimboth(a, proportiontocut, axis=0)

  Slice off a proportion of items from both ends of an array.

  Slice off the passed proportion of items from both ends of the passed array (i.e., with proportiontocut = 0.1, slices leftmost 10% and rightmost 10% of scores). The trimmed values are the lowest and highest ones. Slice off less if proportion results in a non-integer slice index (i.e. conservatively slices off proportiontocut).

  Parameters:
    a array_like

      Data to trim.

    proportiontocut float

      Proportion (in range 0-1) of total data set to trim of each end.

    axis int or None, optional

      Axis along which to trim data. Default is 0. If None, compute over the whole array a.

  Returns:
    out ndarray

      Trimmed version of array a. The order of the trimmed content is undefined.

  See also

  `trim_mean`

  Notes

  Array API Standard Support

  `trimboth` 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 Support for the array API standard for more information.

  Examples

  Create an array of 10 values and trim 10% of those values from each end:

      >>> import numpy as np
      >>> from scipy import stats
      >>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
      >>> stats.trimboth(a, 0.1)
      array([1, 3, 2, 4, 5, 6, 7, 8])

  Note that the elements of the input array are trimmed by value, but the output array is not necessarily sorted.

  The proportion to trim is rounded down to the nearest integer. For instance, trimming 25% of the values from each end of an array of 10 values will return an array of 6 values:

      >>> b = np.arange(10)
      >>> stats.trimboth(b, 1/4).shape
      (6,)

  Multidimensional arrays can be trimmed along any axis or across the entire array:

      >>> c = [2, 4, 6, 8, 0, 1, 3, 5, 7, 9]
      >>> d = np.array([a, b, c])
      >>> stats.trimboth(d, 0.4, axis=0).shape
      (1, 10)
      >>> stats.trimboth(d, 0.4, axis=1).shape
      (3, 2)
      >>> stats.trimboth(d, 0.4, axis=None).shape
      (6,)