Skip to content

Instantly share code, notes, and snippets.

@KenoLeon
Last active August 13, 2020 00:32
Show Gist options
  • Select an option

  • Save KenoLeon/99a66b764901122a2a38b7e6f606f278 to your computer and use it in GitHub Desktop.

Select an option

Save KenoLeon/99a66b764901122a2a38b7e6f606f278 to your computer and use it in GitHub Desktop.
Indexing_Slicing_Numpy
import numpy as np
linear_spaced_array = np.linspace(0, 100, 11, dtype=np.uint8)
# >>> [ 0 10 20 30 40 50 60 70 80 90 100]
# -------------- SIMPLE INDEXING --------------
firstN = linear_spaced_array[:4]
# >>> [ 0 10 20 30]
lastN = linear_spaced_array[-4:]
# >>> [ 70 80 90 100]
# -------------- SIMPLE SLICING --------------
sliceBetween = linear_spaced_array[2:6]
# >>> [20 30 40 50]
# -------------- SIMPLE CONDITIONAL --------------
lessthan = linear_spaced_array[linear_spaced_array < 50]
# >>> [ 0 10 20 30 40]
# -------------- SIMPLE ASSIGNEMENT --------------
linear_spaced_array[linear_spaced_array > 50] = 100
# >>> [ 0 10 20 30 40 50 100 100 100 100 100]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment