Open
Description
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