【问题标题】:How we can find the distance between two points inside a cluster?我们如何找到簇内两点之间的距离?
【发布时间】:2021-02-07 21:01:36
【问题描述】:

我必须找到簇内两点之间的距离。我通过以下代码生成了集群:

X = np.arange(0,10.2,0.16)
Y = np.arange(0,10.2,0.16)
A = np.arange(0,2, 0.005) 
x, y = np.meshgrid(X, Y)
z = np.zeros(x.shape)

x0 = X.mean()
y0 = Y.mean()
b_center1 = [2,8]
b_center2 = [6,2]
r1=0.1*X.max()
r2=0.1*X.max()

for i in range(1, len(X)):
    for j in range(1, len(Y)):
        dist = pdist(np.array([[X[i],Y[j]],b_center1]))
        if (dist[0] < r1):
            z[i][j]=1
        dist = pdist(np.array([[X[i],Y[j]],b_center2]))
        if (dist[0] < r2):
            z[i][j]=1

fig = plt.figure(figsize=(12,10))
plt.scatter(x, y, z, cmap='viridis', edgecolor='k')
plt.gca().set_aspect('equal', adjustable='box')
plt.draw()
plt.xticks([0,1,2,3,4,5,6,7,8,9,10])
plt.yticks([0,1,2,3,4,5,6,7,8,9,10])
plt.show()

This is the clusters I have obtained

d is the distance I have to find

【问题讨论】:

  • 该距离是任何点与所有其他点之间的最短距离。没有什么比这更容易了。
  • 1.在 xmax 和 xmin 之间取一个 x 值。 2. 对于那个 x 值,对所有 y 值进行排序。然后取前两个排序后的 y 值的差。

标签: python geometry cluster-analysis


【解决方案1】:

采用z 数组,即64*64 矩阵与0 and 1。 获取x_index,y_index,其中z==1

np.where(z == 1)
(array([ 7,  7,  7,  7,  7,  ...),
 array([47, 48, 49, 50, 51, ...]))

因此可以看出,对于 x=7,z=1 对于 y=47,y=48,y=50 等。 所以我们要计算(7,47)(7,48)之间的距离 所以,距离是Y[48] - Y[47]Y[49] - Y[48]

print(Y[48] - Y[47])
>>> 0.15999999999999925 # i.e. around 0.16

这仅仅是因为你的行距是0.16,你的点是由0.16分隔的,所以你根本不需要计算。 :p

X = np.arange(0,10.2,0.16)

Y = np.arange(0,10.2,0.16)

A = np.arange(0,2, 0.005)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-10
    • 2011-08-08
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多