gpt4 book ai didi

python - 如何从 numpy 数组中提取多个随机子序列

转载 作者:太空宇宙 更新时间:2023-11-04 00:21:39 24 4
gpt4 key购买 nike

假设我有一个序列 s 并且我想从中选择 n 个随机子序列,每个子序列的长度为 l 并存储在一个矩阵。有没有比

s = np.arange(0, 1000)
n = 5
l = 10
i = np.random.randint(0, len(s)-10, 5)
ss = np.array([s[x:x+l] for x in i])

最佳答案

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

from skimage.util.shape import view_as_windows

# Get sliding windows (these are simply views)
w = view_as_windows(s, l)

# Index with indices, i for desired output
out = w[i]

相关:

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

关于python - 如何从 numpy 数组中提取多个随机子序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48920850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com