At the core of the NumPy package, is the ndarray object. An ndarray object is the core data structure used in the NumPy library. The ndarray is a n-dimensional array of homogeneous data types. The array is a table of elements (normally numbers) arranged in rows and columns.
NumPy’s array class is called ndarray. It is also known by the alias array.
What are arrays
An array is a structure for storing and retrieving data. NumPy arrays are classified by what are called dimensions, as described below. The dimensions are also called axes in NumPy.
The array dimensions can be 0-dimension, 1-dimension, 2-dimension or more than two dimensions.
0-dimension Array (Scalar)
The 0-dimensional arrays are also called Scalars. A scalar is a single numerical value that is not part of an array. It represents a magnitude and no direction. Scalars do not have any axes.
Scalars can be of various data types, such as integers, floats, or complex numbers. They are the simplest form of data in NumPy and can be created using the np.array() function.
The program below creates a Scalar data type.
PROGRAM EXAMPLE: 0-dimension (Scalar) NumPy Arrays
1-dimension Array
A one-dimensional (1-D) NumPy array is a linear array that contains elements in a single dimension. It is similar to lists in Python. The 1-dimensional array is sometimes referred to as a vector.
Differences between Python lists and NumPy 1-dimension arrays
- The elements in a 1-D array must be of the same type (homogeneous); whereas the lists elements may be of mixed data.
- The performance of computation of 1-D array is much faster as compared to lists for large datasets.
- The memory usage of a 1-D array is generally more efficient than that of lists.
The 1-D array can be created using the example below:
PROGRAM EXAMPLE: A 1-dimensional NumPy Array and List
2-dimensional Array
A 2-dimensional array (sometimes referred to as matrix) comprises of homogeneous data values arranged in a rectangular (or square) array in rows and columns. A 2-D format allow for efficient representation and manipulation of large, structured data.
The program below shows how to create a 2-D array.
PROGRAM EXAMPLE: A 2-dimensional NumPy Array
3-dimensional Array
A 3-dimensional has the elements arranged in three axes. You can imagine a 3-dimensional array as two or more 2D arrays printed on separate pages and overlayed together. For physical visualization of 3-D arrays, think about the 3-D array as a cube with the elements along the three axes. You can create a 3D array using the np.array() function as in the program example below.
PROGRAM EXAMPLE: A 3-dimensional NumPy Array
n-dimensional Array
An array with more than 3 dimensions is difficult to visualize physically since our physical world is limited to three dimensions.
Printing Arrays
Note that when you print an array, NumPy displays it in a similar way to nested lists, but with the following layout:
- the last axis is printed from left to right,
- the second-to-last is printed from top to bottom,
- The rest are also printed from top to bottom, with each slice separated from the next by an empty line.
One-dimensional arrays are printed as rows, 2-dimensionals as matrices, and 3-dimensional as lists of matrices.