NumPy

Introduction to multidimensional NumPy arrays

In this tutorial, you will learn how to use NumPy to work with multidimensional data structures.


So far, we have created very simple one-dimensional arrays, like this one:

import numpy as np np.array([1, 2, 3, 4])
Python

However, NumPy's true power lies in its ability to efficiently handle multidimensional data.

Here's a representation of a tic-tac-toe game in a NumPy array:

import numpy as np np.array([ [0, 1, 0], [1, 0, 1], [0, 0, 1] ])
Python

The first dimension of this array represents the rows of the game, while the second dimension represents the columns.

Hence, we have a 3x3 array.

Each NumPy array object has an attribute array.shape that stores information about the array's dimensions:

import numpy as np arr = np.array([ [0, 1, 0], [1, 0, 1], [0, 0, 1] ]) print(arr.shape)
Python
Output

The attribute returns a tuple with two elements, one for each dimension, and each dimension has three elements.

If we remove a row from the board, the resulting shape attribute will be as follows:

import numpy as np arr = np.array([ [0, 1, 0], [1, 0, 1] ]) print(arr.shape)
Python
Output

When you create an array, you must ensure that each dimension has a consistent size.

In a 2D array, also known as a matrix, each row must have the same number of elements.

If you think about the tic-tac-toe board, you need to make sure that it's rectangular.

Let's see what happens if we attempt to create an array with an inconsistent shape:

import numpy as np arr = np.array([ [0, 1, 0], # row has different number of elements [1, 0], [1, 0, 1] ]) print(arr.shape)
Python
Output

Remember how to index a list within a list?

li = [ [0, 1, 2], [3, 4, 5] ] # get second row, third element print(li[1][2])
Python
Output

NumPy arrays offer a more concise syntax.

You can use a single set of brackets and separate the indices by commas.

array[dim1, dim2]
Python

To get the element at row 2 and column 3, we can do the following:

arr = np.array([ [0, 1, 2], [3, 4, 5] ]) # get second row, third element print(arr[1,2])
Python
Output

Slicing is done in a similar way, with each index separated by a comma:

arr = np.array([ [0, 1, 2], [3, 4, 5] ]) # get second row, second and third element print(arr[1,1:3])
Python
Output

Keep in mind that the resulting array is a dynamic view of the original, not a copy.


What will be the output?

import numpy as np arr = np.array([ [3, 1, 7], [2, 0, 5], [10, 4, 9], [12, 11, 8] ]) print(arr.shape)
Python

What will be the output?

import numpy as np arr = np.array([ [3, 1, 7, 4], [2, 0, 5, 7] ]) print(arr.shape)
Python

What will be the output?

import numpy as np arr = np.array([ [3, 1, 7], [2, 0, 5], [10, 4, 9], [12, 11, 8] ]) print(arr[1,2])
Python

What will be the output?

import numpy as np arr = np.array([ [3, 1, 7], [2, 0, 5], [10, 4, 9], [12, 11, 8] ]) print(arr[1:3])
Python

What will be the output?

import numpy as np arr = np.array([ [3, 1, 7], [2, 0, 5], [10, 4, 9], [12, 11, 8] ]) print(arr[1:3,0])
Python

What will be the output?

import numpy as np arr = np.array([ [3, 1, 7], [2, 0, 5], [10, 4, 9], [12, 11, 8] ]) print(arr[1:3,:2])
Python

What will be the output?

import numpy as np li = [ [3, 1, 7], [2, 0, 5], [10, 4, 9], [12, 11, 8] ] arr = np.array(li) print(len(li), arr.size)
Python

NumPy arrays are not limited to two dimensions.

In fact, you can create arrays with as many dimensions as you need.

If you have an array with three dimensions, you can think of it as a cuboid with height, width, and depth.

Let's create a cuboid and get it's shape:

import numpy as np arr = np.array([ [ [3, 1, 7], [2, 0, 5] ], [ [10, 4, 9], [12, 11, 8] ] ]) print(arr.shape)
Python
Output

The array has three dimensions. The first two dimensions have 2 elements each, and the third has 3.

Indexing works the same way:

import numpy as np arr = np.array([ [ [3, 1, 7], [2, 0, 5] ], [ [10, 4, 9], [12, 11, 8] ] ]) print(arr[1,0,2])
Python
Output

To help remembering the order of the indexes, think of it as moving from the outermost square brackets to the innermost ones.


What will be the output?

import numpy as np arr = np.array([ [ [3, 1, 7], [2, 0, 5] ], [ [10, 4, 9], [12, 11, 8] ] ]) print(arr[0,1,0])
Python