4

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?

Henry Gomersall
  • 8,434
  • 3
  • 31
  • 54
Zhubarb
  • 11,432
  • 18
  • 75
  • 114

3 Answers3

3

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. ]])
Zhubarb
  • 11,432
  • 18
  • 75
  • 114
  • Your question and answer are useless as a pair for everyone but yourself. Primarily because you've made no attempt to express what the *actual* problem is. Reading between the lines, it seems that you're using a numpy array as a conceptual proxy for the actual object you have, and then you wanted someone to tell you how to do something other than what you actually asked. Please make a modicum of effort to express yourself in a way that reflects your actual issue and don't assume people can read your mind. As it stands, this answer is poor given the question so downvoting. – Henry Gomersall Sep 16 '13 at 12:52
  • 1
    Question: How should I `modify the list comprehension on the answer line` to get the desired answer? This fully encapsulates what I am looking for. There is nothing cryptic or misleading. You do not need to read my mind at any point. – Zhubarb Sep 16 '13 at 12:55
  • But then you go on to say "for the series above it is..." and then present something that most clearly is *not* what you say you want. If you're dealing with numpy arrays as you describe, you don't need a list comprehension (and indeed, is a really poor answer). If you're dealing with something else, then write the question to reflect that. Your list comprehension is just being a slicer in your answer here. – Henry Gomersall Sep 16 '13 at 13:00
  • 1
    @HenryGomersall you couldn't be more wrong. I just used this answer for my own project and it was useful. People like you that take the moderation of stackexchange so seriously, are the reason why great people have been banned. – DLV Oct 07 '20 at 14:57
  • @DLV Well good for you. It's still a bad answer to a bad question, but if you want to implement it in your code then don't let me stop you. The list comprehension is entirely superfluous in the answer above (replace it with `list_of_arrays[-N:]`). The problem is it's not clear that that is sufficient for the general problem. There are many ways to solve programming problems, and most of them are bad. In this case, fixating on a list comprehension led to a poor solution given the question, even if it works. Also, it's "people like me" that fixed the actual error in the code of the question. – Henry Gomersall Oct 08 '20 at 08:36
2

Here's another answer.

N = 3 # window size
m = np.array([
        [[0, 10], 
         [0, 0]], 
        [[0, 0], 
         [1, 1.]],
        [[0, 0], 
         [0, 1.]],
        [[0, 5], 
         [0, 0]],
        [[0, 10], 
         [0, 0]]])
print m.shape
for t in range(m.shape[0]-N):
    print '\nwindow:', t, 'to', t+N-1
    print m[t:t+N,:,:].shape
    print m[t:t+N,:,:].sum(axis=0)/(N)

Output is

(5, 2, 2)

window 0 to 2
(3, 2, 2)
[[ 0.          3.33333333]
 [ 0.33333333  0.66666667]]

window: 1 to 3
(3, 2, 2)
[[ 0.          1.66666667]
 [ 0.33333333  0.66666667]]
offwhitelotus
  • 1,049
  • 9
  • 15
0

The element wise mean for t-N to t (inclusive) is given by

np.mean(list_of_arrays[t-N+1:t+1], axis=0)

Reading between the lines of your question, I think you want to do some other munging prior to taking the mean. Assuming that your list contains objects that contain numpy arrays (say as a .matrix attribute as you suggested in the comment), then a list comprehension could extract the relevant part, so something like the following modification to the above:

np.mean([a.matrix for a in list_of_arrays[t-N+1:t+1]], axis=0)
Henry Gomersall
  • 8,434
  • 3
  • 31
  • 54
  • Thanks, I need list comprehension because the matrices are object fields, i.e. object[1].matrix, object[2].matrix, etc. I cannot access them as `list_of_arrays[-N:].matrix`. But I am no Python expert so let me know if list comprehension is still avoidable. – Zhubarb Sep 16 '13 at 12:12
  • Well, given your code, the above works fine (now modified to give the mean from t-N to t). Are your list elements something other than you've described? – Henry Gomersall Sep 16 '13 at 12:14
  • The list elements are objects that contain the matrices within. Therefore I cannot access the matrices directly. In other words, it is a list that consists of objects that contain the matrices. That is why I used a list comprehension (to individually access the matrices of each object and then process them altogether- as is given in your answer.) – Zhubarb Sep 16 '13 at 12:27
  • So your question is incorrect? The problem is that you describe a situation in which a list comprehension is the wrong (non) solution. What are these objects to which you refer? Is the matrix element a numpy matrix object, or something else? – Henry Gomersall Sep 16 '13 at 12:34