I have a numpy array with shape:
In: imar.shape
Out: (21, 77, 10000)
I want a binned sum on the last axis, with every bin containing 20 items.
The way I'm doing this now is:
np.sum( 开发者_JS百科imar.reshape([-1,500,20]), axis=2 ).reshape(imar.shape[:2])
It's fast, but seems error-prone if I get the arguments to reshape wrong. Is there a better way to do this?
I've looked at np.digitize,histogram,bincount, and some others, but those are value based; I want sum over a set of ranges.
You have the right approach. I asked a similar question a while back:
How can I efficiently process a numpy array in blocks similar to Matlab's blkproc (blockproc) function
There are several approaches to handling the reshape. If you are careful and write a function to do it, you'll be alright. Of course, you need to be certain that you trim your input matrix if it isn't an integer multiple of your block size.
精彩评论