Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Dimensions in Arrays

A dimension in arrays refers to one level of depth in nested arrays.

Nested arrays are arrays that contain other arrays as their elements.

0-D Arrays

0-D arrays, also known as scalars, represent the individual elements within an array. Each value in an array is a 0-D array.

Example

Create a 0-D array with the value 42.

import numpy as np

arr = np.array(42)

print(arr)

1-D Arrays

An array composed of 0-D arrays as its elements is referred to as a one-dimensional (1-D) array. These are the most fundamental and widely used type of arrays.

Example

Construct a one-dimensional (1-D) array with the values 1, 2, 3, 4, and 5.

import numpy as np

arr = np.array([12345])

print(arr)

2-D Arrays

An array consisting of one-dimensional (1-D) arrays as its elements is called a two-dimensional (2-D) array. These are commonly used to represent matrices or second-order tensors.

NumPy includes a dedicated submodule for matrix operations known as numpy.mat.

Example

Create a two-dimensional (2-D) array that consists of two arrays: the first with the values 1, 2, 3, and the second with the values 4, 5, 6.

import numpy as np

arr = np.array([[123], [456]])

print(arr)

3-D arrays

An array that contains two-dimensional arrays (matrices) as its elements is called a three-dimensional (3-D) array. These are often used to represent third-order tensors.

Example

Create a three-dimensional (3-D) array consisting of two two-dimensional (2-D) arrays, where each 2-D array contains two arrays with the values 1, 2, 3 and 4, 5, 6.

import numpy as np

arr = np.array([[[123], [456]], [[123], [456]]])

print(arr)