如何计算向量的距离使用短例子

计算距离

In [25]: def distance(x,y):
    m_x = np.array(x)
    m_y = np.array(y)
    result = sum((m_x - m_y)*np.transpose(m_x - m_y))
    return math.sqrt(float(result))

Test:

In [172]: distance([5,10],[7,8])
Out[172]: 2.8284271247461903

需要注意的是numpy的数据类型和原始类型是不同的,numpy进行了封装。参见这里这里

scipy库,包含了十几种不同的距离计算方法

>> scipy.spatial.distance
>> help(scipy.spatial.distance)
...
braycurtis       -- the Bray-Curtis distance.
       canberra         -- the Canberra distance.
       chebyshev        -- the Chebyshev distance.
       cityblock        -- the Manhattan distance.
       correlation      -- the Correlation distance.
       cosine           -- the Cosine distance.
       dice             -- the Dice dissimilarity (boolean).
       euclidean        -- the Euclidean distance.
       hamming          -- the Hamming distance (boolean).
       jaccard          -- the Jaccard distance (boolean).
       kulsinski        -- the Kulsinski distance (boolean).
       mahalanobis      -- the Mahalanobis distance.
       matching         -- the matching dissimilarity (boolean).
       minkowski        -- the Minkowski distance.
       rogerstanimoto   -- the Rogers-Tanimoto dissimilarity (boolean).
       russellrao       -- the Russell-Rao dissimilarity (boolean).
       seuclidean       -- the normalized Euclidean distance.
       sokalmichener    -- the Sokal-Michener dissimilarity (boolean).
       sokalsneath      -- the Sokal-Sneath dissimilarity (boolean).
       sqeuclidean      -- the squared Euclidean distance.
       wminkowski       -- the weighted Minkowski distance.
       yule             -- the Yule dissimilarity (boolean).

sklearn库,著名的python机器学习库

sklearn.metrics.pairwise.pairwise_distances
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容