PyTorch torch.nanmedian function
PyTorch torch reference manual
torch.nanmedian is a function in PyTorch used to return the median of the non-NaN values of a tensor. Unlike median, it ignores all NaN values.
Function definition
torch.nanmedian(input, dim, keepdim=False)
Usage examples
Example
import
torch
x
=
torch.
tensor
(
[
1.0
,
float
(
'nan'
)
,
3.0
,
2.0
,
5.0
]
)
Return the median of non-NaN values
(
"Non-NaN median:"
,
torch.
nanmedian
(
x
)
)
Non-NaN median along dim=0
y
=
torch.
tensor
(
[
[
1.0
,
float
(
'nan'
)
,
2.0
]
,
[
4.0
,
1.0
,
3.0
]
]
)
(
"dim=0 non-NaN median:"
,
torch.
nanmedian
(
y
,
dim
=
0
)
)
The output is:
非NaN中位数: tensor(2.)
dim=0 非NaN中位数: torch.return_types.nanmedian(values=tensor([1., 1., 2.]), indices=tensor([0, 1, 0]))