Roman
Roman

Reputation: 3241

3D Array indexing in NumPy to reduce number of rows

I have a 3d array with the shape:

data = (5433L, 3543L, 3L)

I want to make new array by subtracting 100 rows from the end:

ans = (5433L-100L, 3543L, 3L)

How to do it?

Upvotes: 2

Views: 79

Answers (1)

Alex Riley
Alex Riley

Reputation: 176948

You can use slicing to stop 100 rows before the end of the array:

ans = data[:-100]

With this notation NumPy slices just the first dimension of data: other dimensions are left intact (i.e. it's equivalent to data[:-100, :, :]).

Note: ans still shares the same underlying memory as data: any changes made to one array will be seen in the other. If you want ans to be a brand new array in memory, you need to explicitly make a copy:

ans = data[:-100].copy()

Upvotes: 2

Related Questions