欧氏距离:python和numpy之间的结果不同,实例数量很多

时间:2019-03-19 08:08:50

标签: python python-3.x numpy numpy-ndarray

我正在尝试两种方法来实现欧氏距离的平方结果。

通过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

通过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

测试结果的代码为:

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

当n达到100000时结果不同,而当n小时结果保持不变。

为什么会发生?如何获得相同的结果?

1 个答案:

答案 0 :(得分:0)

在这个最小的示例中,我们看到2个结果之间的差异可以忽略不计。

import numpy as np
from sklearn.metrics.pairwise import euclidean_distances

def inference_sklearn(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

def inference_python(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


d = 128
ns = [100, 1000, 10000, 100000, 200000]
for n in ns: 
    print("n =", n)
    test_array = [(np.random.rand(d)/4, np.random.rand(d)/3) for x in range(n)]
    result_sklearn = inference_sklearn(test_array)
    result_python = inference_python(test_array)
    print(euclidean_distances([result_sklearn], [result_python])[0][0])

输出:

n = 100
0.0
n = 1000
0.0
n = 10000
0.0
n = 100000
0.0
n = 200000
1.52587890625e-05

当您要测试相等性时,不仅要打印结果。您也可以使用numpy.set_printoptions处理阵列的打印质量。