多维切片

时间:2018-03-26 05:09:58

标签: arrays python-2.7 performance numpy slice

我想多次切出数组Inside Subject.js: const SubjectSchema = new Schema({ topics: [{ type: Schema.Types.ObjectId, ref: 'topic' }] ... 的部分内容。目前我正在使用for循环,我希望通过矩阵计算替代它以在速度方面获得更好的性能。

foo

这是我目前的做法。

foo = np.arange(6000).reshape(6,10,10,10)
target = np.zeros((100,6,3,4,5))
startIndices = np.random.randint(5, size=(100))

我试图将切片表示为数组,但我无法找到正确的表示。

1 个答案:

答案 0 :(得分:3)

我们可以利用基于np.lib.stride_tricks.as_stridedscikit-image's view_as_windows进行有效的补丁提取,就像这样 -

from skimage.util.shape import view_as_windows

# Get sliding windows (these are simply views)
WSZ = (1,3,4,5) # window sizes along the axes
w = view_as_windows(foo,WSZ)[...,0,:,:,:]

# Index with startIndices along the appropriate axes for desired output
out = w[:,startIndices, startIndices, startIndices].swapaxes(0,1)

相关:

NumPy Fancy Indexing - Crop different ROIs from different channels

Take N first values from every row in NumPy matrix that fulfill condition

Selecting Random Windows from Multidimensional Numpy Array Rows

how can I extract multiple random sub-sequences from a numpy array