-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【Hackathon No.25】为 Paddle 新增 nanquantile 数学计算API #41343
Changes from 8 commits
126720b
dc89e91
908548f
34f7742
b63469e
34f4df9
3306abc
56e3be8
1b0c13d
350ccad
09a5c25
0e2ef42
78d332f
34ab31f
31e1734
4669518
4250918
86d65bb
b3bc441
26c993f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import print_function | ||
|
||
import unittest | ||
import numpy as np | ||
import paddle | ||
|
||
|
||
class TestNaNQuantile(unittest.TestCase): | ||
""" | ||
This class is used for numerical precision testing. If there is | ||
a corresponding numpy API, the precision comparison can be performed directly. | ||
Otherwise, it needs to be verified by numpy implementated function. | ||
""" | ||
|
||
def setUp(self): | ||
np.random.seed(2022) | ||
self.input_data = np.random.rand(6, 7, 8, 9, 10) | ||
|
||
# Test correctness when q and axis are set. | ||
def test_nanquantile_single_q(self): | ||
x = paddle.to_tensor(self.input_data) | ||
paddle_res = paddle.nanquantile(x, q=0.5, axis=2) | ||
np_res = np.nanquantile(self.input_data, q=0.5, axis=2) | ||
self.assertTrue(np.allclose(paddle_res.numpy(), np_res)) | ||
|
||
# Test correctness for default axis. | ||
def test_nanquantile_with_no_axis(self): | ||
x = paddle.to_tensor(self.input_data) | ||
paddle_res = paddle.nanquantile(x, q=0.35) | ||
np_res = np.nanquantile(self.input_data, q=0.35) | ||
self.assertTrue(np.allclose(paddle_res.numpy(), np_res)) | ||
|
||
# Test correctness for multiple axis. | ||
def test_nanquantile_with_multi_axis(self): | ||
x = paddle.to_tensor(self.input_data) | ||
paddle_res = paddle.nanquantile(x, q=0.75, axis=[0, 2, 3]) | ||
np_res = np.nanquantile(self.input_data, q=0.75, axis=[0, 2, 3]) | ||
self.assertTrue(np.allclose(paddle_res.numpy(), np_res)) | ||
|
||
# Test correctness when keepdim is set. | ||
def test_nanquantile_with_keepdim(self): | ||
x = paddle.to_tensor(self.input_data) | ||
paddle_res = paddle.nanquantile(x, q=0.35, axis=4, keepdim=True) | ||
np_res = np.nanquantile(self.input_data, q=0.35, axis=4, keepdims=True) | ||
self.assertTrue(np.allclose(paddle_res.numpy(), np_res)) | ||
|
||
# Test correctness when all parameters are set. | ||
def test_nanquantile_with_keepdim_and_multiple_axis(self): | ||
x = paddle.to_tensor(self.input_data) | ||
paddle_res = paddle.nanquantile(x, q=0.1, axis=[1, 4], keepdim=True) | ||
np_res = np.nanquantile(self.input_data, q=0.1, axis=[1, 4], keepdims=True) | ||
self.assertTrue(np.allclose(paddle_res.numpy(), np_res)) | ||
|
||
# Test correctness when q = 0. | ||
def test_nanquantile_with_boundary_q(self): | ||
x = paddle.to_tensor(self.input_data) | ||
paddle_res = paddle.nanquantile(x, q=0, axis=3) | ||
np_res = np.nanquantile(self.input_data, q=0, axis=3) | ||
self.assertTrue(np.allclose(paddle_res.numpy(), np_res)) | ||
|
||
# Test correctness when input includes NaN. | ||
def test_nanquantile_include_NaN(self): | ||
input_data = np.random.randn(2, 3, 4) | ||
input_data[0, 1, 1] = np.nan | ||
x = paddle.to_tensor(input_data) | ||
paddle_res = paddle.nanquantile(x, q=0.35, axis=0) | ||
np_res = np.nanquantile(x, q=0.35, axis=0) | ||
self.assertTrue(np.allclose(paddle_res.numpy(), np_res, equal_nan=True)) | ||
|
||
|
||
class TestNaNQuantileMuitlpleQ(unittest.TestCase): | ||
""" | ||
This class is used to test multiple input of q. | ||
""" | ||
|
||
def setUp(self): | ||
np.random.seed(2022) | ||
self.input_data = np.random.rand(10, 3, 4, 5, 4) | ||
|
||
def test_nanquantile(self): | ||
x = paddle.to_tensor(self.input_data) | ||
paddle_res = paddle.nanquantile(x, q=[0.3, 0.44], axis=-2) | ||
np_res = np.nanquantile(self.input_data, q=[0.3, 0.44], axis=-2) | ||
self.assertTrue(np.allclose(paddle_res.numpy(), np_res)) | ||
|
||
def test_nanquantile_multiple_axis(self): | ||
x = paddle.to_tensor(self.input_data) | ||
paddle_res = paddle.nanquantile(x, q=[0.2, 0.67], axis=[1, -1]) | ||
np_res = np.nanquantile(self.input_data, q=[0.2, 0.67], axis=[1, -1]) | ||
self.assertTrue(np.allclose(paddle_res.numpy(), np_res)) | ||
|
||
def test_nanquantile_multiple_axis_keepdim(self): | ||
x = paddle.to_tensor(self.input_data) | ||
paddle_res = paddle.nanquantile( | ||
x, q=[0.1, 0.2, 0.3], axis=[1, 2], keepdim=True) | ||
np_res = np.nanquantile( | ||
self.input_data, q=[0.1, 0.2, 0.3], axis=[1, 2], keepdims=True) | ||
self.assertTrue(np.allclose(paddle_res.numpy(), np_res)) | ||
|
||
|
||
class TestNaNQuantileError(unittest.TestCase): | ||
""" | ||
This class is used to test that exceptions are thrown correctly. | ||
Validity of all parameter values and types should be considered. | ||
""" | ||
|
||
def setUp(self): | ||
self.x = paddle.randn((2, 3, 4)) | ||
|
||
def test_errors(self): | ||
# Test error when q > 1 | ||
def test_q_range_error_1(): | ||
paddle_res = paddle.nanquantile(self.x, q=1.5) | ||
|
||
self.assertRaises(ValueError, test_q_range_error_1) | ||
|
||
# Test error when q < 0 | ||
def test_q_range_error_2(): | ||
paddle_res = paddle.nanquantile(self.x, q=[0.2, -0.3]) | ||
|
||
self.assertRaises(ValueError, test_q_range_error_2) | ||
|
||
# Test error with no valid q | ||
def test_q_range_error_3(): | ||
paddle_res = paddle.nanquantile(self.x, q=[]) | ||
|
||
self.assertRaises(ValueError, test_q_range_error_3) | ||
|
||
# Test error when x is not Tensor | ||
def test_x_type_error(): | ||
x = [1, 3, 4] | ||
paddle_res = paddle.nanquantile(x, q=0.9) | ||
|
||
self.assertRaises(TypeError, test_x_type_error) | ||
|
||
# Test error when scalar axis is not int | ||
def test_axis_type_error_1(): | ||
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=0.4) | ||
|
||
self.assertRaises(ValueError, test_axis_type_error_1) | ||
|
||
# Test error when axis in List is not int | ||
def test_axis_type_error_2(): | ||
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=[1, 0.4]) | ||
|
||
self.assertRaises(ValueError, test_axis_type_error_2) | ||
|
||
# Test error when axis not in [-D, D) | ||
def test_axis_value_error_1(): | ||
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=10) | ||
|
||
self.assertRaises(ValueError, test_axis_value_error_1) | ||
|
||
# Test error when axis not in [-D, D) | ||
def test_axis_value_error_2(): | ||
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=[1, -10]) | ||
|
||
self.assertRaises(ValueError, test_axis_value_error_2) | ||
|
||
# Test error with no valid axis | ||
def test_axis_value_error_3(): | ||
paddle_res = paddle.nanquantile(self.x, q=0.4, axis=[]) | ||
|
||
self.assertRaises(ValueError, test_axis_value_error_3) | ||
|
||
|
||
class TestNaNQuantileRuntime(unittest.TestCase): | ||
""" | ||
This class is used to test the API could run correctly with | ||
different devices, different data types, and dygraph/static mode. | ||
""" | ||
|
||
def setUp(self): | ||
np.random.seed(2022) | ||
self.input_data = np.random.rand(6, 7, 8, 9, 10) | ||
self.dtypes = ['float32', 'float64'] | ||
self.devices = ['cpu'] | ||
if paddle.device.is_compiled_with_cuda(): | ||
self.devices.append('gpu') | ||
|
||
def test_dygraph(self): | ||
paddle.disable_static() | ||
for device in self.devices: | ||
# Check different devices | ||
paddle.set_device(device) | ||
for dtype in self.dtypes: | ||
# Check different dtypes | ||
np_input_data = self.input_data.astype(dtype) | ||
x = paddle.to_tensor(np_input_data, dtype=dtype) | ||
paddle_res = paddle.nanquantile(x, q=0.5, axis=2) | ||
np_res = np.nanquantile(np_input_data, q=0.5, axis=2) | ||
self.assertTrue(np.allclose(paddle_res.numpy(), np_res)) | ||
|
||
def test_static(self): | ||
paddle.enable_static() | ||
for device in self.devices: | ||
x = paddle.static.data( | ||
name="x", shape=self.input_data.shape, dtype=paddle.float32) | ||
x_fp64 = paddle.static.data( | ||
name="x_fp64", | ||
shape=self.input_data.shape, | ||
dtype=paddle.float64) | ||
|
||
results = paddle.nanquantile(x, q=0.5, axis=2) | ||
np_input_data = self.input_data.astype('float32') | ||
results_fp64 = paddle.nanquantile(x_fp64, q=0.5, axis=2) | ||
np_input_data_fp64 = self.input_data.astype('float64') | ||
|
||
exe = paddle.static.Executor(device) | ||
paddle_res, paddle_res_fp64 = exe.run( | ||
paddle.static.default_main_program(), | ||
feed={"x": np_input_data, | ||
"x_fp64": np_input_data_fp64}, | ||
fetch_list=[results, results_fp64]) | ||
np_res = np.nanquantile(np_input_data, q=0.5, axis=2) | ||
np_res_fp64 = np.nanquantile(np_input_data_fp64, q=0.5, axis=2) | ||
self.assertTrue( | ||
np.allclose(paddle_res, np_res) and np.allclose(paddle_res_fp64, | ||
np_res_fp64)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -260,6 +260,7 @@ | |
from .stat import numel # noqa: F401 | ||
from .stat import median # noqa: F401 | ||
from .stat import quantile # noqa: F401 | ||
from .stat import nanquantile # noqa: F401 | ||
|
||
from .to_string import set_printoptions # noqa: F401 | ||
|
||
|
@@ -442,6 +443,7 @@ | |
'numel', | ||
'median', | ||
'quantile', | ||
'nanquantile' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 又少了一个逗号了。。
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 抱歉,已修改。当时看的时候还以为是__all__里的。。 |
||
'is_complex', | ||
'is_integer', | ||
'rank', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -342,13 +342,14 @@ def median(x, axis=None, keepdim=False, name=None): | |
return out_tensor | ||
|
||
|
||
def quantile(x, q, axis=None, keepdim=False): | ||
def _compute_quantile(x, q, axis=None, keepdim=False, ignore_nan=False): | ||
""" | ||
Compute the quantile of the input along the specified axis. | ||
Args: | ||
Args: | ||
x (Tensor): The input Tensor, it's data type can be float32, float64. | ||
q (int|float|list): The q for calculate quantile, which should be in range [0, 1]. If q is a list, | ||
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`` . | ||
|
@@ -360,37 +361,28 @@ def quantile(x, q, axis=None, keepdim=False): | |
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 :ref:`api_guide_Name`. | ||
ignore_nan: (bool, optional): Whether to ignore NaN of input Tensor. | ||
If ``ignore_nan`` is True, it will calculate nanquantile. | ||
Otherwise it will calculate quantile. Default is False. | ||
Returns: | ||
Tensor, results of quantile along ``axis`` of ``x``. If data type of ``x`` is float64, data type of results will be float64, otherwise data type will be float32. | ||
Examples: | ||
.. code-block:: python | ||
import paddle | ||
x = paddle.randn((2,3)) | ||
#[[-1.28740597, 0.49533170, -1.00698614], | ||
# [-1.11656201, -1.01010525, -2.23457789]]) | ||
y1 = paddle.quantile(x, q=0.5, axis=[0, 1]) | ||
# y1 = -1.06333363 | ||
y2 = paddle.quantile(x, q=0.5, axis=1) | ||
# y2 = [-1.00698614, -1.11656201] | ||
y3 = paddle.quantile(x, q=[0.3, 0.5], axis=1) | ||
# y3 =[[-1.11915410, -1.56376839], | ||
# [-1.00698614, -1.11656201]] | ||
y4 = paddle.quantile(x, q=0.8, axis=1, keepdim=True) | ||
# y4 = [[-0.10559537], | ||
# [-1.05268800]]) | ||
Tensor, results of quantile along ``axis`` of ``x``. | ||
In order to obtain higher precision, data type of results will be float64. | ||
""" | ||
# Validate x | ||
if not isinstance(x, Variable): | ||
raise TypeError("input x should be a Tensor.") | ||
|
||
# Validate q | ||
if isinstance(q, (int, float)): | ||
q = [q] | ||
elif isinstance(q, (list, tuple)): | ||
if len(q) <= 0: | ||
raise ValueError("q should not be empty") | ||
else: | ||
raise TypeError("Type of q should be int, float, list or tuple.") | ||
|
||
# Validate axis | ||
dims = len(x.shape) | ||
out_shape = list(x.shape) | ||
if axis is None: | ||
|
@@ -399,7 +391,7 @@ def quantile(x, q, axis=None, keepdim=False): | |
out_shape = [1] * dims | ||
else: | ||
if isinstance(axis, list): | ||
if (len(axis) <= 0): | ||
if len(axis) <= 0: | ||
raise ValueError("axis should not be empty") | ||
axis_src, axis_dst = [], [] | ||
for axis_single in axis: | ||
|
@@ -424,54 +416,164 @@ def quantile(x, q, axis=None, keepdim=False): | |
if axis < 0: | ||
axis += dims | ||
out_shape[axis] = 1 | ||
|
||
mask = x.isnan() | ||
valid_counts = mask.logical_not().sum( | ||
axis=axis, keepdim=True, dtype='float64') | ||
|
||
indices = [] | ||
if isinstance(q, (int, float)): | ||
if q < 0 or q > 1: | ||
|
||
for q_num in q: | ||
if q_num < 0 or q_num > 1: | ||
raise ValueError("q should be in range [0, 1]") | ||
indices.append(q * (x.shape[axis] - 1)) | ||
elif isinstance(q, (list, tuple)): | ||
if len(q) <= 0: | ||
raise ValueError("q should not be empty") | ||
for q_num in q: | ||
if q_num < 0 or q_num > 1: | ||
raise ValueError("q should be in range [0, 1]") | ||
indices.append(q_num * (x.shape[axis] - 1)) | ||
else: | ||
raise TypeError("Type of q should be int, float, list or tuple.") | ||
if paddle.in_dynamic_mode(): | ||
q_num = paddle.to_tensor(q_num, dtype='float64') | ||
if ignore_nan: | ||
indices.append(q_num * (valid_counts - 1)) | ||
else: | ||
# TODO(Asthestarsfalll): Use paddle.index_fill instead of where | ||
index = q_num * (valid_counts - 1) | ||
last_index = x.shape[axis] - 1 | ||
nums = paddle.full_like(index, fill_value=last_index) | ||
index = paddle.where(mask.any(axis=axis, keepdim=True), nums, index) | ||
indices.append(index) | ||
|
||
sorted_tensor = paddle.sort(x, axis) | ||
indices_tensor = paddle.assign(indices).astype(paddle.float32) | ||
indices_below = paddle.floor(indices_tensor).astype(paddle.int32) | ||
indices_upper = paddle.ceil(indices_tensor).astype(paddle.int32) | ||
outputs = [] | ||
|
||
def expand_dim(indices, sorted_tensor_shape, axis): | ||
assert axis < len(list(sorted_tensor_shape)) | ||
expanded_shape = [1] * len(list(sorted_tensor_shape)) | ||
expanded_shape = tuple(expanded_shape) | ||
indices = indices.reshape(expanded_shape) | ||
return indices | ||
outputs = [] | ||
|
||
# TODO(chenjianye): replace the for-loop to directly take elements. | ||
for i in range(len(indices)): | ||
if (indices_upper[i] != indices_below[i]): | ||
tensor_below = paddle.take_along_axis( | ||
sorted_tensor, | ||
expand_dim(indices_below[i], sorted_tensor.shape, axis), axis) | ||
tensor_upper = paddle.take_along_axis( | ||
sorted_tensor, | ||
expand_dim(indices_upper[i], sorted_tensor.shape, axis), axis) | ||
weights = (indices[i] - indices_below[i]).astype(x.dtype) | ||
out = paddle.lerp(tensor_below, tensor_upper, weights) | ||
else: | ||
out = paddle.take_along_axis( | ||
sorted_tensor, | ||
expand_dim(indices_below[i], sorted_tensor.shape, axis), axis) | ||
for index in indices: | ||
indices_below = paddle.floor(index).astype(paddle.int32) | ||
indices_upper = paddle.ceil(index).astype(paddle.int32) | ||
tensor_upper = paddle.take_along_axis( | ||
sorted_tensor, indices_upper, axis=axis) | ||
tensor_below = paddle.take_along_axis( | ||
sorted_tensor, indices_below, axis=axis) | ||
weights = (index - indices_below.astype('float64')) | ||
out = paddle.lerp( | ||
tensor_below.astype('float64'), | ||
tensor_upper.astype('float64'), weights) | ||
if not keepdim: | ||
out = paddle.squeeze(out, axis=axis) | ||
else: | ||
out = out.reshape(out_shape) | ||
outputs.append(out) | ||
if isinstance(q, (list, tuple)): | ||
return paddle.stack(outputs, 0) | ||
|
||
if len(q) > 1: | ||
outputs = paddle.stack(outputs, 0) | ||
else: | ||
return outputs[0] | ||
outputs = outputs[0] | ||
|
||
return outputs | ||
|
||
|
||
def quantile(x, q, axis=None, keepdim=False): | ||
""" | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Args: | ||
x (Tensor): The input Tensor, it's data type can be float32, float64. | ||
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 :math:`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 :ref:`api_guide_Name`. | ||
Returns: | ||
Tensor, results of quantile along ``axis`` of ``x``. | ||
In order to obtain higher precision, data type of results will be float64. | ||
Examples: | ||
.. code-block:: python | ||
import numpy as np | ||
import paddle | ||
x = np.random.randn(2, 3) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议示例不要用随机数,用一个固定构造的数组。2*3矩阵里的元素可以简单便于手动计算:
|
||
x[0][0] = float('nan') | ||
# [[ nan 0.41205737 0.36599339] | ||
# [ 0.84388305 -1.21257817 0.25973139]] | ||
x = paddle.to_tensor(x) | ||
y1 = paddle.quantile(x, q=0.5, axis=[0, 1]) | ||
# nan | ||
y2 = paddle.quantile(x, q=0.5, axis=1) | ||
# [ nan 0.25973139] | ||
y3 = paddle.quantile(x, q=[0.3, 0.5], axis=1) | ||
# [[ nan -0.32919244] | ||
# [ nan 0.25973139]] | ||
y4 = paddle.quantile(x, q=0.8, axis=1, keepdim=True) | ||
# [[ nan] | ||
# [0.61022238]] | ||
""" | ||
return _compute_quantile(x, q, axis=axis, keepdim=keepdim, ignore_nan=False) | ||
|
||
|
||
def nanquantile(x, q, axis=None, keepdim=False): | ||
""" | ||
Compute the quantile of the input as if NaN values in input did not exist. | ||
If all values in a reduced row are NaN then the quantiles for that reduction will be NaN. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
需要补充全NAN的示例 |
||
Args: | ||
x (Tensor): The input Tensor, it's data type can be float32, float64. | ||
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 :math:`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 :ref:`api_guide_Name`. | ||
Returns: | ||
Tensor, results of quantile along ``axis`` of ``x``. | ||
In order to obtain higher precision, data type of results will be float64. | ||
Examples: | ||
.. code-block:: python | ||
import numpy as np | ||
import paddle | ||
x = np.random.randn(2, 3) | ||
x[0][0] = float('nan') | ||
# [[ nan 1.26085129 -0.35944291] | ||
# [-0.62427785 1.73718584 1.06024497]] | ||
x = paddle.to_tensor(x) | ||
y1 = paddle.nanquantile(x, q=0.5, axis=[0, 1]) | ||
# 1.0602449747672356 | ||
y2 = paddle.nanquantile(x, q=0.5, axis=1) | ||
# [0.45070419 1.06024497] | ||
y3 = paddle.nanquantile(x, q=[0.3, 0.5], axis=1) | ||
# [[0.12664535 0.38643585] | ||
# [0.45070419 1.06024497]] | ||
y4 = paddle.nanquantile(x, q=0.8, axis=1, keepdim=True) | ||
# [[0.93679245] | ||
# [1.4664095 ]] | ||
""" | ||
return _compute_quantile(x, q, axis=axis, keepdim=keepdim, ignore_nan=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
单测和quantile几乎一摸一样,但nanquantile更要侧重对NAN的测试:
因为两份单测非常类似,如果可以的话,看如何更好地进行复用(非强制要求),如
Paddle/python/paddle/fluid/tests/unittests/test_nanmean_api.py
Lines 79 to 87 in 1d43e2d
Paddle/python/paddle/fluid/tests/unittests/test_max_min_amax_amin_op.py
Lines 105 to 108 in 1d43e2d