【发布时间】:2017-12-31 19:46:09
【问题描述】:
问题
我有两个 numpy 数组,A 和 indices。
A 的尺寸为 m x n x 10000。
indices 的尺寸为 m x n x 5(从 argpartition(A, 5)[:,:,:5] 输出)。
我想得到一个 m x n x 5 数组,其中包含对应于indices 的A 的元素。
尝试
indices = np.array([[[5,4,3,2,1],[1,1,1,1,1],[1,1,1,1,1]],
[500,400,300,200,100],[100,100,100,100,100],[100,100,100,100,100]])
A = np.reshape(range(2 * 3 * 10000), (2,3,10000))
A[...,indices] # gives an array of size (2,3,2,3,5). I want a subset of these values
np.take(A, indices) # shape is right, but it flattens the array first
np.choose(indices, A) # fails because of shape mismatch.
动机
我正在尝试使用 np.argpartition 按排序顺序为每个 i<m、j<n 获取 5 个最大值 A[i,j],因为数组可能会变得相当大。
【问题讨论】:
标签: python arrays numpy indexing