1

I have a matrix of size (n_classes, n_features) and i want to compute the pairwise euclidean distance of each pair of classes so the output would be a (n_classes, n_classes) matrix where each cell has the value of euclidean_distance(class_i, class_j).

I know that there is this scipy spatial distances (http://docs.scipy.org/doc/scipy-0.14.0/reference/spatial.distance.html) and sklearn.metric.euclidean distances (http://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.euclidean_distances.html) but i want to use this in Theano software so i need a pure mathematical formula rather than functions that compute the results.

for example i need a series of transformations like A = X * B, D = X.T-X, results = D.T something that contains just matrix mathematical operations not functions.

Ash
  • 3,428
  • 1
  • 34
  • 44

1 Answers1

3

You can do this using numpy broadcasting as shown in this gist. It should be straightforward to convert this to Theano code, or just reference @eickenberg's comment above, since he's the one who showed me how to do this!

Kyle Kastner
  • 1,008
  • 8
  • 7
  • I also found [this code](http://trunghuyduong.blogspot.com.au/2011/09/pair-wise-weighted-euclidean-distance.html) which is doing the same thing. Thanks Kyle. – Ash Oct 29 '14 at 00:04
  • 2
    +1 :) - if I read your gist correctly, in `np.exp(-(x1[np.newaxis, :, :] - x2[:, np.newaxis, :])[:, :, 0] ** 2).T` you seem to be collecting only the distance on the first coordinate. Wouldn't you want `np.exp(-((x1[np.newaxis, :, :] - x2[:, np.newaxis, :]) ** 2).sum(2)).T` ? – eickenberg Oct 29 '14 at 09:26
  • Yeah... that looks like a bug. All features should matter :) – Kyle Kastner Oct 29 '14 at 13:21