是“规范"吗?相当于“欧几里德距离"? [英] Is "norm" equivalent to "Euclidean distance"?
问题描述
我不确定范数"和欧几里得距离"是否是同一个意思.请你能帮我解决这个区别吗?
I am not sure whether "norm" and "Euclidean distance" mean the same thing. Please could you help me with this distinction.
我有一个 n
by m
数组 a
,其中 m
> 3. 我想计算第二个数据点 a[1,:]
到所有其他点(包括其自身)之间的欧几里得距离.所以我使用了 np.linalg.norm
,它输出两个给定点的范数.但我不知道这是否是获得 ED 的正确方法.
I have an n
by m
array a
, where m
> 3. I want to calculate the Eculidean distance between the second data point a[1,:]
to all the other points (including itself). So I used the np.linalg.norm
, which outputs the norm of two given points. But I don't know if this is the right way of getting the EDs.
import numpy as np
a = np.array([[0, 0, 0 ,0 ], [1, 1 , 1, 1],[2,2, 2, 3], [3,5, 1, 5]])
N = a.shape[0] # number of row
pos = a[1,:] # pick out the second data point.
dist = np.zeros((N,1), dtype=np.float64)
for i in range(N):
dist[i]= np.linalg.norm(a[i,:] - pos)
推荐答案
A norm 是一个函数,它接受一个向量作为输入并返回一个标量值,该值可以解释为该向量的大小"、长度"或大小".更正式地,范数被定义为具有以下数学特性:
A norm is a function that takes a vector as an input and returns a scalar value that can be interpreted as the "size", "length" or "magnitude" of that vector. More formally, norms are defined as having the following mathematical properties:
- 它们按乘法缩放,即对于任何标量 Norm(a·v) = |a|·Norm(v)一个
- 它们满足三角不等式,即Norm(u + v) ≤ Norm(u) + Norm(>v)
- 向量的范数为零当且仅当它是零向量,即 Norm(v) = 0 ⇔ v = 0
- They scale multiplicatively, i.e. Norm(a·v) = |a|·Norm(v) for any scalar a
- They satisfy the triangle inequality, i.e. Norm(u + v) ≤ Norm(u) + Norm(v)
- The norm of a vector is zero if and only if it is the zero vector, i.e. Norm(v) = 0 ⇔ v = 0
欧几里德范数(也称为 L² 范数)只是众多不同范数中的一种 - 还有最大范数、曼哈顿范数等.单个向量的 L² 范数等价于到该点的欧几里德距离到原点,两个向量之差的L²范数等价于两点间的欧式距离.
The Euclidean norm (also known as the L² norm) is just one of many different norms - there is also the max norm, the Manhattan norm etc. The L² norm of a single vector is equivalent to the Euclidean distance from that point to the origin, and the L² norm of the difference between two vectors is equivalent to the Euclidean distance between the two points.
正如 @nobar 的回答所说,np.linalg.norm(x - y, ord=2)
(或只是 np.linalg.norm(x - y)
) 会给你向量 x
和 y
之间的欧几里得距离.
As @nobar's answer says, np.linalg.norm(x - y, ord=2)
(or just np.linalg.norm(x - y)
) will give you Euclidean distance between the vectors x
and y
.
由于您想计算 a[1, :]
与 a
中的其他每一行之间的欧几里德距离,您可以通过消除 a
来更快地完成此操作code>for 循环并广播 a
的行:
Since you want to compute the Euclidean distance between a[1, :]
and every other row in a
, you could do this a lot faster by eliminating the for
loop and broadcasting over the rows of a
:
dist = np.linalg.norm(a[1:2] - a, axis=1)
使用广播自己计算欧几里得距离也很容易:
It's also easy to compute the Euclidean distance yourself using broadcasting:
dist = np.sqrt(((a[1:2] - a) ** 2).sum(1))
最快的方法大概是scipy.spatial.distance.cdist
:
The fastest method is probably scipy.spatial.distance.cdist
:
from scipy.spatial.distance import cdist
dist = cdist(a[1:2], a)[0]
<小时>
(1000, 1000) 数组的一些时间:
Some timings for a (1000, 1000) array:
a = np.random.randn(1000, 1000)
%timeit np.linalg.norm(a[1:2] - a, axis=1)
# 100 loops, best of 3: 5.43 ms per loop
%timeit np.sqrt(((a[1:2] - a) ** 2).sum(1))
# 100 loops, best of 3: 5.5 ms per loop
%timeit cdist(a[1:2], a)[0]
# 1000 loops, best of 3: 1.38 ms per loop
# check that all 3 methods return the same result
d1 = np.linalg.norm(a[1:2] - a, axis=1)
d2 = np.sqrt(((a[1:2] - a) ** 2).sum(1))
d3 = cdist(a[1:2], a)[0]
assert np.allclose(d1, d2) and np.allclose(d1, d3)
这篇关于是“规范"吗?相当于“欧几里德距离"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!