You are on page 1of 15

K-Means

The Clustering Algorithm


Tutorial

Data Mining
Dr. Ismael A. Ali | ismaelali.net
UoZ
K-means Clustering
● It automatically groups the data into clusters
● We need to determine the number of clusters (k)
● Each cluster has a centroid (center point)
● Each point is assigned to the cluster with the closest
centroid
K-means Clustering

Stop
K-means Clustering

k=3
K-means Clustering

k=3
Goal: Cluster the Iris flowers in the garden into 3
categories/clusters. After clustering we will be
having 3 clusters/groups of flowers with similar
Petal Septal properties (length/width)

A garden of Iris flowers needed to be


clustered into 3 different groups, based
on their Length and Width properties of
Septal and Petal of each flower.
Example of K-means Clustering
1- select k=3 random centers
2- assign each point to its closest Example of K-means Clustering
center Distance measure between any two
points in the chart, such as the p1
Y p4 (x4, y4)
and p2 , as each point has two values
for x-axis and y-axis:

p1 (x1, y1) = (-0.75, 0.70)


p3 (x3, y3) p2(x2, y2) = (-0.6, 1.8)
y3 p3(x3, y3) = (0.3, 2.1)

y2
p2 (x2, y2) Distance(p1,p2) = | x2 - x1 | + | y2 - y1 |
= | -0.6 - -0.75| + |1.8 - 0.70 |

? = 1.15 + 1.1 = 0.05


?

y1
p1 (x1,y1)

x2
X
x1
x3
Example of K-means Clustering
Example of K-means Clustering
1- select k=3 random centers
2- assign each point to its
closest center
Example of K-means Clustering
Example of K-means Clustering
1- select k=3 random centers
2- assign each point to its
closest center
Example of K-means Clustering
Example of K-means Clustering
3- move centers/centroids to the middle of their groups
Example of K-means Clustering
Example of K-means Clustering
Example of K-means Clustering
Example of K-means Clustering
Example of K-means Clustering
Example of K-means Clustering
Example of K-means Clustering
Example of K-means Clustering
on next week ...
● We will be practicing data clustering in Python
● Looking at real-world datasets
● Explain the Data Clustering Assignment
○ Report template
○ List of datasets to work on

You might also like