gpt4 book ai didi

python - 在 python 中绘制 sklearn 集群

转载 作者:行者123 更新时间:2023-11-30 08:49:11 25 4
gpt4 key购买 nike

我使用亲和性传播获得了以下 sklearn 集群。

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 中绘制集群。是否可以使用 pandas 数据框来做到这一点?

编辑:

我使用了code in sklearn直接按照@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()

但是,我得到的输出有点奇怪,如下所示(第二个聚类点(绿色)位于蓝线上。因此,我认为它不应该聚类为一个单独的点,也应该位于蓝色中簇)。如果我在代码中犯了任何错误,请告诉我。 enter image description here

编辑2

正如 σηγ 所指出的,我补充道:

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 个数组都代表一个点。

enter image description here

请帮助我。

最佳答案

按照前面的例子,我会尝试这样的事情:

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()

AP_SE

关于python - 在 python 中绘制 sklearn 集群,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46232216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com