torch.nanmean
function nanmean<S extends Shape, D extends DType, Dev extends DeviceType>(input: Tensor<S, D, Dev>, options?: NanReductionOptions): Tensor<DynamicShape, D, Dev>function nanmean<S extends Shape, D extends DType, Dev extends DeviceType>(input: Tensor<S, D, Dev>, dim: number | number[], keepdim: boolean): Tensor<DynamicShape, D, Dev>Computes the mean of all non-NaN elements.
Averages elements while automatically excluding any NaN values. Useful for:
- Missing data handling: computing averages with NaN placeholders for missing values
- Robust statistics: mean that ignores invalid/corrupted measurements
- Data quality assessment: examining valid data subset statistics
- Sensor fusion: averaging sensor readings when some sensors malfunction
- Incomplete datasets: averaging despite some missing features
- Outlier handling: robust averaging after marking outliers as NaN
Unlike regular mean() which returns NaN if any element is NaN, nanmean counts only non-NaN values in its calculation. Automatically adjusts denominator.
- NaN excluded from count: Only non-NaN elements are counted in denominator
- Performance: Slightly slower than regular mean due to NaN detection
- Broadcasting: keepdim=true preserves dimensions for auto-broadcasting
- All-NaN case: If all elements are NaN, result is NaN
- Numeric stability: No special numerical stability measures (unlike nansum/nanmean in some libraries)
- Different semantics: nanmean(1, NaN, 3) = 2 but mean(1, NaN, 3) = NaN
- Silent data loss: NaN values are ignored without warning
- Denominator changes: Different windows may have different valid counts
Parameters
inputTensor<S, D, Dev>- The input tensor (may contain NaN values)
optionsNanReductionOptionsoptional
Returns
Tensor<DynamicShape, D, Dev>– Tensor containing the mean of non-NaN valuesExamples
// Simple NaN handling in averaging
const x = torch.tensor([1, NaN, 3, 5]);
torch.nanmean(x); // 3 (mean of 1, 3, 5; ignores NaN)
torch.mean(x); // NaN (regular mean propagates NaN)
// Sensor data with intermittent failures
const sensors = torch.tensor([[10, NaN, 12], [NaN, 15, 16]]);
const averages = torch.nanmean(sensors, 1); // [11, 15.5]
// Feature extraction from incomplete dataset
const batch = torch.randn(32, 256);
batch.masked_fill_(batch.isnan(), NaN); // Some values are missing
const feature_means = torch.nanmean(batch, 0); // [256] - mean per feature
// Temporal averaging with missing timesteps
const timeseries = torch.randn(100, 30); // 100 samples, 30 timesteps
timeseries.masked_fill_(timeseries.lt(-5), NaN); // Mark invalid as NaN
const smoothed = torch.nanmean(timeseries, 1); // [100] - average each series
// Quality score computation ignoring failed measurements
const measurements = torch.tensor([0.95, NaN, 0.98, 0.92, NaN, 0.96]);
const quality = torch.nanmean(measurements); // 0.953 (mean of valid measurements)See Also
- PyTorch torch.nanmean()
- nansum - Sum while ignoring NaN
- mean - Regular averaging (propagates NaN)
- std - Standard deviation (may also want nanstd variant)
- where - Conditional value selection