【问题标题】:how to calculate distance of data points from cluster centers using python如何使用python计算数据点与聚类中心的距离
【发布时间】:2018-02-06 02:47:38
【问题描述】:

我使用了 scipy.spatial.distance.cdist 库,但我无法在循环中生成结果。

有没有办法使用for循环?

谢谢。

【问题讨论】:

  • 您能否提供您已经尝试生成结果的代码?此外,更具体地说明您想要什么输出。
  • 你必须更加更加具体。包括您的代码和示例数据并提出具体问题。

标签: python data-analysis data-science


【解决方案1】:
from scipy.spatial.distance import cdist
from __future__ import division
import numpy as np

x1 =np.array([(1,3),(2,4),(5,6)])
x2 =[(3,7),(4,8),(6,9)]
cdist(x1,x2,metric='euclidean')

Out[20]:
array([[ 4.47213595,  5.83095189,  7.81024968],
       [ 3.16227766,  4.47213595,  6.40312424],
       [ 2.23606798,  2.23606798,  3.16227766]])

np.power((1-3)**2 +(3-7)**2,1/2)
Out[23]: 4.4721359549995796

参考:http://blog.csdn.net/kancy110/article/details/75675574

【讨论】:

    猜你喜欢
    • 2015-02-27
    • 2011-12-09
    • 1970-01-01
    • 2014-06-15
    • 2016-08-02
    • 1970-01-01
    • 2020-12-25
    • 2018-09-30
    • 2021-11-21
    相关资源
    最近更新 更多