2

Let's say I have a 3d Numpy array:

array([[[0, 1, 2],
        [0, 1, 2],
        [0, 2, 5]]])

Is it possible to remove the first entry from all the rows (those inner most rows). In this case the 0 would be deleted in each row.

Giving us the following output:

[[[1, 2],
  [1, 2],
  [2, 5]]]

2 Answers 2

2
x
array([[[0, 1, 2],
        [0, 1, 2],
        [0, 2, 5]]])

x.shape
# (1, 3, 3)

You can use Ellipsis (...) to select across all the outermost axes, and slice out the first value from each row with 1:.

x[..., 1:]    
array([[[1, 2],
        [1, 2],
        [2, 5]]])

x[..., 1:].shape
# (1, 3, 2)
Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain this syntax?
@JamesMitchell ... means the other axes are not operated on in any way. The 1: indicates to slice the last column in the innermost axis.
1

To complement @coldspeed's response), slicing in numpy is very powerful and can be done in a variety of ways including with the colon operator : in the index, that is

print(x[:,:,1:])
# array([[[1, 2],
#         [1, 2],
#         [2, 5]]])

is equivalent to the established use of the ellipsis.

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.