Skip to content

Instantly share code, notes, and snippets.

@Robofied
Created February 15, 2019 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robofied/180c4b84aeecb00d6a6c843e70ac2886 to your computer and use it in GitHub Desktop.
Save Robofied/180c4b84aeecb00d6a6c843e70ac2886 to your computer and use it in GitHub Desktop.
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