In this tutorial, you’ll learn how to generate a zero matrix using the NumPy zeros function. Zero arrays and matrices have special purposes in machine learning. Being able to create them efficiently will allow you to become more capable in linear algebra and machine learning.
By the end of this tutorial, you’ll have learned:
- Why to create zero matrices
- How to use the NumPy
- How to create 1D, 2D, and 3D zero arrays and matricies
- How to change the data types of the zeros in the matrix
What is a Zero Matrix?
A zeros matrix is a special type of matrix where every value is a zero. This allows you to create a matrix that has special properties and characteristics when interacting with other matrices. Typically, a zero matrix is defined as 0m,n, where
n represent the dimensions of that matrix.
This means that a zero matrix of size
(4,4) would look like this:
# A Zero Matrix of size 4x4 [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]
Let’s take a look at some of the characteristics of this type matrix. Let’s imagine we have a zero matrix
0 and another matrix
A. Then, we have the following characteristics:
- A + 0 = 0 + A = A
- A – 0 = A
- A – A = 0
- 0A = 0
Knowing this can allow us to determine some more important characteristics in machine learning. In the following sections, you’ll learn how to generate zero matrices in Python using NumPy.
Understanding the NumPy Zeros Function
In order to create a zero matrix using Python and NumPy, we can use the Numpy
.zeros() function. Let’s take a look the
np.zeros() function function and its parameters:
# The NumPy zeros() function np.zeros( shape, # Int or tuple of ints dtype=float, # Data type for array order='C', # Memory optimization like=None # Reference object to help create )
In this tutorial, we’ll focus on the first two parameters. The
shape= parameter allows you to define the size of the zeros array or matrix that is to be created. The
dtype= parameter allows you to set the data type of the zero matrix being created.
Create a 1-Dimensional Zeros Array in Numpy
Let’s take a look at one of the simplest ways in which we can create a zeros matrix in Python: a one-dimension array of zeros. This can be done by passing in a single integer into the shape argument of the zeros function.
Let’s see how to create a zero array of size 5 using Python:
# Creating an array of zeros import numpy as np array_1d = np.zeros(5) print(array_1d) # Returns: [0. 0. 0. 0. 0.]
By default, NumPy will create a one-dimensional array with the data type of floats.
Create a 2-Dimensional Zeros Matrix in Numpy
NumPy makes it equally easy to create a 2-dimensional zero matrix. We can do this by passing in a tuple of integers that represent the dimensions of this matrix. By default, NumPy will use a data type of floats.
Let’s see how to create a 3×2 matrix of zeros using NumPy:
# Creating a Zeros Matrix in NumPy import numpy as np matrix_2d = np.zeros((3,2)) print(matrix_2d) # Returns: # [[0. 0.] # [0. 0.] # [0. 0.]]
Create a 3-Dimensional Zeros Matrix in Numpy
To create a three dimensional zeros matrix in NumPy, we can simply pass in a tuple of length three. Let’s see how we can create a tuple that is of size
# Creating a 3-Dimensional Zeros Matrix import numpy as np matrix_3d = np.zeros((3,3,2)) print(matrix_3d) # Returns: # [[[0. 0.] # [0. 0.] # [0. 0.]] # [[0. 0.] # [0. 0.] # [0. 0.]] # [[0. 0.] # [0. 0.] # [0. 0.]]]
How to Change the Data Type of a Zeros Matrix in Numpy
By default, NumPy will pass in floats into the zeros matrix it creates using the
np.zeros() function. We can use the
dtype= parameter to change the data type of values in the zeros matrix.
Let’s see how we can create a zeros matrix with integers instead of floats:
# Creating a Zero Matrix with Integers import numpy as np matrix_2d = np.zeros((3,2), dtype=int) print(matrix_2d) # Returns: # [[0 0] # [0 0] # [0 0]]
You can go even further than this and pass in a tuple of data types. This allows you to easily create tuples of zeros of different data types in NumPy:
# Changing data types in zero matrix import numpy as np matrix_2d = np.zeros((3,2), dtype=[('a', 'float'), ('b', 'int')]) print(matrix_2d) # Returns: # [[(0., 0) (0., 0)] # [(0., 0) (0., 0)] # [(0., 0) (0., 0)]]
In the above example, we create a zero matrix that contains tuples of zeros. In the tuple, the first data type is a float while the second is an integer.
Understanding the Order Parameter in Numpy Zeros
NumPy also lets you customize the style in which data are stored in row-major or column-major styles in memory. This gives you additional flexibility to change how memory is handled while creating zero matrices.
By using the
order= parameter, you can pass in either
'F' in order to modify how these values are stored. By using
'C', data will be stored in a row-major format. Meanwhile, when you pass in
'F', the data are stored as column-major (Fortran style).
NumPy will default to using the row-major format. We can change this by setting the
# Changing the matrix to column-major import numpy as np matrix_2d = np.zeros((3,2), order='F') print(matrix_2d) # Returns: # [[0. 0.] # [0. 0.] # [0. 0.]]
In this tutorial, you learned how to use NumPy to create zero matrices. You first learned what the characteristics of a zero matrix are. Then, you learned how to create arrays of zeros, as well as two-dimensional and three-dimensional arrays of zeros in NumPy. Finally, you learned how change the ordering of the matrix.
To learn more about related topics, check out the tutorials below: