Vector Norms

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 L^{1} Norm

The length of a vector can be calculated using the L^{1} norm, where the 1 is a superscript of the L. The notation for theL^{1} norm of a vector is ||v||_{1}, where 1 is a subscript. As such, this length is sometimes called the taxicab norm or the Manhattan norm.

                         L^{1}(v)=\left \| v \right \|_{1}

 The L^{1} norm is calculated as the sum of the absolute vector values, where the absolute value of a scalar uses the notation |a_{1}|. 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 L^{1} 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 \left \| v \right \|_{inf}, where inf is a subscript.

                \left \| L \right \|^{inf}(v) = \left \| v \right \|_{inf}

The max norm is calculated as returning the maximum value of the vector , hence the name.

\left \| v \right \|_{inf} = maxa_{1},a_{2},a_{3}

# 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.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值