0

I write a program, including the following function, which involves initializing an empty list, and append it during iterations.

def build_window_sequence(x,y,windowsize):
    num_sequence = x.shape[0]
    num_size = x.shape[1]
    x_window_sequence = []
    y_window = []
    for i in range(num_sequence):
        low_index = 0
        high_index = 0
        for j in range(num_size):
            low_index = j
            high_index = low_index+windowsize-1
            current_index = low_index+round(windowsize/2)
            x_window_sequence = x_window_sequence.append(train_x[i,low_index:high_index])
            y_window = y_window.append('train_y[current_index]')
    return x_window, y_window

However, running the program gives the following error message

x_window_sequence = x_window_sequence.append('train_x[i,low_index:high_index]')
AttributeError: 'NoneType' object has no attribute 'append'

Just for more information, the involved arrays have the following shape

train_x shape (5000, 501)
train_y shape  (5000, 501)
user297850
  • 7,705
  • 17
  • 54
  • 76
  • `x_window_sequence = x_window_sequence.append(...)` <- `.append` returns `None`. See e.g. https://stackoverflow.com/q/3840784/3001761, and remember to search your error messages before posting. – jonrsharpe Apr 16 '18 at 15:20

2 Answers2

0
x_window_sequence = x_window_sequence.append(train_x[i,low_index:high_index])

Here you are assigning the result of .append, but .append does not return anything (that is, it returns None). You can read more here.

Colin Ricardo
  • 16,488
  • 11
  • 47
  • 80
0

list.append is an in place operation which returns None.

Therefore, you should append without assigning back to a variable:

x_window_sequence.append(train_x[i,low_index:high_index])
y_window.append('train_y[current_index]')

This is noted explicitly in the Python documentation.

jpp
  • 159,742
  • 34
  • 281
  • 339