Selecting elements with a Numpy Array

2021-01-30

Selecting elements with a Numpy Array

To select items in a Numpy array, we can use the same notation as normal python arrays. In the example below, we create an array using the special arange method. Then, we show how to select a single element, and how to select multiple elements using the python slice.

# create a numpy array from a range 0 to 11
arr = np.arange(0, 11)

# Selecting an array like a normal python array
print(arr[3])

# Using the slice notation like numpy to select 1 up to 5 (does not include 5)
print(arr[1:5])

# Select up to 4
print(arr[:4])

# select after 3
print(arr[3:])
GoTea - KoalaTea