1

我使用 python 形成了以下集群,我想标记集群内的点。我不知道该怎么做。

Python 集群

import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

a = [262,562,733,335,544,259,682,423,769,444]

b = [19516842,16927322,14067158,12541731,10993709,10851871,10557379,10200356,10198000,9470625]

P = [list(item) for item in zip(a,b)]

kmeans = KMeans(n_clusters=2)
kmeans.fit(P)

labels = kmeans.labels_

colors = ["g.","r."]

for i in range(len(P)):
    print("coordinate:",P[i], "label:", labels[i])
    plt.plot(P[i][0], P[i][1], colors[labels[i]], markersize = 10)

plt.show()
4

1 回答 1

0

这是我的做法:

    import matplotlib.pyplot as plt
    from sklearn.cluster import KMeans

    a = [262,562,733,335,544,259,682,423,769,444]

    b = [19516842,16927322,14067158,12541731,10993709,10851871,10557379,10200356,10198000,9470625]

    P = [list(item) for item in zip(a,b)]

    kmeans = KMeans(n_clusters=2)
    kmeans.fit(P)

    labels = kmeans.labels_

    colors = ["g.","r."]

    for i in range(len(P)):
        print("coordinate:",P[i], "label:", labels[i])
        plt.plot(P[i][0], P[i][1], colors[labels[i]], markersize = 10)

    for label, x, y in zip(labels, a,b):
        plt.annotate(label, xy = (x, y), xytext = (-5, 5),textcoords = 'offset points', ha = 'right', va = 'bottom')
        #Fancy
        #plt.annotate(label, xy = (x, y), xytext = (-5, 5),textcoords = 'offset points', ha = 'right', va = 'bottom',bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))


    plt.show()
于 2015-11-29T17:41:32.450 回答