PyTorch torch.nanquantile 函数
torch.nanquantile 是 PyTorch 中用于返回张量的非NaN值的分位数的函数。与 quantile 不同的是,它会忽略所有的 NaN 值。
函数定义
torch.nanquantile(input, q, dim, keepdim=False)
使用示例
实例
import torch
x = torch.tensor([1.0, float('nan'), 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
# 返回非NaN值的 0.5 分位数
print("非NaN 0.5 分位数:", torch.nanquantile(x, 0.5))
# 返回多个非NaN分位数
print("非NaN 0.25, 0.75 分位数:", torch.nanquantile(x, torch.tensor([0.25, 0.75])))
# 沿 dim=0 非NaN分位数
y = torch.tensor([[1.0, float('nan'), 3.0], [4.0, 5.0, 6.0]])
print("dim=0 非NaN 0.5 分位数:", torch.nanquantile(y, 0.5, dim=0))
x = torch.tensor([1.0, float('nan'), 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0])
# 返回非NaN值的 0.5 分位数
print("非NaN 0.5 分位数:", torch.nanquantile(x, 0.5))
# 返回多个非NaN分位数
print("非NaN 0.25, 0.75 分位数:", torch.nanquantile(x, torch.tensor([0.25, 0.75])))
# 沿 dim=0 非NaN分位数
y = torch.tensor([[1.0, float('nan'), 3.0], [4.0, 5.0, 6.0]])
print("dim=0 非NaN 0.5 分位数:", torch.nanquantile(y, 0.5, dim=0))
输出结果为:
非NaN 0.5 分位数: tensor(5.5000) 非NaN 0.25, 0.75 分位数: tensor([2.7500, 7.7500]) dim=0 非NaN 0.5 分位数: tensor([2.5000, 5.0000, 4.5000])

Pytorch torch 参考手册