Created
April 9, 2018 11:32
-
-
Save N-McA/64b729a545e31f543f1d08b99c1afaf5 to your computer and use it in GitHub Desktop.
Pull out an array of indexes in numpy, eg for lookup in image
This file contains 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
def extract(idxs, target): | |
trailing_shape = target.shape[idxs.shape[1]:] | |
flat_idxs = np.ravel_multi_index(idxs.T, target.shape[:idxs.shape[1]]) | |
result_shape = (idxs.shape[0], ) + trailing_shape | |
return target.reshape([-1, *trailing_shape])[flat_idxs].reshape(result_shape) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The ever-wise Eric Wieser points out that this is
target[tuple(idxs.T)]