Calculating the length or magnitude of vectors is often required either directly as a regularization method in machine learning, or as part of broader vector or matrix operations. In this tutorial, you will discover the different ways to calculate vector lengths or magnitudes, called the vector norm. After completing this tutorial, you will know:
- The L 1 norm that is calculated as the sum of the absolute values of the vector.
- The L 2 norm that is calculated as the square root of the sum of the squared vector values.
- The max norm that is calculated as the maximum vector values.
1.1 Tutorial Overview
This tutorial is divided into 4 parts; they are:
- Vector Norm
- Vector L 1 Norm
- Vector L 2 Norm
- Vector Max Norm
1.2 Vector Norm
Calculating the size or length of a vector is often required either directly or as part of a broader vector or vector-matrix operation. The length of the vector is referred to as the vector norm or the vector’s magnitude.
The length of a vector is a nonnegative number that describes the extent of the vector in space, and is sometimes referred to as the vector’s magnitude or the norm.
The length of the vector is always a positive number, except for a vector of all zero values. It is calculated using some measure that summarizes the distance of the vector from the origin of the vector space. For example, the origin of a vector space for a vector with 3 elements is (0, 0, 0). Notations are used to represent the vector norm in broader calculations and the type of vector norm calculation almost always has its own unique notation. We will take a look at a few common vector norm calculations used in machine learning.
1.3 Vector
Norm
The length of a vector can be calculated using the norm, where the 1 is a superscript of the L. The notation for the
norm of a vector is
, where 1 is a subscript. As such, this length is sometimes called the taxicab norm or the Manhattan norm.
The norm is calculated as the sum of the absolute vector values, where the absolute value of a scalar uses the notation
. In effect, the norm is a calculation of the Manhattan distance from the origin of the vector space.
||v||1 = |a1| + |a2| + |a3|
In several machine learning applications, it is important to discriminate between elements that are exactly zero and elements that are small but nonzero. In these cases, we turn to a function that grows at the same rate in all locations, but retains mathematical simplicity: the L 1 norm.
The norm of a vector can be calculated in NumPy using the norm() function with a parameter to specify the norm order, in this case 1.
# Example of calculating the L^1 vector norm.
# vector L1 norm
from numpy import array
from numpy.linalg import norm
# define vector
a = array([1, 2, 3])
print(a)
# calculate norm
l1 = norm(a, 1)
print(l1)
1.4 Vecror L^2 Norm
The length of a vector can be calculated using the L 2 norm, where the 2 is a superscript of the L. The notation for the L 2 norm of a vector is ||v||2 where 2 is a subscript.
# Example of calculating the L^2
# vector L2 norm
from numpy import array
from numpy.linalg import norm
# define vector
a = array([1, 2, 3])
print(a)
# calculate norm
l2 = norm(a)
print(l2)
1.5 Vector Max Norm
The length of a vector can be calculated using the maximum norm, also called max norm . Max norm of a vector is referred to as LÎinf where inf is a superscript and can be represented with the infinitiy symbol . The notation for max norm is , where inf is a subscript.
The max norm is calculated as returning the maximum value of the vector , hence the name.
# Example of calculating the max vector norm
# vector max norm
from math import inf
from numpy import array
from numpy.linalg import norm
# define vector
a = array([1, 2, 3])
print(a)
# calculate norm
maxnorm = norm(a, inf)
print(maxnorm)
First, a 3 × 3 vector is defined, then the max norm of the vector is calculated. Running the example first prints the defined vector and then the vector’s max norm.
[1 2 3] 3.0
Max norm is also used as a regularization in machine learning, such as on neural network weights, called max norm regularization.
1.6 Extensions
This section lists some ideas for extending the tutorial that you may wish to explore.
- Create one example using each operation using your own small array data.
- Implement each operation manually for vectors defined as lists of lists.
- Search machine learning papers and find 1 example of each operation being used.
1.7 Summary
In this tutorial, you discovered the different ways to calculate vector lengths or magnitudes, called the vector norm. Specifically, you learned:
- The L 1 norm that is calculated as the sum of the absolute values of the vector.
- The L 2 norm that is calculated as the square root of the sum of the squared vector values.
- The max norm that is calculated as the maximum vector values.