Python:使用sklearn流形绘制预先计算的距离矩阵

时间:2018-09-05 15:16:20

标签: python scikit-learn data-science multi-dimensional-scaling

在Python 3.6.3中,我有一个预先计算的距离矩阵D

In[1]: D
Out[1]:
array([[0.00, 305923.00, 269966.00, 349816.00, 304120.00, 326591.00,
        341136.00, 254420.00, 228892.00, 344290.00],
       [305923.00, 0.00, 13901.00, 288851.00, 9496.00, 9793.00, 8863.00,
        11598.00, 388409.00, 9545.00],
       [269966.00, 13901.00, 0.00, 268908.00, 10595.00, 9649.00,
        11120.00, 10683.00, 468215.00, 12278.00],
       [349816.00, 288851.00, 268908.00, 0.00, 275312.00, 277246.00,
        285087.00, 267596.00, 309412.00, 293227.00],
       [304120.00, 9496.00, 10595.00, 275312.00, 0.00, 8569.00, 8765.00,
        10600.00, 418165.00, 8714.00],
       [326591.00, 9793.00, 9649.00, 277246.00, 8569.00, 0.00, 8473.00,
        9147.00, 464342.00, 8777.00],
       [341136.00, 8863.00, 11120.00, 285087.00, 8765.00, 8473.00, 0.00,
        9981.00, 476542.00, 7791.00],
       [254420.00, 11598.00, 10683.00, 267596.00, 10600.00, 9147.00,
        9981.00, 0.00, 331620.00, 9285.00],
       [228892.00, 388409.00, 468215.00, 309412.00, 418165.00, 464342.00,
        476542.00, 331620.00, 0.00, 516956.00],
       [344290.00, 9545.00, 12278.00, 293227.00, 8714.00, 8777.00,
        7791.00, 9285.00, 516956.00, 0.00]])
我试图绘制的

以便可视化群集。在给定here的第一个示例之后,我正在为此使用sklearn.manifold.MDS()

from sklearn import manifold
mds=manifold.MDS(n_components=2, dissimilarity='precomputed')
X_r=mds.fit_transform(D) #returns the embedded coordinates in the D1, D2 space. The distances between points are from the distance matrix D.

### graph
import matplotlib.pyplot as plt
k=2 #the number of clusters
fig=plt.figure(figsize=(11,9))
ax=fig.add_subplot(1,1,1)
colors=('red','blue','green','yellow','k','grey')
for label,color in zip(range(k),colors):
    position=k==label
    ax.scatter(X_r[position,0],X_r[position,1],label="Cluster {0}".format(label),color=color)

ax.set_xlabel("Dimension 1", fontsize=14)
ax.set_ylabel("Dimension 2", fontsize=14)
ax.legend(loc="best",fontsize=14)
ax.set_title("MDS", fontsize=16)
plt.xlim(-300000,300000)
plt.ylim(-300000,300000)
plt.show()

但是,由于position=k==label=False,我的地块是空的。我应该能够看到两个群集。

0 个答案:

没有答案