【问题标题】:what's the difference between np.linalg.norm(a-x) and np.linalg.norm(a) - np.linalg.norm(x)?np.linalg.norm(a-x) 和 np.linalg.norm(a) - np.linalg.norm(x) 有什么区别?
【发布时间】:2021-07-15 14:09:02
【问题描述】:

我正在尝试从图像中提取特征,然后将这些特征与数千张图像中的一系列特征进行比较。

image = Image.open('C:/Users/Timmi/Desktop/test_image.jpg')

query = fe.extract(image)
print(np.shape(query))
print(np.linalg.norm(query))

dists = np.linalg.norm(features - query, axis=1)
print(dists)
print(np.shape(features))
dists2 = (np.linalg.norm(features, axis=1)) - (np.linalg.norm(query, 0))
print(dists2)

ids = np.argsort(dists)[:3]
scores = [(dists[id], img_paths[id]) for id in ids]
print(scores)

这是打印出来的

(4096,)
0.99999994
[0.96851873 1.0867099  1.054868   ... 1.2182273  1.2591194  1.2556202 ]
(31303, 4096)
[-1158. -1158. -1158. ... -1158. -1158. -1158.]
[(0.0, WindowsPath('static/img/image1.jpg')), (0.0, WindowsPath('static/img/image2.jpg')), (0.08249867, WindowsPath('static/img/image3.jpg'))]

我试图找出这两行之间的区别:

dists1 = np.linalg.norm(features - query, axis=1)

dists2 = (np.linalg.norm(features, axis=1)) - np.array(np.linalg.norm(query, 0))

【问题讨论】:

  • 这是两种不同的数学运算,所以当然输出不同的结果,你的问题究竟是什么?
  • 这是一道数学题,不是编程题

标签: python arrays numpy


【解决方案1】:

对于向量x = (x1, x2, ..., xn),其欧几里得范数(在numpy 中为linalg.norm)定义为:

sqrt(x1 ** 2 + x2 ** 2 + ... + xn ** 2)

所以features - query向量的范数是:

sqrt((f1 - q1) ** 2 + (f2 - q2) ** 2 + ... + (fn - qn) ** 2)

features 的范数减去query 的范数是:

sqrt(f1 ** 2 + f2 ** 2 + ... + fn ** 2) - sqrt(q1 ** 2 + q2 ** 2 + ... + qn ** 2)

哪些是不同的数量。

【讨论】:

  • 非常感谢,这澄清了一点.. 但我仍然在努力寻找与 np.linalg.norm(features-query, axis=1) 相同的输出而不放两个数组都在同一个函数中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
  • 2011-04-28
  • 1970-01-01
  • 2021-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多