使用numpy实现基于欧几里得距离的公式 [英] implementing euclidean distance based formula using numpy

查看:177
本文介绍了使用numpy实现基于欧几里得距离的公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用numpy在python中实现此公式

I am trying to implement this formula in python using numpy

如上图所示,X是numpy矩阵,每个xi是具有n个维的向量,C也是一个numpy矩阵,每个Ci也也是具有n个维的向量,dist(Ci,xi)是介于这两个向量. 我在python中实现了一个代码:

As you can see in picture above X is numpy matrix and each xi is a vector with n dimensions and C is also a numpy matrix and each Ci is vector with n dimensions too, dist(Ci,xi) is euclidean distance between these two vectors. I implement a code in python:

value = 0
for i in range(X.shape[0]):
    min_value = math.inf
    #this for loop iterate k times
    for j in range(C.shape[0]):
        distance = (np.dot(X[i] - C[j],
                           X[i] - C[j])) ** .5
        min_value = min(min_value, distance)
    value += min_value
fitnessValue = value

但是我的代码性能不够好,我正在寻找更快的速度,是否有任何更快的方法可以在python中计算该公式,所以任何想法都会令人感激.

But my code performance is not good enough I'am looking for faster,is there any faster way to calculate that formula in python any idea would be thankful.

推荐答案

通常,在python中,应尽可能避免运行大量次的循环.

Generally, loops running an important number of times should be avoided when possible in python.

在这里,存在一个Scipy函数scipy.spatial.distance.cdist(C, X),该函数计算C和X之间的成对距离矩阵.也就是说,如果调用distance_matrix = scipy.spatial.distance.cdist(C, X),则具有distance_matrix [i,j] = dist(C_i ,X_j).

Here, there exists a scipy function, scipy.spatial.distance.cdist(C, X), which computes the pairwise distance matrix between C and X. That is to say, if you call distance_matrix = scipy.spatial.distance.cdist(C, X), you have distance_matrix[i, j] = dist(C_i, X_j).

然后,对于每个j,您要计算所有i上的dist(C_i,X_j)的最小值.您既不需要循环来计算它!如果您传递轴参数,则函数numpy.minimum会为您完成此操作.

Then, for each j, you want to compute the minimum of the dist(C_i, X_j) over all i. You do not either need a loop to compute this! The function numpy.minimum does it for you, if you pass an axis argument.

最后,通过调用numpy.sum函数完成所有这些最小值的求和.

And finally, the summation of all these minimum is done by calling the numpy.sum function.

这使代码更具可读性和速度:

This gives code much more readable and faster:

import scipy.spatial.distance
import numpy as np
def your_function(C, X):
    distance_matrix = scipy.spatial.distance.cdist(C, X)
    minimum = np.min(distance_matrix, axis=0)
    return np.sum(minimum)

哪个返回与您的函数相同的结果:) 希望这会有所帮助!

Which returns the same results as your function :) Hope this helps!

这篇关于使用numpy实现基于欧几里得距离的公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆