Basic indexing and slicing
NumPy is a powerful library for numerical computation in Python. Supports multidimensional arrays and various operations on them. Indexing and slicing in NumPy are fundamental operations used to access and manipulate elements within an array. Understanding indexing and slicing is important for working effectively with NumPy arrays, especially when dealing with large data sets or complex calculations.
Let’s look at each process individually.
indexing
Indexing a NumPy array is similar to indexing a Python list. You can use brackets ” to access individual elements or fragments of elements.[ ]” Indexing in NumPy starts from 0.
import numpy as np # Sample 1D array arr = np.array([1, 2, 3, 4, 5]) # Accessing individual elements print(arr[0]) print(arr[2]) # Negative indexing (starts from the end) print(arr[-1]) # Output: 1 # Output: 3 # Output: 5
slicing
Slicing allows you to access part of an array by specifying an index range. The slicing syntax is: [ start : stop : step ]. Returns a new array containing the specified elements.
one-dimensional array
import numpy as np # Sample 1D array arr = np.array([1, 2, 3, 4, 5]) # Slicing print(arr[1:4]) print(arr[:3]) print(arr[2:]) print(arr[::2])
[2 3 4] [1 2 3] # (start is omitted, defaults to 0) [3 4 5] # (stop is omitted, defaults to end) [1 3 5] # (step by 2)
multidimensional array
import numpy as np # Sample 2D array arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Slicing print(arr[0, 1]) print(arr[:, 1]) print(arr[1:, :2])
# (row 0, column 1) 2 # (all rows, column 1) [2 5 8] # (rows 1 and 2, columns 0 and 1) [[4 5] [7 8]]
Boolean indexing
NumPy’s Boolean indexing allows you to select elements from an array based on a condition expressed as a Boolean array. This is a powerful way to filter and manipulate data within an array.
You can create a Boolean array that has the same shape as the original array. Here, each element indicates whether the corresponding element in the original array satisfies a certain condition. You can then use this boolean array to select elements from the original array. You can also combine multiple conditions using logical operators such as ‘.&” (and)“|” (or)and “~” (no).
Let’s look at it through a real example.
one-dimensional array
import numpy as np # Sample 1D array arr = np.array([1, 2, 3, 4, 5]) # Boolean array based on condition bool_arr = arr > 2 print(bool_arr) # Output: [False False True True True]
Now you can filter your array based on the above. This is done by inputting a Boolean array as the index of the original array.
# Using boolean array to index the original array print(arr[bool_arr]) # Output: [3 4 5]
Let’s look at an example with multiple conditions.
import numpy as np # Sample 1D array arr = np.array([1, 2, 3, 4, 5]) # Boolean array based on multiple conditions bool_arr = (arr > 2) & (arr < 5) print(bool_arr) # Output: [False False True True False]
Let’s filter the array again.
# Using boolean array to index the original array print(arr[bool_arr]) # Output: [3 4]
multidimensional array
A Boolean array must match the shape of the original array in all dimensions.
import numpy as np # Sample 2D array arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Boolean array based on condition bool_arr = arr > 5 print(bool_arr) # Output: [[False False False] [False False True] [True True True]]
Now filter the results.
# Using boolean array to index the original array print(arr[bool_arr]) # Output: [6 7 8 9]
nice indexing
NumPy’s fancy indexing means accessing and modifying arrays using indexed arrays or boolean arrays. It allows for more advanced and flexible indexing operations compared to basic indexing and slicing. This method uses an array of indices to index, rather than entering a single number (scalar).
Let’s look at a practical example.
one-dimensional array
import numpy as np # Sample 1D array arr = np.array([1, 2, 3, 4, 5]) # Array of indices indices = np.array([0, 2, 4]) # Fancy indexing to access elements print(arr[indices]) # Output: [1 3 5]
multidimensional array
import numpy as np # Sample 2D array arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Arrays of row and column indices row_indices = np.array([0, 1, 2]) col_indices = np.array([0, 2, 1]) # Fancy indexing to access elements print(arr[row_indices, col_indices]) # Output: [1 6 8]
boolean array
import numpy as np # Sample 1D array arr = np.array([1, 2, 3, 4, 5]) # Boolean array bool_arr = np.array([True, False, True, False, True]) # Boolean array for fancy indexing print(arr[bool_arr]) # Output: [1 3 5]
this is the original Indexing and Slicing This is an educational material created by aicorr.com.
Next: Broadcast