Is there a way in numpy to retrieve all items in an array except the item of the index provided.
x =
array([[[4, 2, 3],
[2, 0, 1],
[1, 3, 4]],
[[2, 1, 2],
[3, 2, 3],
[3, 4, 2]],
[[2, 4, 1],
[0, 2, 2],
[4, 0, 0]]])
and by asking for
x[not 1,:,:]
you will get
array([[[4, 2, 3],
[2, 0, 1],
[1, 3, 4]],
[[2, 4, 1],
[0, 2, 2],
[4, 0, 0]]])
Thanks
asked Jan 3, 2012 at 12:30
3 Answers 3
In [42]: x[np.arange(x.shape[0])!=1,:,:]
Out[42]:
array([[[4, 2, 3],
[2, 0, 1],
[1, 3, 4]],
[[2, 4, 1],
[0, 2, 2],
[4, 0, 0]]])
answered Jan 3, 2012 at 12:44
3 Comments
eumiro
+1 This is just beautiful! With
x[np.arange(x.shape[0])!=1,:,:]
it would be even perfect :-)unutbu
Thanks for the improvement, eurimo.
wim
I knew unutbu would chime in with a nice answer :) even
x[np.arange(x.shape[0]) != 1]
will work, the other dims will take :
by defaultHave you tried this?
a[(0,2), :, :]
Instead of blacklisting what you don't want to get, you can try to whitelist what you need.
If you need to blacklist anyway, you can do something like this:
a[[i for i in range(a.shape[0]) if i != 1], :, :]
Basically you just create a list with all possible indexes (range(a.shape[0])
) and filter out those that you don't want to get displayed (if i != 1
).
answered Jan 3, 2012 at 12:40
2 Comments
JustInTime
What if I have 1000 2D matrices ?? should it be (0,2,3,4,...).
jcollado
@JustInTime I'm not sure I understand, but the
[i for i in range(a.shape[0]) if i != 1]
expression will let you create a mask for all the indexes you need except 1
regardless of number of matrices because of a.shape[0]
usage.This is quite a generic solution:
x[range(0,i)+range(i+1,x.shape[0]),:,:]
answered Jan 3, 2012 at 12:43
Comments
lang-py
x[::2,:,:]