python - How do I get the largest 2d slice of a numpy array? -


i have 3d numpy array dimensions different. plot slice parallel largest 2 dimensions , midway through smallest. how slice?

e.g. if original data

np.ones(3*4*5).reshape(3,4,5) 

i dataset

np.ones(3*4*5).reshape(3,4,5)[1,:,:] 

that halfway though first dimension smallest , of other 2 dimensions because larger.

you use np.rollaxis such task , work multi-dimensional ndarray, -

def ndim_largest_slice(arr):     shp = arr.shape     return np.rollaxis(arr, np.argmin(shp), 0)[shp[np.argmin(shp)]/2] 

sample runs -

in [511]: arr = np.random.rand(6,7,6,3,4,5)  in [512]: np.allclose(ndim_largest_slice(arr),arr[:,:,:,1,:,:]) out[512]: true  in [513]: arr = np.random.rand(6,7,6,4,5,5)  in [514]: np.allclose(ndim_largest_slice(arr),arr[:,:,:,2,:,:]) out[514]: true  in [515]: arr = np.random.rand(3,4,5)  in [516]: np.allclose(ndim_largest_slice(arr),arr[1,:,:]) out[516]: true 

Comments

Popular posts from this blog

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

matlab - error with cyclic autocorrelation function -

php - Using grpc in Laravel, "Class 'Grpc\ChannelCredentials' not found." -