Basics of NumPy Array
Understanding NumPy arrays and their attributes.
Basics of NumPy Array
In this lesson, you will be introduced to the basic concepts related to NumPy arrays, how they differ from Python Lists and why they are referred to n-dimensional arrays (or nd-arrays).
What is a NumPy Array?
A NumPy array is a grid-like data structure that can hold values of a single data type. It may feel similar to Python Lists since it contains information about the raw data, how to locate an element, and how to interpret an element.
Let us understand what a NumPy array looks like with an example.
# Importing the NumPy library as np
import numpy as np
# Creating a NumPy array from a Python list
num_array = np.array([1, 2, 3, 4])
# Printing the content of the variable
print(num_array)As you can see, the content of the num_array variable looks very similar to a Python list, however, it is actually a NumPy array. To prove this, you can check the type of the variable by using the Python built-in function called type().
# Printing the type of the variable
print(type(num_array))The variable has the type of a numpy.ndarray class which means that it is in fact a NumPy array and not a Python List.
What is the difference between a NumPy Array and a Python List?
Although the contents of a NumPy array and a Python List may seem similar, they are quite different.
A Python List is heterogeneous and can contain elements of different data types. On the other hand, a NumPy array is homogeneous and can only contain elements of a single data type. Furthermore, a NumPy array consumes less memory and is more optimized for numerical operations in comparison to Python Lists.
Also, you can specify the data type for a NumPy array and optimize it even further. However, the same cannot be done using a Python List.
Why is a NumPy Array called an n-dimensional array?
NumPy arrays are called n-dimensional arrays or ndarrays since they contain ‘n’ number of axes/dimensions.
If you are not familiar with Linear Algebra, the concept of dimensionality may feel new to you. Therefore, the sections below will give you a quick crash course on what dimensions are using 1D, 2D, and 3D NumPy arrays as examples.
What is a 1D NumPy Array?
A 1D NumPy array is an array that contains a single axis. It represents a one-dimensional vector and has its shape defined by the number of data elements it holds.
Consider a NumPy array as defined in the code example below,
# Creating a 1D array
num_array = np.array([1, 2, 3])
# Printing the content of the variable
print(num_array)Here, the NumPy array represents a 1D vector with the elements 1, 2 and 3. Also, the shape of the array is 3 since there are 3 different elements in the NumPy array.
What is a 2D NumPy Array?
A 2D NumPy array is an array that contains two axes. It represents a two-dimensional vector, called a 'matrix', and its shape is defined by the number of data elements it holds in its two axes respectively.
Consider a NumPy array as defined in the code example below,
# Creating a 2D array
num_array = np.array([[1, 2, 3], [4, 5, 6]])
# Printing the content of the variable
print(num_array)Here, the NumPy array represents a 2D vector with the elements [1 2 3] and [4 5 6]. Also, the shape of the array is (2, 3) since there are 2 and 3 elements of data in the row and column axis respectively.
Note: The names ‘Row Axis’ and ‘Column Axis’ are only given to the axes for explanation purposes. It is more common to name the axes as ‘Axis 1’ and ‘Axis 2’ since it is more general.
What is a 3D NumPy Array?
A 3D NumPy array is an array that contains three axes. It represents a three-dimensional vector and has its shape defined by the number of data elements it holds in its three axes respectively.
# Creating a 3D array
num_array = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
# Printing the content of the variable
print(num_array)In the example above, the NumPy array contains three different axes, and the shape of the array is (2, 2, 3).
What is a nD NumPy Array?
Generalizing the concept of 1D, 2D and 3D NumPy arrays even further, you can think of nD NumPy arrays as finite-value arrays having ‘n’ number of axes.
Dimensionality Hack: You can find the dimensionality of a NumPy array by counting the total number of brackets wrapping the innermost element of a NumPy array.
Attributes of a NumPy Array
The attributes of a NumPy array define the intrinsic information of the array. The following table shows a list of NumPy attributes along with their meaning.
Attributes of a NumPy Array | Definition |
|---|---|
| flags | Information about the memory layout of the array. |
| shape | Tuple of array dimensions. |
| strides | Tuple of bytes to step in each dimension when traversing an array. |
| ndim | Number of array dimensions. |
| data | Python buffer object pointing to the start of the array's data. |
| size | Number of elements in the array. |
| itemsize | Length of one array element in bytes. |
| nbytes | Total bytes consumed by the elements of the array. |
| base | Base object if memory is from some other object. |
Consider the example given below that shows you all the attributes of a 1D NumPy array.
# Creating a 1D array
num_array = np.array([1, 2, 3])
# Printing the content
print("The content of the NumPy array: ", num_array)
# Printing the flags attribute
print("The flags of the NumPy array: ", num_array.flags)
# Printing the shape attribute
print("The shape of the NumPy array: ", num_array.shape)
# Printing the strides attribute
print("The strides of the NumPy array: ", num_array.strides)
# Printing the ndim attribute
print("The ndim of the NumPy array: ", num_array.ndim)
# Printing the data attribute
print("The data of the NumPy array: ", num_array.data)
# Printing the size attribute
print("The size of the NumPy array: ", num_array.size)
# Printing the itemsize attribute
print("The itemsize of the NumPy array: ", num_array.itemsize)
# Printing the nbytes attribute
print("The nbytes of the NumPy array: ", num_array.nbytes)
# Printing the base attribute
print("The base of the NumPy array: ", num_array.base)To understand NumPy array attributes even better, you can print out the attributes of 2D and 3D NumPy arrays as an exercise.