paddle. quantile ( x, q, axis=None, keepdim=False ) [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.

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

  • q (int|float|list) – The q for calculate quantile, which should be in range [0, 1]. If q is a list, each q will be calculated and the first dimension of output is same to the number of q .

  • 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.

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


Tensor, results of quantile along axis of x. In order to obtain higher precision, data type of results will be float64.


>>> 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=float64, place=Place(cpu), stop_gradient=True,

>>> y2 = paddle.quantile(y, q=0.5, axis=1)
>>> print(y2)
Tensor(shape=[4], dtype=float64, 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=float64, 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=float64, place=Place(cpu), stop_gradient=True,
[[nan       ],