【问题标题】:A Pure Pythonic Pairwise Euclidean distance of rows of a numpy ndarraynumpy ndarray 行的纯 Pythonic Pairwise Euclidean 距离
【发布时间】:2014-12-23 06:31:29
【问题描述】:

我有一个大小为 (n_classes, n_features) 的矩阵,我想计算每对类的成对欧几里得距离,因此输出将是一个 (n_classes, n_classes) 矩阵,其中每个单元格的值为 euclidean_distance(class_i , class_j)。

我知道有这个 scipy 空间距离 (http://docs.scipy.org/doc/scipy-0.14.0/reference/spatial.distance.html) 和 sklearn.metric.euclidean 距离 (http://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.euclidean_distances.html) 但我想在 Theano 软件中使用它,所以我需要一个纯数学公式而不是计算函数结果。

例如,我需要一系列转换,例如 A = X * B、D = X.T-X、结果 = D.T 只包含矩阵数学运算而不是函数的东西。

【问题讨论】:

标签: numpy matrix euclidean-distance theano


【解决方案1】:

您可以使用 numpy 广播来执行此操作,如 this gist 所示。将其转换为 Theano 代码应该很简单,或者只是参考上面@eickenberg 的评论,因为他是向我展示如何做到这一点的人!

【讨论】:

  • 我还发现this code 也在做同样的事情。谢谢凯尔。
  • +1 :) - 如果我正确阅读了您的要点,在np.exp(-(x1[np.newaxis, :, :] - x2[:, np.newaxis, :])[:, :, 0] ** 2).T 中,您似乎只收集了第一个坐标上的距离。你不想np.exp(-((x1[np.newaxis, :, :] - x2[:, np.newaxis, :]) ** 2).sum(2)).T 吗?
  • 是的......这看起来像一个错误。所有功能都应该重要:)
猜你喜欢
  • 2012-10-16
  • 2016-06-27
  • 2017-08-20
  • 2017-06-15
  • 2018-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
相关资源
最近更新 更多