1 回答

TA贡献1780条经验 获得超1个赞
在这个最小的例子中,我们看到 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来处理阵列的打印质量。
添加回答
举报