Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating Higher Dimensional Arrays | NumPy Basics
Ultimate NumPy
course content

Course Content

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

bookCreating Higher Dimensional Arrays

2D Arrays

Let’s now create a higher dimensional array, namely a 2D array:

1234
import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) print(f'2-dimensional array: \n{array_2d}')
copy

Basically, creating a higher-dimensional NumPy array involves passing a higher-dimensional list as the argument of the array() function.

Note

Any NumPy array object is called an ndarray.

Here is a visualization of our 2D array:

We can think of it as a 2x3 matrix.

3D Array (Optional)

Creating 3D arrays is nearly identical to creating 2D arrays. The only difference is that we now need to pass a 3D list as an argument:

12345678
import numpy as np # Creating a 3D array array_3d = np.array([ [[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]] ]) print(f'3-dimensional array: \n{array_3d}')
copy

However, visualizing a 3D array is a bit more complex, but it can still be done:

The array is 3x3x3, which is why we have a cube with each side equal to 3. The innermost 1D arrays lie along axis 2 (e.g., [1, 2, 3]), where each small cube with a side length of 1 is a particular element (number).

All the elements of a 3D array are stored inside these innermost 1D arrays. The cube is just a visual representation to make things clear. The total number of elements (small cubes) is 27 (the volume of the cube).

However, in practice, the approach to handling 3D and higher-dimensional arrays is no different from handling 2D arrays.

Task
test

Swipe to show code editor

Create a 2D array named array_2d:

  • Use the correct function to create a numpy 2D array;
  • Create a 2D array based on two lists (the first argument): [24, 41] and [32, 25] in this order;
  • Set the data type of its elements to np.int8 via specifying the second argument.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3
toggle bottom row

bookCreating Higher Dimensional Arrays

2D Arrays

Let’s now create a higher dimensional array, namely a 2D array:

1234
import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) print(f'2-dimensional array: \n{array_2d}')
copy

Basically, creating a higher-dimensional NumPy array involves passing a higher-dimensional list as the argument of the array() function.

Note

Any NumPy array object is called an ndarray.

Here is a visualization of our 2D array:

We can think of it as a 2x3 matrix.

3D Array (Optional)

Creating 3D arrays is nearly identical to creating 2D arrays. The only difference is that we now need to pass a 3D list as an argument:

12345678
import numpy as np # Creating a 3D array array_3d = np.array([ [[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]] ]) print(f'3-dimensional array: \n{array_3d}')
copy

However, visualizing a 3D array is a bit more complex, but it can still be done:

The array is 3x3x3, which is why we have a cube with each side equal to 3. The innermost 1D arrays lie along axis 2 (e.g., [1, 2, 3]), where each small cube with a side length of 1 is a particular element (number).

All the elements of a 3D array are stored inside these innermost 1D arrays. The cube is just a visual representation to make things clear. The total number of elements (small cubes) is 27 (the volume of the cube).

However, in practice, the approach to handling 3D and higher-dimensional arrays is no different from handling 2D arrays.

Task
test

Swipe to show code editor

Create a 2D array named array_2d:

  • Use the correct function to create a numpy 2D array;
  • Create a 2D array based on two lists (the first argument): [24, 41] and [32, 25] in this order;
  • Set the data type of its elements to np.int8 via specifying the second argument.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3
toggle bottom row

bookCreating Higher Dimensional Arrays

2D Arrays

Let’s now create a higher dimensional array, namely a 2D array:

1234
import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) print(f'2-dimensional array: \n{array_2d}')
copy

Basically, creating a higher-dimensional NumPy array involves passing a higher-dimensional list as the argument of the array() function.

Note

Any NumPy array object is called an ndarray.

Here is a visualization of our 2D array:

We can think of it as a 2x3 matrix.

3D Array (Optional)

Creating 3D arrays is nearly identical to creating 2D arrays. The only difference is that we now need to pass a 3D list as an argument:

12345678
import numpy as np # Creating a 3D array array_3d = np.array([ [[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]] ]) print(f'3-dimensional array: \n{array_3d}')
copy

However, visualizing a 3D array is a bit more complex, but it can still be done:

The array is 3x3x3, which is why we have a cube with each side equal to 3. The innermost 1D arrays lie along axis 2 (e.g., [1, 2, 3]), where each small cube with a side length of 1 is a particular element (number).

All the elements of a 3D array are stored inside these innermost 1D arrays. The cube is just a visual representation to make things clear. The total number of elements (small cubes) is 27 (the volume of the cube).

However, in practice, the approach to handling 3D and higher-dimensional arrays is no different from handling 2D arrays.

Task
test

Swipe to show code editor

Create a 2D array named array_2d:

  • Use the correct function to create a numpy 2D array;
  • Create a 2D array based on two lists (the first argument): [24, 41] and [32, 25] in this order;
  • Set the data type of its elements to np.int8 via specifying the second argument.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

2D Arrays

Let’s now create a higher dimensional array, namely a 2D array:

1234
import numpy as np # Creating a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6]]) print(f'2-dimensional array: \n{array_2d}')
copy

Basically, creating a higher-dimensional NumPy array involves passing a higher-dimensional list as the argument of the array() function.

Note

Any NumPy array object is called an ndarray.

Here is a visualization of our 2D array:

We can think of it as a 2x3 matrix.

3D Array (Optional)

Creating 3D arrays is nearly identical to creating 2D arrays. The only difference is that we now need to pass a 3D list as an argument:

12345678
import numpy as np # Creating a 3D array array_3d = np.array([ [[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 11, 12], [13, 14, 15], [16, 17, 18]], [[19, 20, 21], [22, 23, 24], [25, 26, 27]] ]) print(f'3-dimensional array: \n{array_3d}')
copy

However, visualizing a 3D array is a bit more complex, but it can still be done:

The array is 3x3x3, which is why we have a cube with each side equal to 3. The innermost 1D arrays lie along axis 2 (e.g., [1, 2, 3]), where each small cube with a side length of 1 is a particular element (number).

All the elements of a 3D array are stored inside these innermost 1D arrays. The cube is just a visual representation to make things clear. The total number of elements (small cubes) is 27 (the volume of the cube).

However, in practice, the approach to handling 3D and higher-dimensional arrays is no different from handling 2D arrays.

Task
test

Swipe to show code editor

Create a 2D array named array_2d:

  • Use the correct function to create a numpy 2D array;
  • Create a 2D array based on two lists (the first argument): [24, 41] and [32, 25] in this order;
  • Set the data type of its elements to np.int8 via specifying the second argument.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 3
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt