I am implementing a sliding-window model, where I want to initialize a matrix @t
as the element-wise means of the previous N matrices, where N is the window size.
This is my initial attempt, which displays the last N matrices:
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)]
The desired answer is the element-wise means of the past N matrices. For the series above, it is:
(list_of_arrays[2]+list_of_arrays[1]) / 2 = [[0.7,0.8],[0.9,1.0]]
How should I modify the list comprehension on the answer
line to get the desired answer?
I figured it out. This is the necessary modification to the indicated answer
line in the question:
answer = np.mean([list_of_arrays[t-j] for j in range1(1,N)], axis = 0)
>> array([[ 0.7, 0.8],
>> [ 0.9, 1. ]])