0

Suppose I have an array like:

from numpy import array
a = array([[[1,2,3]]])

How can I make right indexing to get a view array like:

array([[[1,3]]])

3 Answers 3

0

Ok,

Just found solution.

b=range(3)
b.remove(1)
a[:,:, b]
Sign up to request clarification or add additional context in comments.

Comments

0

There is a np.delete function. Unlike the list remove it does not act in place. Instead it returns a new array. And like your simple solution, it generates an index containing the items to keep (just does so for more general conditions).

In [1222]: np.delete(a,1,-1)
Out[1222]: array([[[1, 3]]])

delete code is rather long, but pure Python. So it can be instructive reading.

Comments

0

You could also use indexing:

In [1]: a = np.array([[[1,2,3]]])
In [2]: a[:,:,::2]
Out[2]: array([[[1, 3]]])

Explanation: The first two : make sure you keep the whole two first dimensions, i.e., the outer two brackets. ::2 iterates over the innermost/third dimension and skips every second value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.