Accessing Multi-Dimensional NumPy Arrays

The Maptek Python SDK makes heavy usage of two (and higher) dimensional NumPy arrays and thus knowing how to access the data in these efficiently can make writing and understanding scripts considerably easier.

For example, 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 can be constructed in code using the following snippet:

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

When it comes to accessing a single value in the two dimensional array, it can be thought of as an array of one dimensional arrays. That is, array[0] would return the 0th row of the array (highlighted in green in the table 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 element in that row can be retrieved by adding a second index. For example, array[0, 1] returns the element highlighted in green in the table 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

Essentially, to access the element in the ith row and kth column requires indexing the array as array[i, k].

One of the most useful operations one can perform on a two dimensional array is getting the columns. This can be achieved using : for the row index in conjunction with the column index. For example, array[:, 0] returns the 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

Setting the index for the first dimension to : essentially means “everything”.

  • If the array was an array of points, the first column is the x coordinates for each point.

  • If the array was an array of colours, the first column is the red values for each colour.

More complicated indexes are also possible. For example: array[1:-1, 1:-1] returns the elements highlighted in green in the table 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 means to return every element with an index between x (inclusive) and y (exclusive).

  • 1:4 thus indicates return the elements with indices 1, 2 and 3

  • An index of -1 indicates “the last element in the array”.

    • Thus 1:-1 indicates every element except the first and last ones.

    • This extends to all negative indices. -2 indicates “the second last element in the array”

    • An index of 2:-2 would indicate every element except the first and last two.

Finally, the index can also be set to a step to only select certain rows and columns. For example the index: array[::2] returns the elements highlighted in red in the following table:

  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 start point can be specified, which allows for getting the even rows. For example the index array[1::2] would return the elements highlighted in green in the following table:

  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 is also compatible with indexing via column. For example the index array[:, ::2] would return the elements highlighted in red in the following table:

  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

Such indexing can also be combined and done on both rows and columns. For example the index array[::2, ::2] would return the elements highlighted in green in the following table:

  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