-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generic reducer operation #69
Comments
In #51 and here, I said that negative instead of the innermost So negative This has consequences for |
Worth mentioning: the existence of reducers will also enable several other essential functions (though they are not themselves reducers): def moment(self, n, weight=None):
"Compute the n-th moment of an array with optional weight."
with self.numpy.errstate(invalid="ignore"):
if weight is None:
return self.numpy.true_divide((self**n).sum(), self.count())
else:
return self.numpy.true_divide(((self * weight)**n).sum(), (self * 0 + weight).sum())
def mean(self, weight=None):
"Compute the mean (average) of an array with optional weight."
with self.numpy.errstate(invalid="ignore"):
if weight is None:
return self.numpy.true_divide(self.sum(), self.count())
else:
return self.numpy.true_divide((self * weight).sum(), (self * 0 + weight).sum())
def var(self, weight=None, ddof=0):
"Compute the variance of an array with optional weight and possibly reduce it with a number of degrees of freedom."
with self.numpy.errstate(invalid="ignore"):
if weight is None:
denom = self.count()
one = self.numpy.true_divide(self.sum(), denom)
two = self.numpy.true_divide((self**2).sum(), denom)
else:
denom (self * 0 + weight).sum()
one = self.numpy.true_divide((self * weight).sum(), denom)
two = self.numpy.true_divide(((self * weight)**2).sum(), denom)
if ddof != 0:
return (two - one**2) * denom / (denom - ddof)
else:
return two - one**2
def std(self, weight=None, ddof=0):
"Compute the standard deviation of an array with optional weight and possibly reduce it with a number of degrees of freedom."
with self.numpy.errstate(invalid="ignore"):
return self.numpy.sqrt(self.var(weight=weight, ddof=ddof)) |
This is an operation, so it is similar in scope to
Content::flatten
. (Time management: it's one of the hardest; maybe you don't want to start with this.)The generic reducer operation reduces list depth by 1, much like the
Content::flatten
operation. It takes an array at some depth (axis
parameter, just likeflatten
), a function of two arguments (maybe templated by array type?), and an identity (of that same type).Once the generic reducer is in place, all concrete reducers can be implemented. They are:
false
true
+1
for each argument0
+1
for each non-zero argument0
+
0
*
1
x < y ? x : y
inf
x > y ? x : y
-inf
For integer types, min and max should use the integer type's maximum and minimum value as an identity.
As a later enhancement, there should also be a way to skip
None
in arrays of OptionType.The text was updated successfully, but these errors were encountered: