Created
April 9, 2015 15:46
-
-
Save derricw/95eab740e1b08b78c03f to your computer and use it in GitHub Desktop.
Rebin an arbitrary numpy ndarray in N dimensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def bin_ndarray(ndarray, new_shape, operation='sum'): | |
""" | |
Bins an ndarray in all axes based on the target shape, by summing or | |
averaging. | |
Number of output dimensions must match number of input dimensions. | |
Example | |
------- | |
>>> m = np.arange(0,100,1).reshape((10,10)) | |
>>> n = bin_ndarray(m, new_shape=(5,5), operation='sum') | |
>>> print(n) | |
[[ 22 30 38 46 54] | |
[102 110 118 126 134] | |
[182 190 198 206 214] | |
[262 270 278 286 294] | |
[342 350 358 366 374]] | |
""" | |
if not operation.lower() in ['sum', 'mean', 'average', 'avg']: | |
raise ValueError("Operation {} not supported.".format(operation)) | |
if ndarray.ndim != len(new_shape): | |
raise ValueError("Shape mismatch: {} -> {}".format(ndarray.shape, | |
new_shape)) | |
compression_pairs = [(d, c//d) for d, c in zip(new_shape, | |
ndarray.shape)] | |
flattened = [l for p in compression_pairs for l in p] | |
ndarray = ndarray.reshape(flattened) | |
for i in range(len(new_shape)): | |
if operation.lower() == "sum": | |
ndarray = ndarray.sum(-1*(i+1)) | |
elif operation.lower() in ["mean", "average", "avg"]: | |
ndarray = ndarray.mean(-1*(i+1)) | |
return ndarray |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for this ... I'm puzzled why this was so difficult to find and not a builtin numpy function.