NumPy Array Indexing

Introduction

Indexing is a crucial aspect of working with NumPy arrays, allowing you to access and modify individual elements or subsets of an array. In this chapter, we will cover the basics of array indexing, including how to access elements, slice arrays, and use advanced indexing techniques.

Importing NumPy

Before you start working with NumPy arrays, you need to import the NumPy library. It is common practice to import NumPy with the alias np:

import numpy as np

Accessing Array Elements

You can access elements of a NumPy array using square brackets and indices. NumPy arrays are zero-indexed, meaning the first element has an index of 0.

Example: Accessing Elements in a 1D Array

import numpy as np

# Creating a 1D array
arr = np.array([10, 20, 30, 40, 50])

# Accessing elements
print(arr[0])  # First element
print(arr[2])  # Third element
print(arr[-1]) # Last element

Output:

10
30
50

Example: Accessing Elements in a 2D Array

import numpy as np

# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Accessing elements
print(arr[0, 0])  # Element in the first row, first column
print(arr[1, 2])  # Element in the second row, third column
print(arr[2, -1]) # Element in the third row, last column

Output:

1
6
9

Slicing Arrays

Slicing allows you to access a subset of an array. The syntax for slicing is [start:stop:step].

Example: Slicing a 1D Array

import numpy as np

# Creating a 1D array
arr = np.array([10, 20, 30, 40, 50])

# Slicing arrays
print(arr[1:4])    # Elements from index 1 to 3
print(arr[:3])     # First three elements
print(arr[2:])     # Elements from index 2 to the end
print(arr[::2])    # Every second element

Output:

[20 30 40]
[10 20 30]
[30 40 50]
[10 30 50]

Example: Slicing a 2D Array

import numpy as np

# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Slicing arrays
print(arr[0:2, 1:3])  # Subarray with the first two rows and the last two columns
print(arr[:, 1])      # All rows, second column
print(arr[1, :])      # Second row, all columns
print(arr[:, ::2])    # All rows, every second column

Output:

[[2 3]
 [5 6]]
[2 5 8]
[4 5 6]
[[1 3]
 [4 6]
 [7 9]]

Boolean Indexing

Boolean indexing allows you to select elements from an array that satisfy certain conditions.

Example: Boolean Indexing

import numpy as np

# Creating a 1D array
arr = np.array([10, 20, 30, 40, 50])

# Boolean indexing
print(arr[arr > 30])  # Elements greater than 30

Output:

[40 50]

Example: Boolean Indexing with a 2D Array

import numpy as np

# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Boolean indexing
print(arr[arr > 5])  # Elements greater than 5

Output:

[6 7 8 9]

Advanced Indexing

Advanced indexing allows you to access elements using arrays of indices.

Example: Advanced Indexing with Integer Arrays

import numpy as np

# Creating a 1D array
arr = np.array([10, 20, 30, 40, 50])

# Advanced indexing
indices = np.array([0, 2, 4])
print(arr[indices])  # Elements at index 0, 2, and 4

Output:

[10 30 50]

Example: Advanced Indexing with 2D Arrays

import numpy as np

# Creating a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Advanced indexing
row_indices = np.array([0, 1, 2])
col_indices = np.array([1, 2, 0])
print(arr[row_indices, col_indices])  # Elements (0,1), (1,2), and (2,0)

Output:

[2 6 7]

Conclusion

Indexing is a powerful feature in NumPy that allows you to access and modify elements or subsets of an array. This chapter covered basic indexing, slicing, boolean indexing, and advanced indexing techniques. By mastering these techniques, you can efficiently manipulate data in your numerical computing and data analysis tasks.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top