{ } Raw JSON

bundles / scipy 1.17.1 / scipy / stats / _stats_py / rankdata

function

scipy.stats._stats_py:rankdata

source: /scipy/stats/_stats_py.py :9903

Signature

def   rankdata ( a method = average * axis = None nan_policy = propagate )

Summary

Assign ranks to data, dealing with ties appropriately.

Extended Summary

By default (axis=None), the data array is first flattened, and a flat array of ranks is returned. Separately reshape the rank array to the shape of the data array if desired (see Examples).

Ranks begin at 1. The method argument controls how ranks are assigned to equal values. See [1] for further discussion of ranking methods.

Parameters

a : array_like

The array of values to be ranked.

method : {'average', 'min', 'max', 'dense', 'ordinal'}, optional

The method used to assign ranks to tied elements. The following methods are available (default is 'average'):

  • 'average': The average of the ranks that would have been assigned to all the tied values is assigned to each value.

  • 'min': The minimum of the ranks that would have been assigned to all the tied values is assigned to each value. (This is also referred to as "competition" ranking.)

  • 'max': The maximum of the ranks that would have been assigned to all the tied values is assigned to each value.

  • 'dense': Like 'min', but the rank of the next highest element is assigned the rank immediately after those assigned to the tied elements.

  • 'ordinal': All values are given a distinct rank, corresponding to the order that the values occur in a.

axis : {None, int}, optional

Axis along which to perform the ranking. If None, the data array is first flattened.

nan_policy : {'propagate', 'omit', 'raise'}, optional

Defines how to handle when input contains nan. The following options are available (default is 'propagate'):

  • 'propagate': propagates nans through the rank calculation

  • 'omit': performs the calculations ignoring nan values

  • 'raise': raises an error

Returns

ranks : ndarray

An array of size equal to the size of a, containing rank scores.

Notes

Array API Standard Support

rankdata 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                  ⛔                     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

Examples

import numpy as np
from scipy.stats import rankdata
rankdata([0, 2, 3, 2])
rankdata([0, 2, 3, 2], method='min')
rankdata([0, 2, 3, 2], method='max')
rankdata([0, 2, 3, 2], method='dense')
rankdata([0, 2, 3, 2], method='ordinal')
rankdata([[0, 2], [3, 2]]).reshape(2,2)
rankdata([[0, 2, 2], [3, 2, 5]], axis=1)
rankdata([0, 2, 3, np.nan, -2, np.nan], nan_policy="propagate")
rankdata([0, 2, 3, np.nan, -2, np.nan], nan_policy="omit")

Aliases

  • scipy.stats.rankdata

Referenced by