将聚类结果绘制并可视化为网络图



我正在Python中尝试各种聚类算法和字符串距离度量,最终目标是根据各种距离度量(如Levenstein、Jaro等(对字符串列表进行聚类(每个字符串通常有1或2个单词(。

我已经构建了根据不同距离度量计算字符串之间距离的代码(使用水母包(,并使用sklearn.cluster包提供的不同算法对它们进行聚类。以下是Jaro距离和MeanShift聚类的一些示例代码:

tokens = np.array(["test1", "test2", "test3", "cat", "cat food", "apple", "apple pie"])
distances = -1 * np.array([[jellyfish.jaro_distance(w1, w2) for w1 in tokens] for w2 in tokens])
meanshift = sklearn.cluster.MeanShift()
meanshift.fit(distances)
clusters = dict()
key = 0
for cluster_id in np.unique(meanshift.labels_):
cluster = np.unique(tokens[np.nonzero(meanshift.labels_ == cluster_id)])
clusters[key] = cluster.tolist()
key += 1
plot_clusters(clusters, ...)

现在,我想把聚类的结果绘制/可视化/保存为一个类似于这个[1]的网络图。我会很高兴有一个简单的可视化,可以很容易地看到(和计数(不同的集群。这就是为什么我只构建了一个带有集群元素的字典。然而,如果可视化能考虑到预先计算的数据点之间的距离,那就太好了。无论哪种方式对我来说都很好。我只想在分析实际集群的同时,得到一些不错的可视化效果。

有人对如何解决这个问题有什么想法或建议吗?任何帮助都将不胜感激!

谢谢!

[1]https://www.kdnuggets.com/wp-content/uploads/k-means-datasci.jpg

免责声明:我是python和机器学习的新手

它还没有显示距离,但你可以做一些彩色散点图,比如

import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
plt.figure()
clustercount = len(clusters)
color=iter(cm.rainbow(np.linspace(0,1,clustercount)))
for cl in clusters:    
c=next(color)
x = # x data of your cluster here
y = # y data of your cluster here
label = # label of your cluster here
plt.scatter(x, y, color=c, label=label)
plt.xlabel('X');
plt.ylabel('Y');
plt.legend(loc=2);
plt.show()

这将可视化不同颜色的集群,这样你就可以很容易地看到和计数

也许你可以通过meanshift.cluster_centers_访问集群中心。如果是这样,你也可以用静态颜色绘制它们,以可视化距离。

最新更新