【问题标题】:Best way to calculate l2 norm between two 2d arrays in numpy在numpy中计算两个二维数组之间的l2范数的最佳方法
【发布时间】:2022-01-01 07:18:09
【问题描述】:

我正在尝试想出一个快速的方法来calculate l2 distance between the rows of two 2d numpy arrays。所以第一个二维numpy数组是7000 x 100,第二个二维numpy数组是4000 x 100

我想得到一个4000 x 7000 的矩阵,其中每个(i, j) 条目是一个l2 norm between ith row of second 2d numpy array and jth row of first 2d numpy array

我假设我可能必须使用 numpy.linalg.norm,但不太确定如何对操作进行矢量化。

感谢您的帮助!如果问题有任何不清楚之处,请告诉我。

【问题讨论】:

标签: arrays python-3.x numpy-ndarray


【解决方案1】:

scipy 中有一个函数专门用于查找两组向量之间的所有距离:

scipy.spatial.distance.cdist(a, b)

【讨论】:

  • 嗨!我不认为矩阵乘法会起作用,因为我需要得到 2 个行向量之间的欧几里得距离。
  • 现已修复,请在您的问题中将 norm 更改为距离,否则会令人困惑
猜你喜欢
  • 1970-01-01
  • 2019-07-31
  • 1970-01-01
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多