In [1]:
#Adapted from https://pythonprogramming.net/flat-clustering-machine-learning-python-scikit-learn/
from sklearn.cluster import KMeans
import numpy as np

In [2]:
data = np.array([[1, 2],
              [5, 8],
              [1.5, 1.8],
              [8, 8],
              [1, 0.6],
              [9, 11]])

In [3]:
kmeans = KMeans(n_clusters=2)
kmeans.fit(data)

centroids = kmeans.cluster_centers_
labels = kmeans.labels_

print(centroids)
print(labels)


[[ 1.16666667  1.46666667]
 [ 7.33333333  9.        ]]
[0 1 0 1 0 1]

Visualizing the Clusters


In [4]:
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
%matplotlib inline

In [7]:
colors = ["g.","r.","c.","y."]

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


plt.scatter(centroids[:, 0],centroids[:, 1], marker = "x", s=150, linewidths = 5, zorder = 10)

plt.show()


coordinate: [ 1.  2.] label: 0
coordinate: [ 5.  8.] label: 1
coordinate: [ 1.5  1.8] label: 0
coordinate: [ 8.  8.] label: 1
coordinate: [ 1.   0.6] label: 0
coordinate: [  9.  11.] label: 1

In [ ]: