python - Python:用 sklearn 流形绘制预先计算的距离矩阵
问题描述
在 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]])
我正在尝试绘制,以便可视化集群。我正在使用这个,按照这里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
. 我应该能够可视化这两个集群。
解决方案
推荐阅读
- mysql - Mysql返回多行而不是一行图像数组
- consul - 代理和目录之间的consul区别
- mysql - 在 WHERE 中使用 SUM(column = "xxx") 和 COUNT() 之间的区别
- javascript - js数组使用解构/休息语法按值删除元素
- reactjs - 无法从“node_modules\expo\AppEntry.js”解析“../../App”
- python - 'scrapy: command not found' 即使要求已经满足
- reactjs - 什么是浏览代码库和查找网页源代码的好方法
- lua - 使用“tonumber”后如何将数字再次转换为十六进制?
- mysql - 如何编写通用 SQL 查询以 (YYYY-MM-DD) 形式提取与所有 RDBMS(Oracle、SQL 服务器、MySql、DB2 等等)兼容的日期
- react-native - 最初无法在抽屉导航器中传递道具