Basics of NumPy Arrays

Greetings! Some links on this site are affiliate links. That means that, if you choose to make a purchase, The Click Reader may earn a small commission at no extra cost to you. We greatly appreciate your support!

Learn the basics of NumPy arrays, how they differ from Python Lists and why they are referred to as n-dimensional 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)
[1 2 3 4]

In the above example, the following steps occur in a sequential manner:

  1. The NumPy library gets imported as np in Python.
  2. The num_array variable gets assigned with a NumPy array created using the array() method. Here, the array() method takes in a Python List containing the elements [1, 2, 3, 4] as parameters and creates a NumPy array containing the same elements.
  3. The content of the num_array gets printed out as [1 2 3 4].

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().

# 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)

# Printing the type of the variable
print(type(num_array))
[1 2 3 4]
<class 'numpy.ndarray'>

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,

# Importing the NumPy library as np
import numpy as np

# Creating a 1D array
num_array = np.array([1, 2, 3])

# Printing the content of the variable
print(num_array)
[1 2 3]
One-dimensional vector in NumPy

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 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,

# Importing the NumPy library as np
import numpy as np

# Creating a 2D array
num_array = np.array([[1, 2, 3], [4, 5, 6]])

# Printing the content of the variable
print(num_array)
[[1 2 3]
 [4 5 6]]
Two-dimensional vector in NumPy

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.

Consider a NumPy array as defined in the code example below,

# Importing the NumPy library as np
import numpy as np

# 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)
[[[1 2 3]
  [4 5 6]]

 [[ 7  8  9]
  [10 11 12]]

In the example above, the NumPy array contains three different axes, and the shape of the array is (2, 2, 3) which is obtained as the following:

  1. The number of elements in Axis 1 is 2 i.e. [ [ [1 2 3] [4 5 6] ] [ [7 8 9] [10 11 12] ] ].
  2. The number of elements in Axis 2 is 2 i.e. [ [ [1 2 3] [4 5 6] ] [ [7 8 9] [10 11 12] ] ].
  3. The number of elements in Axis 3 is 3 i.e. [ [ [1 2 3] [4 5 6] ] [ [7 8 9] [10 11 12] ] ].

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 ArrayDefinition
flagsInformation about the memory layout of the array.
shapeTuple of array dimensions.
stridesTuple of bytes to step in each dimension when traversing an array.
ndimNumber of array dimensions.
dataPython buffer object pointing to the start of the array’s data.
sizeNumber of elements in the array.
itemsizeLength of one array element in bytes.
nbytesTotal bytes consumed by the elements of the array.
baseBase object if memory is from some other object.

Consider the example given below that shows you all the attributes of a 1D NumPy array.

# Importing the NumPy library as np
import numpy as np

# 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)
The content of the NumPy array: [1 2 3] 
The flags of the NumPy array: C_CONTIGUOUS : True 
                              F_CONTIGUOUS : True 
                              OWNDATA : True 
                              WRITEABLE : True 
                              ALIGNED : True 
                              WRITEBACKIFCOPY : False 
                              UPDATEIFCOPY : False 
The shape of the NumPy array: (3,) 
The strides of the NumPy array: (4,) 
The ndim of the NumPy array: 1 
The data of the NumPy array: <memory at 0x000001DE39C71100> 
The size of the NumPy array: 3 
The itemsize of the NumPy array: 4 
The nbytes of the NumPy array: 12 
The base of the NumPy array: None

To understand NumPy array attributes even better, you can print out the attributes of 2D and 3D NumPy arrays as an exercise.


That is all for this lesson on the basics of NumPy arrays.

Leave a Comment