fn
nanmedian
→Tensornanmedian(x: Tensor, dim: int | None = ..., keepdim: bool = ...)Median of a tensor, ignoring NaN entries.
Computes the median over the requested axis (or the entire tensor
when dim is None), treating NaN entries as missing data
rather than propagating them. This is the NaN-safe counterpart of
lucid.median.
Parameters
xTensorInput tensor of any floating dtype.
dimint | NoneReduction axis.
None (default) flattens x first.keepdimboolIf
True, retain the reduced axis as a singleton in the
output shape. Ignored when dim is None. Defaults to
False.Returns
TensorMedian value(s) over the non-NaN entries. Scalar when
dim is None; otherwise the input shape with the reduction
axis removed (or kept as size 1 if keepdim=True).
Notes
Algorithmic outline:
- Replace NaN entries with so they sort to the high end and never become the chosen median.
- Sort along the reduction axis.
- Pick element at index — the lower-median convention for even counts, matching the reference framework's default behaviour.
If all entries along the reduction are NaN the result is NaN (no valid sample to take).
Examples
>>> import lucid
>>> import math
>>> x = lucid.tensor([1.0, math.nan, 3.0, 5.0])
>>> lucid.nanmedian(x)
Tensor(3.)