Python > Working with Data > Numerical Computing with NumPy > Indexing and Slicing

Advanced NumPy Indexing: Integer Array Indexing

This snippet focuses on advanced indexing techniques using integer arrays to select elements from NumPy arrays in a non-contiguous manner. It also showcases how to use this technique to modify array values.

Creating a Sample Array

We begin by importing the NumPy library. A one-dimensional array named arr containing numbers from 10 to 19 (inclusive) is created using np.arange(10, 20).

import numpy as np

arr = np.arange(10, 20)
print(arr)

Integer Array Indexing

Here, indices is a list or array of integer indices. arr[indices] selects elements at the specified indices. The result is a new array containing the elements at those positions. In this case, the elements at indices 1, 3, 5, and 7 are selected from arr.

indices = [1, 3, 5, 7]
selected_elements = arr[indices]
print(selected_elements)

Integer Indexing with Multidimensional Arrays

For multidimensional arrays, integer array indexing allows you to select elements based on corresponding row and column indices. row_indices and col_indices are lists of row and column indices, respectively. arr_2d[row_indices, col_indices] selects elements at the specified row-column pairs. The element at (0, 0), (1, 1) and (2, 0) will be returned.

arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
row_indices = [0, 1, 2]
col_indices = [0, 1, 0]
selected_elements_2d = arr_2d[row_indices, col_indices]
print(selected_elements_2d)

Modifying Array Elements with Integer Array Indexing

You can modify elements using integer array indexing as well. In this example, the elements at indices 0, 2, and 4 are assigned the value 100. This directly modifies the original array.

arr = np.arange(5)
indices = [0, 2, 4]
arr[indices] = 100
print(arr)

Broadcasting with Integer Array Indexing

Broadcasting rules apply when modifying array elements with integer array indexing. The right-hand side must be broadcastable to the shape implied by the index arrays. In this case, we assign new values to the second column (index 1) of the specified rows.

arr = np.array([[1, 2], [3, 4], [5, 6]])
indices = [0, 1, 2]
arr[indices, 1] = [7, 8, 9]
print(arr)

Real-Life Use Case

Consider a scenario where you have a database of customer information, and you want to update the addresses of specific customers based on their IDs. You can use integer array indexing to efficiently locate and update the records of those customers within a NumPy array.

Best Practices

  • Ensure that the index arrays have compatible shapes when working with multidimensional arrays.
  • Be aware of potential performance implications when using advanced indexing on large arrays.
  • Double-check that the indices are within the valid range of the array to avoid errors.

When to use them

Use integer array indexing when you need to select or modify elements at specific, non-contiguous indices in an array, especially when the indices are not sequential or predictable through standard slicing.

Memory footprint

Integer array indexing typically creates a copy of the selected elements, increasing memory usage compared to basic slicing, which creates a view. Modifying elements in place, however, does not create new arrays.

Interview Tip

Be prepared to discuss the differences between basic slicing and integer array indexing, including their performance and memory implications. Explain scenarios where one technique would be preferred over the other. Also, show understanding of the concept of broadcasting rules when assigning values with integer array indexing.

FAQ

  • What are the advantages of using integer array indexing over basic slicing?

    Integer array indexing allows you to select elements at arbitrary, non-contiguous locations, whereas basic slicing is limited to selecting contiguous portions of the array.
  • Does integer array indexing create a copy or a view of the array?

    Integer array indexing typically creates a copy of the selected elements. Modifications to the selected elements will not affect the original array (unless you modify the original array in place by assigning to selected indexes like in the code snippets).
  • How does broadcasting apply to integer array indexing?

    When assigning values to elements selected by integer array indexing, broadcasting rules are applied. The right-hand side values must be broadcastable to the shape implied by the index arrays.