Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crop part of np.array

Ihave a numpy array A like

A.shape
(512,270,1,20)

I dont want to use all the 20 layers in dimension 4. The new array should be like

Anew.shape
(512,270,1,2)

So I want to crop out 2 "slices" of the array A

like image 510
refle Avatar asked Oct 20 '15 09:10

refle


2 Answers

From the python documentation, the answer is:

start = 4 # Index where you want to start.
Anew = A[:,:,:,start:start+2]
like image 151
Chiel Avatar answered Sep 19 '22 13:09

Chiel


You can use a list or array of indices rather than slice notation in order to select an arbitrary sequence of indices in the final dimension:

x = np.zeros((512, 270, 1, 20))
y = x[..., [4, 10]] # the 5th and 11th indices in the final dimension
print(y.shape)
# (512,270,1,2)
like image 34
ali_m Avatar answered Sep 22 '22 13:09

ali_m