Euclidean distance, different results between Scipy, pure Python and Java

I played with various implementations of the Euclidean distance metric, and I noticed that I got different results for Scipy, pure Python, and Java.

Here, as I calculate the distance using Scipy (= option 1):

distance = scipy.spatial.distance.euclidean(sample, training_vector)

here is the Python implementation that I found on the forum (option 2):

distance = math.sqrt(sum([(a - b) ** 2 for a, b in zip(training_vector, sample)]))

and finally here is my implementation in Java (option 3):

public double distance(int[] a, int[] b) {
    assert a.length == b.length;
    double squaredDistance = 0.0;
    for(int i=0; i<a.length; i++){
        squaredDistance += Math.pow(a[i] - b[i], 2.0);
    }
    return Math.sqrt(squaredDistance);
}

sample training_vector 1-D 784, MNIST. sample training_vector. , (.. 1936 1, 1914 2 1382 3). , sample training_vector 1 2 (.. 1 ), . , ...?

: k-NN- MNIST. Java 94% 100 2700 . Python 1 75%...

- , ? , CSV .

Java 8, Python 2.7 Scipy 1.0.0.

Edit: 2

distance = math.sqrt(sum([(float(a) - float(b)) ** 2 for a, b in zip(training_vector, sample)]))

:

  • ubyte (, , ...)
  • 1 2 .
  • 2 ( Python) 3 (Java)

, : SciPy (.. ?)?

+6
2

, : , , pandas dtype=np.uint8. , sample training_vector numpy uint8. np.float32, . np.uint32, .

, , , , SciPy uint8. , SciPy ? , , , . , !

+1

, . , Python 3.6.1, SciPy 0.19, , Python 2.7.

import numpy as np
import scipy.spatial.distance
import math

sample = np.random.random(784)
training_vector = np.random.random(784)

distance_scipy = scipy.spatial.distance.euclidean(training_vector, sample)
distance_python = math.sqrt(sum([(a - b) ** 2 for a, b in zip(training_vector, sample)]))
distance_scipy_flipped = scipy.spatial.distance.euclidean(sample, training_vector)
distance_python_flipped = math.sqrt(sum([(a - b) ** 2 for a, b in zip(sample, training_vector)]))

print(distance_scipy, distance_python, distance_scipy_flipped, distance_python_flipped, sep='\n')

:

11.891044968771823
11.891044968771824
11.891044968771823
11.891044968771824
0

All Articles