Lab 12: K-Means Clustering

CILO: 3
Weeks: 42–44
Lab #12

Aim

Cluster unlabeled data into k groups using Lloyd’s algorithm (k-means).

Objectives

Algorithm / Procedure

  1. Define 2D dataset with two obvious clusters.
  2. Run KMeans(n_clusters=2).
  3. Print labels and cluster centers.

Python Code

import numpy as np
from sklearn.cluster import KMeans

X = np.array([[1,2],[1,4],[1,0],
              [10,2],[10,4],[10,0]])

kmeans = KMeans(n_clusters=2, n_init=10, random_state=0).fit(X)
print("Cluster Centers:\n", kmeans.cluster_centers_)
print("Labels:", kmeans.labels_.tolist())

Sample Output (expected)

Cluster Centers:
 [[10.  2.]
  [ 1.  2.]]
Labels: [1, 1, 1, 0, 0, 0]