Efficient and precise calculation of the euclidean distance

前端 未结 5 565
独厮守ぢ
独厮守ぢ 2020-12-09 11:50

Following some online research (1, 2, numpy, scipy, scikit, math), I have found several ways for calculating the Euclidean Distance in Python:



        
5条回答
  •  我在风中等你
    2020-12-09 12:34

    This is not exactly answering the question, but it is probably worth mentioning that if you aren't interested in the actual euclidean distance, but just want to compare euclidean distances against each other, square roots are monotone functions, i.e. x**(1/2) < y**(1/2) if and only if x < y.

    So if you don't want the explicit distance, but for instance just want to know if the euclidean distance of vector1 is closer to a list of vectors, called vectorlist, you can avoid the expensive (in terms of both precision and time) square root, but can make do with something like

    min(vectorlist, key = lambda compare: sum([(a - b)**2 for a, b in zip(vector1, compare)])

提交回复
热议问题