我正在实现一个滑动窗口模型,我想将一个矩阵 @ 初始化t
为前 N 个矩阵的元素方式,其中 N 是窗口大小。这是我最初的尝试,它显示了最后 N 个矩阵:
list_of_arrays = [np.array([]) for i in range(3)] N=2 # window size # past 3 matrices list_of_arrays[0] = np.array([[0.1,0.2],[0.3,0.4]]) list_of_arrays[1] = np.array([[0.5,0.6],[0.7,0.8]]) list_of_arrays[2] = np.array([[0.9,1.0],[1.1,1.2]]) # at t=3, get element-wise means of previous N matrices t=3 range1 = lambda start, end: range(start, end+1) # modified range function answer = [list_of_arrays[t-j] for j in range1(1,N)]
期望的答案是过去 N 个矩阵的元素均值。对于上面的系列,它是:
(list_of_arrays[2]+list_of_arrays[1]) / 2 = [[0.7,0.8],[0.9,1.0]]
我应该如何修改answer
在线列表理解以获得所需的答案?