Skip to content

implement weighted aggregate operations #159

Open

Description

@gdementen

here is crude code I wrote for mean for my own uses:

The interesting part is that it allows weights to have more dimensions that the original array, which is sometimes useful.

    def mean(self, *args, **kwargs):
        keepaxes = kwargs.pop('keepaxes', False)
        skipna = kwargs.pop('skipna', None)
        weights = kwargs.pop('weights', None)
        if skipna is None:
            skipna = True
        if weights is None:
            func = np.nanmean if skipna else np.mean
            return self._aggregate(func, args, kwargs, keepaxes=keepaxes,
                                   commutative=True)
        else:
            func = np.nansum if skipna else np.sum
            if np.isscalar(weights):
                # assume weights is a label along an axis
                weights = self[weights]
            wsum = (self * weights)._aggregate(func, args, kwargs, keepaxes=keepaxes, commutative=True)
            expanded_weights = weights.expand(self.axes)
            num = expanded_weights._aggregate(func, args, kwargs, keepaxes=keepaxes, commutative=True)
            return wsum / num

Activity

  1. added this to the 0.30 milestone on Mar 7, 2018
  2. modified the milestones: 0.30, 0.31 on Jul 18, 2018
  3. removed this from the 0.31 milestone on Aug 1, 2019
  4. added this to the nice_to_have milestone on Oct 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions