Accessing Multi-Dimensional NumPy Arrays

The Maptek Python SDK makes extensive use of two-dimensional (and higher-dimensional) NumPy arrays. Understanding how to efficiently access data in these arrays can greatly simplify the process of writing and understanding scripts.

Consider the following two-dimensional array:

  column 0 column 1 column 2
row 0 57 122 239
row 1 193 93 133
row 2 59 155 134
row 3 243 236 243
row 4 64 189 1

This array can be created in Python using the following code snippet:

import numpy as np
 
array = np.array(
  [
    [57, 122, 239],
    [193, 93, 133],
    [59, 155, 134],
    [243, 236,243],
    [64, 189, 1],
  ]
)

To access a single value in a two-dimensional array, it is useful to think of the array as a collection of one-dimensional arrays. For example, array[0] would return the 0th row of the array (highlighted in green below):

  column 0 column 1 column 2
row 0 57 122 239
row 1 193 93 133
row 2 59 155 134
row 3 243 236 243
row 4 64 189 1

To retrieve an element from a specific row, add a second index. For example, array[0, 1] returns the element located in the 0th row and 1st column (highlighted in green below):

  column 0 column 1 column 2
row 0 57 122 239
row 1 193 93 133
row 2 59 155 134
row 3 243 236 243
row 4 64 189 1

To access the element in the ith row and kth column, use the syntax array[i, k].

One of the most useful operations on a two-dimensional array is retrieving entire columns. This can be accomplished using the : (colon) operator for the row index in conjunction with the column index. For example, array[:, 0] returns all elements in column 0 (highlighted in green below):

  column 0 column 1 column 2
row 0 57 122 239
row 1 193 93 133
row 2 59 155 134
row 3 243 236 243
row 4 64 189 1

Using : for the first dimension essentially means "all rows." If the array represents an array of points, the first column represents the x-coordinates for each point. Similarly, if the array represents colours, the first column may contain the red values for each colour.

More complex indexing is also possible. For instance, array[1:-1, 1:-1] returns the elements highlighted in green below:

  column 0 column 1 column 2
row 0 57 122 239
row 1 193 93 133
row 2 59 155 134
row 3 243 236 243
row 4 64 189 1

An index of x:y selects all elements between index x (inclusive) and y (exclusive). Thus, 1:4 selects elements with indices 1, 2, and 3. An index of -1 indicates “the last element in the array”, so 1:-1 includes all elements except the first and last. This logic extends to all negative indices; for example, -2 represents "the second-last element." An index of 2:-2 would include all elements except the first two and last two.

Indexing can also specify a step to select certain rows and columns. For example, array[::2] returns every second row (highlighted in green below):

  column 0 column 1 column 2
row 0 57 122 239
row 1 193 93 133
row 2 59 155 134
row 3 243 236 243
row 4 64 189 1

A starting point can be specified to retrieve specific rows, such as even rows. For example, array[1::2] returns the elements highlighted in green below:

  column 0 column 1 column 2
row 0 57 122 239
row 1 193 93 133
row 2 59 155 134
row 3 243 236 243
row 4 64 189 1

This step-based indexing also works with columns. For example, array[:, ::2] returns every second column (highlighted in green below):

  column 0 column 1 column 2
row 0 57 122 239
row 1 193 93 133
row 2 59 155 134
row 3 243 236 243
row 4 64 189 1

These indexing techniques can be combined for both rows and columns. For example, array[::2, ::2] retrieves the elements highlighted in green below:

  column 0 column 1 column 2
row 0 57 122 239
row 1 193 93 133
row 2 59 155 134
row 3 243 236 243
row 4 64 189 1