Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Numpy
import numpy as np
x = np.arange(10,1,-1)
print(x)
#[Output]:
#[10 9 8 7 6 5 4 3 2]
print(x[np.array([3,3,4,7])])
#[Output]:
#[7 7 6 3]
## Negative indexing is the same as work with single indexes
x[np.array([3,3,-3,8])]
#[Output]:
#array([7, 7, 4, 2])
## Index out of range will give an error
x[np.array([3,3,19,8])]
#[Output]:
#---------------------------------------------------------------------------
#IndexError Traceback (most recent call last)
#<ipython-input-5-9eeb5decb0c8> in <module>()
#----> 1 x[np.array([3,3,19,8])]
#IndexError: index 19 is out of bounds for axis 1 with size 9
x[np.array([[1,1],[2,3]])]
#[Output]:
#array([[9, 9],
# [8, 7]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.