I am trying two methods to implement the square result of euclidean distance.
By Numpy:
def inference(feature_list):
distances = np.zeros(len(feature_list))
for idx, pair in enumerate(feature_list):
distances[idx] = euclidean_distances(pair[0].reshape((1, -1)), pair[1].reshape((1, -1))).item()
distances[idx] = distances[idx] * distances[idx]
return distances
By python:
def inference1(feature_list):
distances = np.zeros(len(feature_list))
for idx, pair in enumerate(feature_list):
for pair_idx in range(len(pair[0])):
tmp = pair[0][pair_idx] - pair[1][pair_idx]
distances[idx] += tmp * tmp
return distances
Code to test the result is:
def main(args):
d = 128
n = 100
array2 = [(np.random.rand(d)/4, np.random.rand(d)/3) for x in range(n)]
result = sample.inference(array2)
print(list(result)) # print result 1
result = sample.inference1(array2)
print(list(result)) # print result 2
The results are different when n reaches 100000, while the results stay the same when n is small.
Why would it happen? How can I get the same result?