quantile

paddle. quantile ( x, q, axis=None, keepdim=False, interpolation='linear' ) [source]

Compute the quantile of the input along the specified axis. If any values in a reduced row are NaN, then the quantiles for that reduction will be NaN.

Parameters
  • x (Tensor) – The input Tensor, it’s data type can be float32, float64, int32, int64.

  • q (int|float|list|Tensor) – The q for calculate quantile, which should be in range [0, 1]. If q is a list or a 1-D Tensor, each element of q will be calculated and the first dimension of output is same to the number of q . If q is a 0-D Tensor, it will be treated as an integer or float.

  • axis (int|list, optional) – The axis along which to calculate quantile. axis should be int or list of int. axis should be in range [-D, D), where D is the dimensions of x . If axis is less than 0, it works the same way as \(axis + D\). If axis is a list, quantile is calculated over all elements of given axises. If axis is None, quantile is calculated over all elements of x. Default is None.

  • keepdim (bool, optional) – Whether to reserve the reduced dimension(s) in the output Tensor. If keepdim is True, the dimensions of the output Tensor is the same as x except in the reduced dimensions(it is of size 1 in this case). Otherwise, the shape of the output Tensor is squeezed in axis . Default is False.

  • interpolation (str, optional) – The interpolation method to use when the desired quantile falls between two data points. Must be one of linear, higher, lower, midpoint and nearest. Default is linear.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

Tensor, results of quantile along axis of x.

Examples

>>> import paddle

>>> y = paddle.arange(0, 8 ,dtype="float32").reshape([4, 2])
>>> print(y)
Tensor(shape=[4, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 1.],
 [2., 3.],
 [4., 5.],
 [6., 7.]])

>>> y1 = paddle.quantile(y, q=0.5, axis=[0, 1])
>>> print(y1)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
3.50000000)

>>> y2 = paddle.quantile(y, q=0.5, axis=1)
>>> print(y2)
Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.50000000, 2.50000000, 4.50000000, 6.50000000])

>>> y3 = paddle.quantile(y, q=[0.3, 0.5], axis=0)
>>> print(y3)
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1.80000000, 2.80000000],
 [3.        , 4.        ]])

>>> y[0,0] = float("nan")
>>> y4 = paddle.quantile(y, q=0.8, axis=1, keepdim=True)
>>> print(y4)
Tensor(shape=[4, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
[[nan       ],
 [2.80000000],
 [4.80000000],
 [6.80000000]])