K-Means clustering algorithm tutorial from Sentdex. 25 SEP 2017


In [1]:
import matplotlib.pyplot as plt
from matplotlib import style
style.use('ggplot')
import numpy as np
from sklearn.cluster import KMeans

In [22]:
# vector of 10 random coordinates
X = np.random.random((100,2))
# scatter plot; x:X[:,0], y:X[:,1]
# plt.scatter(X[:,0], X[:,1], s=150, linewidths=5, color='b')
# # display plot
# plt.show()

In [23]:
# initialize classifier
clf = KMeans(n_clusters=2)
clf.fit(X)

centroids = clf.cluster_centers_
labels = clf.labels_

colors = ['r.','b.','g.','c.','m.']

for i in range(len(X)):
    plt.plot(X[i][0], X[i][1], colors[labels[i]], markersize=10)
plt.scatter(centroids[:,0], centroids[:,1], marker='x', s=150, linewidths=5, c='k')
plt.show()



In [ ]: