如何在不同的K-means集群周围绘制边界

时间:2018-02-05 10:48:36

标签: python matplotlib k-means pca

import pandas as pd

import matplotlib.pyplot as plt

from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn import metrics
import numpy as np
%matplotlib inline
from scipy.spatial import ConvexHull
X =np.random.rand(10,10)

X_Name = ['a','b','c','d','e','f','g','h','i','j']
k = 3 
kmeans = KMeans(n_clusters=k)
clusters = kmeans.fit(X)
labels = clusters.labels_
centroids = clusters.cluster_centers_
pca = PCA(n_components=2, svd_solver='full')
X_ = pca.fit(X).transform(X)
colors = ['navy', 'turquoise', 'darkorange']
target_names = ['cluster1','cluster2','cluster3']
lw = 2
for color, i, target_name in zip(colors, [0, 1, 2], target_names):
    plt.scatter(X_[labels == i, 0], X_[labels == i, 1], color=color, 
    label=target_name)
for i, txt in enumerate(X_Name):
    plt.annotate(txt, (X_[i,0],X_[i,1]))
plt.legend(loc='best', shadow=False, scatterpoints=1)

I am getting image like this

But i want a image in this way

此代码生成由K-means生成的不同群集的散点图。但我想围绕集群生成边界,以便可以轻松区分。

0 个答案:

没有答案