ndarray – an N-dimensional array object#

ndarray allows mathematical operations on whole blocks of data, using a similar syntax to similar operations between scalar elements. In NumPy, there are many different types for describing scalars, mostly based on types from the C language and those compatible with Python.

See also:

Note:

Whenever this tutorial talks about array or ndarray, in most cases it refers to the ndarray object.

[1]:
import numpy as np
[2]:
py_list = [2020, 2021, 20222]
array_1d = np.array(py_list)
[3]:
array_1d
[3]:
array([ 2020,  2021, 20222])

Nested sequences, such as a list of lists of equal length, can be converted into a multidimensional array:

[4]:
list_of_lists = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
array_2d = np.array(list_of_lists)
[5]:
array_2d
[5]:
array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12]])

Since list_of_lists was a list with three lists, the NumPy array array_2d has two dimensions whose shape is derived from the data. With the attributes ndim and shape we can output the number of dimensions and the outline of array_2d:

[6]:
array_2d.ndim
[6]:
2
[7]:
array_2d.shape
[7]:
(3, 4)

To give you an idea of the syntax, I first create an array of random numbers with five columns and seven slices:

[8]:
data = np.random.randn(7, 3)
data
[8]:
array([[-1.48040214,  0.60483587, -0.2437932 ],
       [-0.42025594, -1.75075057,  0.19677647],
       [ 0.98816551,  0.35657111, -0.223424  ],
       [ 1.10143461,  0.25189838, -1.11756074],
       [ 0.57691653,  0.26666378,  0.68076501],
       [ 1.40382396, -0.21795603, -0.20410514],
       [ 0.64489473,  0.18392548, -0.01361532]])

ndarray is a generic multidimensional container. Each array has a shape, a tuple, which indicates the size of the individual dimensions. With shape, I can output the number of rows and columns in an array:

In addition to np.array, there are a number of other functions for creating new arrays. zeros and ones, for example, create arrays of zeros and ones, respectively, with a specific length or shape. empty creates an array without initialising its values to a specific value. To create a higher dimensional array using these methods, pass a tuple for the shape:

[9]:
np.zeros(4)
[9]:
array([0., 0., 0., 0.])
[10]:
np.ones((3,4))
[10]:
array([[1., 1., 1., 1.],
       [1., 1., 1., 1.],
       [1., 1., 1., 1.]])
[11]:
np.empty((2,3,4))
[11]:
array([[[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]],

       [[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]]])

Note:

You may not safely assume that the np.empty function returns an array of zeros, as it returns uninitialised memory and may therefore contain garbage values.

arange is an array-valued version of the Built-in Python range function:

[12]:
np.arange(4)
[12]:
array([0, 1, 2, 3])

Other NumPy standard functions for creating arrays are:

Function

Description

array

converts input data (list, tuple, array or other sequence types) into an ndarray by either deriving a dtype or explicitly specifying a dtype; by default, copies the input data into the array

asarray

converts the input to an ndarray, but does not copy if the input is already an ndarray

arange

like Python built-in range, but returns an ndarray instead of a list

ones, ones_like

ones creates an array of 1s in the given form and dtype; ones_like takes another array and creates an ones array in the same form and dtype

zeros, zeros_like

like ones and ones_like, but creates arrays with 0s instead

empty, empty_like

creates new arrays by allocating new memory, but does not fill them with values like ones and zeros

full, full_like

creates an array of the given shape and dtype, setting all values to the given fill value; full_like takes another array and creates a filled array with the same shape and dtype

eye, identity

creates a square N × N identity matrix (1s on the diagonal and 0s elsewhere)