accumulation problem after reshaping multiple arrays

I have x_train, which is an array belong to data waveform, with dimension (475,1501) and I want the final output (seg2) to be (1425,500). I tried the following code:

count=0
sega=[]
seg2=[]
for i in range (0,len(x_train)):

    sega = x_train[i][:(x_train[i].size // 500) * 500].reshape(-1, 500)
    seg2[count:(count+1),500] = sega
    count = count + i

But it complains with the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-137-72281c805a83> in <module>
     10     sega = x_train[i][:(x_train[i].size // 500) * 500].reshape(-1, 500)
     11     print(sega.shape)
---> 12     seg2[count:(count+1),500] = sega
     13     count = count + i
     14 

TypeError: list indices must be integers or slices, not tuple

How can I fix this error?

1 answer

  • answered 2020-06-02 10:40 nik7

    seg2 is a list. it looks like you need to declare it as np.array. Like this:

    seg2 = np.zeros((total_count, 500))
    

    Where total_count=1425.

    You can also use np.concatenate in this way:

    seg2 = np.concatenate([
        x_train[i][:(x_train[i].size // 500) * 500].reshape(-1, 500)
        for i in range(0,x_train.shape[0])
    ], axis=0)