0

I want to understand a specific part of code which computes a distance matrix between two 2d. matrices.

the smarter and easiest way to do it is by using scipy like this code

import scipy
import scipy.spatial

D = scipy.spatial.distance.cdist(X_norm, X_norm)

​but there is an alternative way which i have a problem with to understand. It is this one

Dalt = np.sqrt(((X_norm ** 2).sum(axis=1).reshape((1, len(X_norm))) \
      + (X_norm ** 2).sum(axis=1).reshape((len(X_norm), 1)) \
      - 2 * numpy.dot(X_norm, X_norm.T)) + 1e-7)

print(((Dalt - D) ** 2).mean())

What I actually do not understand is the last part when he finally add this part

+ le-7)
dave-cz
  • 413
  • 4
  • 22
Ahmed Mohamed
  • 51
  • 1
  • 9
  • 1
    Backslash is the way to continue the expression on the next line without having an indentation error – Julien Rousé Nov 22 '18 at 16:42
  • see https://stackoverflow.com/q/53162/3729797 – Julien Rousé Nov 22 '18 at 16:44
  • Thankx alot for you comment – Ahmed Mohamed Nov 22 '18 at 17:03
  • @AhmedMohamed, that `+ 1e-7` actually causes a small error and is not necessary. I'm not sure why the one who created that added that value. Usually, adding very small values is used to prevent computational errors however I could not identify the error they're trying to avoid in this case – jtitusj Nov 22 '18 at 18:22
  • 1
    Question has nothing to do with `machine-learning` or `artificial-intelligence` - kindly do not spam the tags (removed). – desertnaut Nov 24 '18 at 14:02

0 Answers0