python - NumPy: use 2D index array from argmin in a 3D slice -
i'm trying index large 3d arrays using 2d array of indicies argmin (or related argmax, etc. functions). here example data:
import numpy np shape3d = (16, 500, 335) shapelen = reduce(lambda x, y: x*y, shape3d) # 3d array of [random] source integers intcube = np.random.uniform(2, 50, shapelen).astype('i').reshape(shape3d) # 2d array of indices of minimum value along first axis minax0 = intcube.argmin(axis=0) # 3d array i'd use indices minax0 othercube = np.zeros(shape3d) # 2d array of [random] values i'd assign in othercube some2d = np.empty(shape3d[1:]) at point, both 3d arrays have same shape, while minax0 array has shape (500, 335). i'd assign values 2d array some2d 3d array othercube using minax0 index position of first dimension. i'm trying, doesn't work:
othercube[minax0] = some2d # or othercube[minax0,:] = some2d throws error:
valueerror: dimensions large in fancy indexing
note: i'm using, not numpythonic:
for r in range(shape3d[1]): c in range(shape3d[2]): othercube[minax0[r, c], r, c] = some2d[r, c] i've been digging around web find similar examples can index othercube, i'm not finding elegant. require advanced index? tips?
fancy indexing can little non-intuitive. luckily tutorial has examples.
basically, need define j , k each minidx applies. numpy doesn't deduce shape.
in example:
i = minax0 k,j = np.meshgrid(np.arange(335), np.arange(500)) othercube[i,j,k] = some2d
Comments
Post a Comment