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.
参考资源链接:[Matlab详解:向量模的高效计算与可视化应用](https://wenku.csdn.net/doc/6zrpamq5cu?utm_source=wenku_answer2doc_content) 在MATLAB中,计算矩阵每一行向量的模,并将其大小通过图形化展示出来,需要结合矩阵操作、向量模的计算以及MATLAB的绘图功能。为了实现这一目标,你可以使用`norm`函数来计算模,并通过`plot`函数系列来绘制图形。这里提供一个具体的操作步骤: 首先,创建一个矩阵,例如`A = [1,2,3; 4,5,6; 7,8,9]`。接下来,使用`for`循环遍历矩阵的每一行,并计算每一行的向量模。然后,为了可视化这些模的大小,可以使用`bar`函数绘制条形图,每个条形的长度代表对应行向量模的大小。以下是示例代码: ```matlab A = [1,2,3; 4,5,6; 7,8,9]; % 定义矩阵 norms = zeros(size(A,1),1); % 初始化存储向量模的数组 for i = 1:size(A,1) norms(i) = norm(A(i,:)); % 计算每一行向量的模 end bar(norms, 'FaceColor', 'flat') % 绘制条形图 title('Matrix Row Vector Norms'); % 添加图表标题 xlabel('Row Index'); % X轴标签 ylabel('Norm'); % Y轴标签 ``` 通过执行上述代码,你将得到一个条形图,图中每个条形的长度表示矩阵相应行向量的模。这种方式可以直观地展示矩阵中每行向量的大小关系。 为了深入理解和掌握MATLAB中向量模的计算和可视化技术,建议阅读《Matlab详解:向量模的高效计算与可视化应用》。这本书详细介绍了向量模的多种计算方法,并提供了丰富的实例和技巧,帮助你更好地应用这些知识于科学计算和机器学习等领域。 参考资源链接:[Matlab详解:向量模的高效计算与可视化应用](https://wenku.csdn.net/doc/6zrpamq5cu?utm_source=wenku_answer2doc_content)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值