nip.utils.maths.aggregate_mean_grouped_by_class

nip.utils.maths.aggregate_mean_grouped_by_class#

nip.utils.maths.aggregate_mean_grouped_by_class(values: Float[Tensor, 'batch'] | Float[ndarray, 'batch'], classes: Int[Tensor, 'batch'] | Int[ndarray, 'batch'], num_classes: int | None = None) Float[Tensor, 'class'] | Float[ndarray, 'class'][source]#

Compute the mean of values grouped by class.

values is a 1D tensor of values to aggregate, and classes is a 1D tensor of class labels for each value. The function computes the mean of the values for each class.

It returns a 1D tensor of mean values for each class. If any class has no values associated with it, the mean for that class is set to NaN.

Parameters:
  • values (Float[Tensor | numpy.ndarray, "batch"]) – The values to aggregate.

  • classes (Int[Tensor | numpy.ndarray, "batch"]) – The class labels for each value.

  • num_classes (int, optional) – The number of classes. If not provided, it is inferred from the class labels.

Returns:

mean_values (Float[Tensor | numpy.ndarray, “class”]) – The mean of the values for each class. If either of the arguments is a numpy array, this will be one too.