问题描述
我有以下通过亲和力传播获得的sklearn簇.
I have the following sklearn clusters obtained using affinity propagation.
import sklearn.cluster
import numpy as np
sims = np.array([[0, 17, 10, 32, 32], [18, 0, 6, 20, 15], [10, 8, 0, 20, 21], [30, 16, 20, 0, 17], [30, 15, 21, 17, 0]])
affprop = sklearn.cluster.AffinityPropagation(affinity="precomputed", damping=0.5)
affprop.fit(sims)
cluster_centers_indices = affprop.cluster_centers_indices_
labels = affprop.labels_
#number of clusters
n_clusters_ = len(cluster_centers_indices)
现在,我想绘制群集的输出.我是sklearn的新手.请给我建议一个合适的方法来在python中绘制聚类.可以用熊猫数据框来做到这一点吗?
Now I want to plot the output of the clusters. I am new to sklearn. Please suggest me a suitable approach to plot the clusters in python. Is it possible to do this with pandas dataframes?
我使用了直接在sklearn中编码,如下所示,如@MohammedKashif所指出的那样.
I used the code in sklearn directly as follows as pointed by @MohammedKashif.
import sklearn.cluster
import numpy as np
sims = np.array([[0, 17, 10, 32, 32], [18, 0, 6, 20, 15], [10, 8, 0, 20, 21], [30, 16, 20, 0, 17], [30, 15, 21, 17, 0]])
affprop = sklearn.cluster.AffinityPropagation(affinity="precomputed", damping=0.5)
affprop.fit(sims)
cluster_centers_indices = affprop.cluster_centers_indices_
print(cluster_centers_indices)
labels = affprop.labels_
n_clusters_ = len(cluster_centers_indices)
print(n_clusters_)
import matplotlib.pyplot as plt
from itertools import cycle
plt.close('all')
plt.figure(1)
plt.clf()
colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
for k, col in zip(range(n_clusters_), colors):
class_members = labels == k
cluster_center = sims[cluster_centers_indices[k]]
plt.plot(sims[class_members, 0], sims[class_members, 1], col + '.')
plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
for x in sims[class_members]:
plt.plot([cluster_center[0], x[0]], [cluster_center[1], x[1]], col)
plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.show()
但是,我得到的输出有点奇怪(第二个聚类点(绿色)在蓝线上.因此,我不认为它应该作为单独的聚类,也不应在蓝色中)簇).如果我在代码中有任何错误,请告诉我.
However, the output I get is bit weird as follows (The second cluster point (green) is on the blue line. Hence, I don't think it should be clustered as a separate one and should also be in the blue cluster). Please let me know if I have made any mistakes in the code.
编辑2
正如σηγ所指出的,我添加了:
As pointed by σηγ I added:
se = SpectralEmbedding(n_components=2, affinity='precomputed')
X = se.fit_transform(sims)
print(X)
但是,对于数组np.array([[0, 17, 10, 32, 32], [0, 17, 10, 32, 32], [0, 17, 10, 32, 33], [0, 17, 10, 32, 32], [0, 17, 10, 32, 32]])
,它给了我3分,如下所示.这让我感到困惑,因为所有5个数组都代表一个点.
However, for the array np.array([[0, 17, 10, 32, 32], [0, 17, 10, 32, 32], [0, 17, 10, 32, 33], [0, 17, 10, 32, 32], [0, 17, 10, 32, 32]])
it gave me 3 points as shown below. That confuses me because all the 5 arrays represents one point.
请帮助我.
推荐答案
在上一个示例之后,我将尝试以下操作:
Following the previous example, I would try something like this:
import sklearn.cluster
from sklearn.manifold import SpectralEmbedding
import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle
sims = np.array([[0, 17, 10, 32, 32], [18, 0, 6, 20, 15], [10, 8, 0, 20, 21], [30, 16, 20, 0, 17], [30, 15, 21, 17, 0]])
affprop = sklearn.cluster.AffinityPropagation(affinity="precomputed", damping=0.5)
affprop.fit(sims)
cluster_centers_indices = affprop.cluster_centers_indices_
print(cluster_centers_indices)
labels = affprop.labels_
n_clusters_ = len(cluster_centers_indices)
print(n_clusters_)
se = SpectralEmbedding(n_components=2, affinity='precomputed')
X = se.fit_transform(sims)
plt.close('all')
plt.figure(1)
plt.clf()
colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
for k, col in zip(range(n_clusters_), colors):
class_members = labels == k
cluster_center = X[cluster_centers_indices[k]]
plt.plot(X[class_members, 0], X[class_members, 1], col + '.')
plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
for x in X[class_members]:
plt.plot([cluster_center[0], x[0]], [cluster_center[1], x[1]], col)
plt.title('Estimated number of clusters: %d' % n_clusters_)
plt.show()
这篇关于在python中绘制sklearn集群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!