Home > Software engineering >  How to reorder dstack
How to reorder dstack

Time:12-07

I have 6 files with shape (6042,) or 1 column. I used dstack to stack the 6 files in hopes of getting a shape (6042, 1, 6). But after I stack it I get shape (1, 6042, 6). Then I tried to change the order using

new_train = np.reshape(train_x,(train_x[1],1,train_x[2]))

error appears:

IndexError: index 1 is out of bounds for axis 0 with size 1

This is my dstack code:

train_x = dstack([train_data['gx'],train_data['gy'], train_data['gz'], train_data['ax'],train_data['ay'], train_data['az']])

CodePudding user response:

error is because

train_x[1]

tries looking 2nd row of train_x but it has only 1 row as you said shape 1, 6042, 6). So you need to look shape and index it

new_train = np.reshape(train_x, (train_x.shape[1], 1, train_x.shape[2]))

but this can be also doable with transpose

new_train = train_x.transpose(1, 0, 2)

so this changes axes 0 and 1's positions.

Other solution is fixing dstack's way. It gives "wrong" shape because your datas shape not (6042, 1) but (6042,) as you say. So if you reshape the datas before dstack it should also work:

datas = [train_data['gx'],train_data['gy'], train_data['gz'],
         train_data['ax'],train_data['ay'], train_data['az']]

#this list comprehension makes all shape (6042, 1) now
new_datas = [td[:, np.newaxis] for td in datas]

new_train = dstack(new_datas)

CodePudding user response:

You can use np.moveaxis(X, 0, -2), where X is your (1,6042,6) array.

This function swaps the axis. 0 for your source axis and -2 is your destination axis.

  • Related