In [16]:
import mglearn
import matplotlib.pyplot as plt
mglearn.plots.plot_kmeans_algorithm()
plt.show()
mglearn.plots.plot_kmeans_boundaries()
plt.show()
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
#generate synthetic 2D data
X,y=make_blobs(random_state=1)
#build clustering model
kmeans=KMeans(n_clusters=3)
kmeans.fit(X)
print (kmeans.predict(X))
plt.scatter(X[:,0], X[:,1], c=kmeans.labels_, cmap=mglearn.cm3, s=60)
plt.scatter(kmeans.cluster_centers_[:, 0], kmeans.cluster_centers_[:, 1],
marker='^', s=100, linewidth=2, c=[0, 1, 2], cmap=mglearn.cm3)
plt.show()
fig, axes=plt.subplots(1,2)
#using two clusters
kmean=KMeans(n_clusters=2)
kmeans.fit(X)
assignments=kmeans.labels_
axes[0].scatter(X[:,0], X[:,1], c=assignments, cmap='jet', s=60)
plt.show()
#using 5 clusters
kmean=KMeans(n_clusters=5)
kmeans.fit(X)
assignments=kmeans.labels_
axes[1].scatter(X[:,0], X[:,1], c=assignments, cmap='jet', s=60)
plt.show()
In [ ]: