def section_by_index(array, index, axis=0):
"""
Take the slice of `array` indexed by entries of `index`
along the specified `axis`.
"""
# alternative `axisindex` implementation
# that avoids the index arithmetic
# uses `numpy` fancy indexing instead
# possible index values for each dimension represented
# as `numpy` arrays all having the shape of `index`
indices = np.ix_(*[np.arange(dim) for dim in index.shape])
# the slice is taken along `axis`
# except for the array `index` itself, the other indices
# do nothing except trigger `numpy` fancy indexing
fancy_index = indices[:axis] + (index,) + indices[axis:]
# result has the same shape as `index`
return array[fancy_index]
评论列表
文章目录