绘制亲和力聚类算法 - Sklearn

时间:2017-01-30 22:43:42

标签: python machine-learning scikit-learn data-visualization

我编写了以下代码来聚合一些数据:

X = numpy.concatenate((title, abstract), axis=1)
X, y = sklearn.utils.shuffle(X, y, random_state=0)
af = AffinityPropagation().fit(X)
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_
n_clusters_= len(cluster_centers_indices)

我现在希望可视化这些数据,并基于this,我写了这个:

plt.close('all')
plt.figure(1)
plt.clf()
colors = cycle('bgrcmykbgrcmykbgrcmykbgrcmyk')
for k, col in zip(range(n_clusters_), colors):
    class_members = y == 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()

然而,当我运行它时,我收到此错误:

Traceback (most recent call last):
  File "/Users/...", line 83, in <module>
    plt.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col,
  File "/Users/.../anaconda/lib/python3.5/site-packages/numpy/matrixlib/defmatrix.py", line 318, in __getitem__
    out = N.ndarray.__getitem__(self, index)
IndexError: index 1 is out of bounds for axis 0 with size 1

0 个答案:

没有答案