-
-
Notifications
You must be signed in to change notification settings - Fork 11k
ENH: Increase average calculation performance. See #5507 #5525
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
Conversation
@@ -514,8 +514,7 @@ def average(a, axis=None, weights=None, returned=False): | |||
avg = a.mean(axis) | |||
scl = avg.dtype.type(a.size/avg.size) | |||
else: | |||
a = a + 0.0 | |||
wgt = np.asarray(weights) | |||
wgt = np.asarray(weights, dtype=float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As said in the corresponding issue, you should use np.result_type(weights, np.float)
instead of float
only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately that won't work as result_type needs an ndarray as input, which is was asarray creates
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe asarray needs an argument to say 'make the dtype at least this high
on the casting graph (higher is OK)'?
On 30 Jan 2015 16:50, "Julian Taylor" notifications@github.com wrote:
In numpy/lib/function_base.py
#5525 (comment):@@ -514,8 +514,7 @@ def average(a, axis=None, weights=None, returned=False):
avg = a.mean(axis)
scl = avg.dtype.type(a.size/avg.size)
else:
a = a + 0.0
wgt = np.asarray(weights)
wgt = np.asarray(weights, dtype=float)
unfortunately that won't work as result_type needs an ndarray as input,
which is was asarray creates—
Reply to this email directly or view it on GitHub
https://github.com/numpy/numpy/pull/5525/files#r23854432.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guys, the calculation down below already does a result type for weights, you do not need to cast it here (unless you want at least 64bit floats). As for a, you can actually use result type to cast it as suggested, and just do the cast in the sum like it is done for the weights?
On further consideration, this change doesn't make much sense to me. The OTOH assuming that weights is real is probably OK -- I thought we were BTW, copy=0 is the default for asarray.
|
I think the best solution would look something like:
and later on always make calls to the ufuncs with
There is also a very weird construct a little bit further down:
which we could probably do away with if we implemented the above, as the only conceivable reason I can see to do that would be to have |
Doesn't On Fri, Jan 30, 2015 at 2:55 PM, Jaime notifications@github.com wrote:
|
Yes, you would need to use |
In the current code base by doing
The performance of this change by using the script https://gist.github.com/skuschel/2d148a37a2ce17925fb0 provided by execution time np.average: 3.06e-01 sec What do you guys think? |
Sorry I accidentally closed it. |
Real or complex while preserving inexact type. It is an tricky way to achieve that end. Maybe we should just check for @minhlongdo At some point the commits will need to be squashed into one and the commit message cleaned up. See |
@charris Will do, thanks for the feedback. |
No description provided.