给定以下代码计算列表“vect”中向量之间的距离:
import numpy as np
vect=([0.123, 0.345, 0.789], [0.234, 0.456, 0.567],[0.134, 0.246, 0.831])
def kn():
for j in vect:
c=np.array(j)
for z in vect:
p=np.array(z)
space = np.linalg.norm(c-p)
print space
kn()
有没有办法避免双重“for循环”导致的二次复杂性?
双 for 循环的计算结果(二次)==3X3=9
Ideal computation (what I want) should be (3):
[0.123, 0.345, 0.789] and [0.234, 0.456, 0.567] =
[0.123, 0.345, 0.789] and [0.134, 0.246, 0.831] =
[0.234, 0.456, 0.567] and [0.134, 0.246, 0.831] =
提前感谢您的建议。