在Python中获取向量的1范数

时间:2021-11-29 04:17:43

How can I calculate the 1-norm of the difference of two vectors, ||a - b||_1 = sum(|a_i - b_i|) in Python?

如何在Python中计算两个向量差异的1范数,|| a - b || _1 = sum(| a_i - b_i |)?

a = [1,2,3,4]  
b = [2,3,4,5]

||a - b||_1 = 4  

4 个解决方案

#1


Python has powerful built-in types, but Python lists are not mathematical vectors or matrices. You could do this with lists, but it will likely be cumbersome for anything more than trivial operations.

Python具有强大的内置类型,但Python列表不是数学向量或矩阵。你可以用列表来做这件事,但除了琐碎的操作之外,它可能很麻烦。

If you find yourself needing vector or matrix arithmetic often, the standard in the field is NumPy, which probably already comes packaged for your operating system the way Python also was.

如果您经常发现自己需要向量或矩阵运算,那么该领域的标准就是NumPy,它可能已经像Python一样为您的操作系统打包了。

I share the confusion of others about exactly what it is you're trying to do, but perhaps the numpy.linalg.norm function will help:

我分享了其他人对你正在尝试做什么的困惑,但也许numpy.linalg.norm函数会有所帮助:

>>> import numpy
>>> a = numpy.array([1, 2, 3, 4])
>>> b = numpy.array([2, 3, 4, 5])
>>> numpy.linalg.norm((a - b), ord=1)
4

To show how that's working under the covers:

为了说明这是如何在幕后工作:

>>> a
array([1, 2, 3, 4])
>>> b
array([2, 3, 4, 5])
>>> (a - b)
array([-1, -1, -1, -1])
>>> numpy.linalg.norm((a - b))
2.0
>>> numpy.linalg.norm((a - b), ord=1)
4

#2


In NumPy, for two vectors a and b, this is just

在NumPy中,对于两个向量a和b,这只是

numpy.linalg.norm(a - b, ord=1)

#3


You appear to be asking for the sum of the differences between the paired components of the two arrays:

您似乎要求两个数组的配对组件之间的差异总和:

>>> A=[1,2,3,4]
>>> B=[2,3,4,5]
>>> sum(abs(a - b) for a, b in zip(A, B))
4

#4


It is not clear what exactly is required here, but here is my guess

目前尚不清楚这里究竟需要什么,但这是我的猜测

a=[1,2,3,4]
b=[2,3,4,5]
def a_b(a,b):
    return sum(map(lambda a:abs(a[0]-a[1]), zip(a,b)))

print a_b(a,b)

#1


Python has powerful built-in types, but Python lists are not mathematical vectors or matrices. You could do this with lists, but it will likely be cumbersome for anything more than trivial operations.

Python具有强大的内置类型,但Python列表不是数学向量或矩阵。你可以用列表来做这件事,但除了琐碎的操作之外,它可能很麻烦。

If you find yourself needing vector or matrix arithmetic often, the standard in the field is NumPy, which probably already comes packaged for your operating system the way Python also was.

如果您经常发现自己需要向量或矩阵运算,那么该领域的标准就是NumPy,它可能已经像Python一样为您的操作系统打包了。

I share the confusion of others about exactly what it is you're trying to do, but perhaps the numpy.linalg.norm function will help:

我分享了其他人对你正在尝试做什么的困惑,但也许numpy.linalg.norm函数会有所帮助:

>>> import numpy
>>> a = numpy.array([1, 2, 3, 4])
>>> b = numpy.array([2, 3, 4, 5])
>>> numpy.linalg.norm((a - b), ord=1)
4

To show how that's working under the covers:

为了说明这是如何在幕后工作:

>>> a
array([1, 2, 3, 4])
>>> b
array([2, 3, 4, 5])
>>> (a - b)
array([-1, -1, -1, -1])
>>> numpy.linalg.norm((a - b))
2.0
>>> numpy.linalg.norm((a - b), ord=1)
4

#2


In NumPy, for two vectors a and b, this is just

在NumPy中,对于两个向量a和b,这只是

numpy.linalg.norm(a - b, ord=1)

#3


You appear to be asking for the sum of the differences between the paired components of the two arrays:

您似乎要求两个数组的配对组件之间的差异总和:

>>> A=[1,2,3,4]
>>> B=[2,3,4,5]
>>> sum(abs(a - b) for a, b in zip(A, B))
4

#4


It is not clear what exactly is required here, but here is my guess

目前尚不清楚这里究竟需要什么,但这是我的猜测

a=[1,2,3,4]
b=[2,3,4,5]
def a_b(a,b):
    return sum(map(lambda a:abs(a[0]-a[1]), zip(a,b)))

print a_b(a,b)