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:

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:

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:

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:

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:

Python
Output

Remember how to index a list within a list?

Python
Output

NumPy arrays offer a more concise syntax.

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

Python

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

Python
Output

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

Python
Output

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


What will be the output?

Python

What will be the output?

Python

What will be the output?

Python

What will be the output?

Python

What will be the output?

Python

What will be the output?

Python

What will be the output?

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:

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:

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?

Python