我很难在numpy向量上执行下面的操作。
我想从previous_n的vector精加工厂取indices样品。
就像我想要执行一个np.take,并对previous_n样本进行切片。
示例:
import numpy as np
vector = np.array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
# number of previous samples
previous_n = 3
indices = np.array([ 5, 7, 12])结果
array([[ 3, 4, 5],
[ 5, 6, 7],
[10, 11, 12]])发布于 2018-01-28 07:37:13
好吧,这好像是我想做的事。发现这里
def stack_slices(arr, previous_n, indices):
all_idx = indices[:, None] + np.arange(previous_n) - (previous_n - 1)
return arr[all_idx]>>> stack_slices(vector, 3, indices)
array([[ 3, 4, 5],
[ 5, 6, 7],
[10, 11, 12]])https://stackoverflow.com/questions/48483935
复制相似问题