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([[ 0.60972663, -1.09008297, -0.54370769],
[-0.8635271 , 0.65038133, -1.8683108 ],
[-0.7872255 , 0.37868543, -0.84196325],
[ 2.65143687, 1.12253299, -0.3692989 ],
[-0.90096871, 0.31359236, -0.03624293],
[-0.76783756, -0.50115094, 0.09854764],
[-0.52639221, 0.49102799, 0.22972431]])
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 |
|---|---|
|
converts input data (list, tuple, array or other sequence types) into an |
|
converts the input to an |
|
like Python built-in |
|
|
|
like |
|
creates new arrays by allocating new memory, but does not fill them with values like |
|
creates an array of the given |
|
creates a square N × N identity matrix (1s on the diagonal and 0s elsewhere) |