Anda di halaman 1dari 23

Clustering Dokumen (K-Means)

Table Of Contents
1. Konsep Dasar Clustering
2. Tahapan Clustering
3. K-Means Clustering
 Algoritma K-Means
 Rumus Umum K-Means
4. Case Study
Konsep Dasar
• Klusterisasi Data, atau Data Clustering juga
disebut sebagai analisis klaster, analisis
segmentasi, analisis taxonomi, atau
unsupervised classification.
• Metode yang digunakan untuk membangun
grup dari objek-objek, atau klaster-klaster,
dimana objek-objek dalam satu kluster tertentu
memiliki kesamaan ciri yang tinggi dan objek-
objek pada kluster yang berbeda memiliki
kesamaan ciri yang rendah.
Konsep Dasar
• Tujuan dari klasterisasi data adalah
mengelompokkan data yang memiliki
kesamaan ciri dan memisahkan data ke dalam
klaster yang berbeda untuk objek-objek yang
memiliki ciri yang berbeda.
• Berbeda dengan klasifikasi, yang memiliki
kelas yang telah didefinisikan sebelumnya.
Dalam klasterisasi, klaster akan terbentuk
sendiri berdasarkan ciri objek yang dimiliki dan
kriteria pengelompokan yang telah ditentukan.
Tahapan Klasterisasi
1. Feature Selection
– Penentuan informasi fitur yang digunakan.
2. Proximity Measure
– Tahap kuantifikasi item kemiripan data.
3. Clustering Criterion
– Penentuan fungsi pembobotan / tipe aturan.
4. Clustering Algorithm
– Metode klaster berdasarkan ukuran kemiripan data dan kriteria
klasterisasi.
5. Validation of the Result
6. Interpretation of the Result
Proximity Measure
• Koefisien kemiripan menunjukkan kekuatan
hubungan antara dua data.
• Semakin banyak kemiripan titik data satu sama
lain, maka semakin besar koefisien kesamaan.
• Misalkan x = (x1, x2, ..., xd) dan y = (y1, y2, ..., yd)
adalah dua titik data pada d dimensi. Maka nilai
koefisien kemiripan antara x dan y adalah
beberapa nilai atribut fungsi s(x,y) = s(x1, x2, ..., xd,
y1, y2, ..., yd).
Proximity Measure
• Pemilihan jarak pada clustering adalah sangat penting, dan pilihan yang
terbaik sering diperoleh melalui pengalaman, kemampuan, pengetahuan.
• Pengukuran Jarak Data :
– Numerik dengan banyak fitur atau dimensi (d) :
- Euclidean Distance : - Minkowski Distance :
1 1
 2  r
d euclid ( x, y )   x j  y j  
d 2 d r
d minkow ( x, y )   x j  y j  , r 1
 j 1   j 1 
- Manhattan Distance : - Mahalanobis Distance :
d
d man ( x, y)   x j  y j d mah ( x, y )  x  y  1 x  y T
j 1

- Maximum Distance : - Average Distance : 1


1 2
d ave ( x, y )    x j  y j  
d 2
d max ( x, y )  max x j  y j
j 1.. d

– Kategorikal :  d j 1 
- Simple Matching Distance

d sim ( x, y )    x j , y j 
d
0 if x  y
 ( x, y )  
 1 if x  y j 1
Proximity Measure
• Jika x1=(1,2) dan x2=(2,3). Hitunglah Jarak x1 dan x2 dengan Euclidean!
1
 d
 2
    
1 1
 22 
2

  1  2   2  3  1  1
1
d euclid ( x1 , x2 )   x1 j  x2 j 2  1.4
2 2 2 2 2 2

 j 1 
1 2
3 5 5
 4 x2  
1
• Jika x1  Hitunglah Jarak x1 dan x2 dengan Mahalanobis!
7
2 3

(Note : Mahalanobis biasanya digunakan untuk menghitung jarak antar cluster)

– Hitung Mean Corected Matrix x 0


i 1
1  3  2 2  4  3 
x  
 3 3 

 2 3
 5  7 5  1
 x2  
 2 2   6 3
1  2 2  3  1  1 5  6 5  3  1 2 
x10  
3  2 4  3  1 1 x20   
1  3   1  2
   7  6

2  2 3  3
  0 0 

– Hitung Matrik Covarian (Ci)


 1  1
1 0T 0 1  1 1 0    1 2 2  0.7 0.7
C1  x1 x1  
0  3 2 2 0.7 0.7
 1 1
n1 3  1 1  0    
 0 
1 0T 0 1  1 1   1 2  1 2  4  1  2
C2  x2 x2         
n2 2 2  2  1  2 2  4 8   2 4 

Proximity Measure
1 2
5 5
• Jika x1   4 x2   Hitunglah Jarak x1 dan x2 dengan mahalanobis!
3  7 1

2 3

– Hitung Matrik Covarian (Ci)
 1  1
1 0T 0 1  1 1 0    1 2 2  0.7 0.7
C1  x1 x1    1 1  3 2 2 0.7 0.7
n1 3  1 1 0     

0 0 
1 0T 0 1  1 1   1 2  1 2  4  1  2
C2  x2 x2         
n2 2 2  2  1  2 2  4 8   2 4 

1
n g ro u p
1 2
1 2 3 0.7 0.7  2  1  2
 n
  n C
i i 
n1  n2
 n C
i i   i i 5 0.7
n C  
0.7  5  2 4 
i 1 i 1 5 i 1 
0.4 0.4  0.4  0.8  0.8  0.4
    
0.4 0.4  0.8 1.6   0.4 2 
 2 0.4 1  2 0.4 1.4 0.3
Adj  
1 1
 1


 
0.8 * 2   0.4 * 0.4 0.4 0.8 1.44 0.4 0.8 0.3 0.6
Proximity Measure
1 2
5 5
• Jika x1   4 x2   Hitunglah Jarak x1 dan x2 dengan mahalanobis!
3  7 1

2 3

 2 0.4 1  2 0.4 1.4 0.3
Adj  
1 1
 1


 
0.8 * 2   0.4 * 0.4 0.4 0.8 1.44 0.4 0.8 0.3 0.6
– Hitung Mean Different (X1,X2) :

1  3  2 2  4  3   5  7 5  1
x    2 3  x2     6 3
1
 3 3    2 2 
x1, x2   2  6 3  3   4 0

d mah ( x1 , x2 )  x1  x2  1 x1  x2 T


1.4 0.3  4
d mah ( x1 , x2 )    x1 , x2  1  x1 , x2  
T
 4 0
0.3 0.6  0 

  4
  5.6  1.1   22.2  4.7
 0 
Clustering Algorithm
Clustering Algorithm

Sequential Hierarchical

Hard Clustering Agglomerative (bottom-up)

Fuzzy Clustering Divisive (top-down)

Possibilistic Clustering Combinations


Klasterisasi K-Means
• K-Means merupakan salah satu metode pengelompokan data non hirarki
yang digunakan untuk mempartisi N objek data ke dalam K kelompok.
• Algoritma K-Means (MacQueen, 1967) :
1. Tentukan jumlah cluster, lalu alokasikan data ke dalam cluster secara acak.
2. Hitung pusat cluster (C) dari data yang ada di masing-masing cluster.

1 m Note : m menyatakan jumlah data pada


Ci   x j suatu kelompok, dan i menyatakan fitur
m j 1 ke-i dalam sebuah kelompok.
3. Hitung jarak (D) semua data terhadap masing-masing cluster, lalu alokasikan
masing-masing data ke centroid terdekat (a).
1
 2 1 , d  min( D( x j , Ci ))
Deuclid ( x, y )   x j  y j  
nd im 2
a ji  
 j 1  0 , lainnya
4. Kembali ke langkah 3, jika masih ada data yang berpindah cluster atau ada
perubahan nilai centroid, misal dengan batasan iterasi max atau dengan
objective function.
ndata k k ndata 2 k ndata
 j  j
F   a ji D( x j , Ci ) J   xi Cj   D( xi , C j )
j 1 i 1 atau j 1 i 1 j 1 i 1
Contoh Studi Kasus
• Perhatikan dataset berikut :
Data Fitur x Fitur y Kelompok 1 Kelompok 2 Kelompok 3 Inisialisasi :
1 1 1 *
2 4 1 * K = 3,
3 6 1 * IterasiMax = 2,
4 1 2 *
5 2 3 *
Fungsi Objektif (FInit) = 0,
6 5 3 * Threshold (T) = 0.8.
7 2 5 *
8 3 5 *
Tentukan Hasil Akhir
9 2 6 * Clusteringnya !
10 3 8 *

• Bentuk Visualisasi data :


Contoh Studi Kasus (Cont.)
• Menghitung Centroid Setiap Cluster :
Data Fx Fy K1 K2 K3 K1Fx K1Fy K2Fx K2Fy K3Fx K3Fy
1 1 1 * 1 1
2 4 1 * 4 1
3 6 1 * 6 1
4 1 2 * 1 2
5 2 3 * 2 3
6 5 3 * 5 3
7 2 5 * 2 5
8 3 5 * 3 5
9 2 6 * 2 6
10 3 8 * 3 8
Total 2 5 3 2 3 21 18 6 14

• Hasil Centroid Setiap Cluster :


Kelompok Centroid Fitur x Centroid Fitur y
1 Total K1Fx / Total K1 = 2 / 2 = 1 Total K1Fy / Total K1 = 3 / 2 = 1.5
2 Total K2Fx / Total K2 = 21 / 5 = 4.2 Total K2Fy / Total K2 = 18 / 5 = 3.6
3 Total K3Fx / Total K3 = 6 / 3 = 2 Total K3Fy / Total K3 = 14 / 3 = 4.6667
Contoh Studi Kasus (Cont.)
• Menghitung Jarak Data Ke Centroid (Euclidian Distance) :
Dx  1,1, C1  1,1.5  1 12  1 1.52  02   0.52  0.25  0.5

Dx  1,1, C2  4.2,3.6  1  4.22  1  3.62   3.22   2.62  10.24  6.76  17  4.1231

Dx  1,1, C3  2,4.6667  1  22  1  4.66672  12   3.66672  3.8006

Kelompok Kelompok
Data Fx Fy Jarak Ke C 1 Jarak Ke C 2 Jarak Ke C 3 Min
Baru Sebelumnya
1 1 1 0.5000 4.1231 3.8006 0.5000 1 1
2 4 1 3.0414 2.6077 4.1767 2.6077 2 2
3 6 1 5.0249 3.1623 5.4263 3.1623 2 2
4 1 2 0.5000 3.5777 2.8480 0.5000 1 1
5 2 3 1.8028 2.2804 1.6667 1.6667 3 3
6 5 3 4.2720 1.0000 3.4319 1.0000 2 2
7 2 5 3.6401 2.6077 0.3333 0.3333 3 3
8 3 5 4.0311 1.8439 1.0541 1.0541 3 2
9 2 6 4.6098 3.2558 1.3333 1.3333 3 3
10 3 8 6.8007 4.5607 3.4801 3.4801 3 2
Total 1.0000 13.1746 3.3333 (Total berdasarkan kelompok sebelumnya)

Sehingga, F baru = 1.0000 + 13.1746 + 3.3333 = 17.5079


Delta = | F baru – F lama | = | 17.5079 – 0 | = 17.5079 ( > T) , Lanjutkan !
Contoh Studi Kasus (Cont.)
• Iterasi 1 : (Mengalokasikan Setiap Data Pada Centroid Terdekat)
Data Fx Fy K1 K2 K3 Jarak Ke C 1 Jarak Ke C 2 Jarak Ke C 3 Min Kelompok Baru
1 1 1 * 0.5000 4.1231 3.8006 0.5000 1
2 4 1 * 3.0414 2.6077 4.1767 2.6077 2
3 6 1 * 5.0249 3.1623 5.4263 3.1623 2
4 1 2 * 0.5000 3.5777 2.8480 0.5000 1
5 2 3 * 1.8028 2.2804 1.6667 1.6667 3
6 5 3 * 4.2720 1.0000 3.4319 1.0000 2
7 2 5 * 3.6401 2.6077 0.3333 0.3333 3
8 3 5 * 4.0311 1.8439 1.0541 1.0541 3
9 2 6 * 4.6098 3.2558 1.3333 1.3333 3
10 3 8 * 6.8007 4.5607 3.4801 3.4801 3
Total 2 3 5 1.0000 13.1746 3.3333
Contoh Studi Kasus (Cont.)
• Menghitung Centroid Setiap Cluster :
Data Fx Fy K1 K2 K3 K1Fx K1Fy K2Fx K2Fy K3Fx K3Fy
1 1 1 * 1 1
2 4 1 * 4 1
3 6 1 * 6 1
4 1 2 * 1 2
5 2 3 * 2 3
6 5 3 * 5 3
7 2 5 * 2 5
8 3 5 * 3 5
9 2 6 * 2 6
10 3 8 * 3 8
Total 2 3 5 2 3 15 5 12 27

• Hasil Centroid Setiap Cluster :


Kelompok Centroid Fitur x Centroid Fitur y
1 Total K1Fx / Total K1 = 2 / 2 = 1 Total K1Fy / Total K1 = 3 / 2 = 1.5
2 Total K2Fx / Total K2 = 15 / 3 = 5 Total K2Fy / Total K2 = 5 / 3 = 1.6667
3 Total K3Fx / Total K3 = 12 / 5 = 2.4 Total K3Fy / Total K3 = 27 / 5 = 5.4
Contoh Studi Kasus (Cont.)
• Hasil Centroid Setiap Cluster :
Kelompok Centroid Fitur x Centroid Fitur y
1 Total K1Fx / Total K1 = 2 / 2 = 1 Total K1Fy / Total K1 = 3 / 2 = 1.5
2 Total K2Fx / Total K2 = 15 / 3 = 5 Total K2Fy / Total K2 = 5 / 3 = 1.6667
3 Total K3Fx / Total K3 = 12 / 5 = 2.4 Total K3Fy / Total K3 = 27 / 5 = 5.4

• Menghitung Jarak Data Ke Centroid :


Kelompok Kelompok
Data Fx Fy Jarak Ke C 1 Jarak Ke C 2 Jarak Ke C 3 Min
Baru Sebelumnya
1 1 1 0.5000 4.0552 4.6174 0.5000 1 1
2 4 1 3.0414 1.2019 4.6819 1.2019 2 2
3 6 1 5.0249 1.2019 5.6851 1.2019 2 2
4 1 2 0.5000 4.0139 3.6770 0.5000 1 1
5 2 3 1.8028 3.2830 2.4331 1.8028 1 3
6 5 3 4.2720 1.3333 3.5384 1.3333 2 2
7 2 5 3.6401 4.4845 0.5657 0.5657 3 3
8 3 5 4.0311 3.8873 0.7211 0.7211 3 3
9 2 6 4.6098 5.2705 0.7211 0.7211 3 3
10 3 8 6.8007 6.6416 2.6683 2.6683 3 3
Total 1.0000 3.7370 7.1093 (Total berdasarkan kelompok sebelumnya)

Sehingga, F baru = 1.0000 + 3.7370 + 7.1093 = 11.8464


Delta = | F baru – F lama | = | 11.8464 – 17.5079 | = 5.6615 ( > T) ,
Lanjutkan !
Contoh Studi Kasus (Cont.)
• Iterasi 2 : (Mengalokasikan Setiap Data Pada Centroid Terdekat)
Data Fx Fy K1 K2 K3 Jarak Ke C 1 Jarak Ke C 2 Jarak Ke C 3 Min Kelompok Baru
1 1 1 * 0.5000 4.0552 4.6174 0.5000 1
2 4 1 * 3.0414 1.2019 4.6819 1.2019 2
3 6 1 * 5.0249 1.2019 5.6851 1.2019 2
4 1 2 * 0.5000 4.0139 3.6770 0.5000 1
5 2 3 * 1.8028 3.2830 2.4331 1.8028 1
6 5 3 * 4.2720 1.3333 3.5384 1.3333 2
7 2 5 * 3.6401 4.4845 0.5657 0.5657 3
8 3 5 * 4.0311 3.8873 0.7211 0.7211 3
9 2 6 * 4.6098 5.2705 0.7211 0.7211 3
10 3 8 * 6.8007 6.6416 2.6683 2.6683 3
Total 2 3 5 1.0000 3.7370 7.1093
Contoh Studi Kasus (Cont.)
• Menghitung Centroid Setiap Cluster :
Data Fx Fy K1 K2 K3 K1Fx K1Fy K2Fx K2Fy K3Fx K3Fy
1 1 1 * 1 1
2 4 1 * 4 1
3 6 1 * 6 1
4 1 2 * 1 2
5 2 3 * 2 3
6 5 3 * 5 3
7 2 5 * 2 5
8 3 5 * 3 5
9 2 6 * 2 6
10 3 8 * 3 8
Total 3 3 4 4 6 15 5 10 24

• Hasil Centroid Setiap Cluster :


Kelompok Centroid Fitur x Centroid Fitur y
1 Total K1Fx / Total K1 = 4 / 3 = 1.3333 Total K1Fy / Total K1 = 6 / 3 = 2
2 Total K2Fx / Total K2 = 15 / 3 = 5 Total K2Fy / Total K2 = 5 / 3 = 1.6667
3 Total K3Fx / Total K3 = 10 / 4 = 2.5 Total K3Fy / Total K3 = 24 / 4 = 6
Contoh Studi Kasus (Cont.)
• Hasil Centroid Setiap Cluster :
Kelompok Centroid Fitur x Centroid Fitur y
1 Total K1Fx / Total K1 = 4 / 3 = 1.3333 Total K1Fy / Total K1 = 6 / 3 = 2
2 Total K2Fx / Total K2 = 15 / 3 = 5 Total K2Fy / Total K2 = 5 / 3 = 1.6667
3 Total K3Fx / Total K3 = 10 / 4 = 2.5 Total K3Fy / Total K3 = 24 / 4 = 6

• Menghitung Jarak Data Ke Centroid :


Kelompok Kelompok
Data Fx Fy Jarak Ke C 1 Jarak Ke C 2 Jarak Ke C 3 Min
Baru Sebelumnya
1 1 1 1.0541 4.0552 5.2202 1.0541 1 1
2 4 1 2.8480 1.2019 5.2202 1.2019 2 2
3 6 1 4.7726 1.2019 6.1033 1.2019 2 2
4 1 2 0.3333 4.0139 4.2720 0.3333 1 1
5 2 3 1.2019 3.2830 3.0414 1.2019 1 1
6 5 3 3.8006 1.3333 3.9051 1.3333 2 2
7 2 5 3.0732 4.4845 1.1180 1.1180 3 3
8 3 5 3.4319 3.8873 1.1180 1.1180 3 3
9 2 6 4.0552 5.2705 0.5000 0.5000 3 3
10 3 8 6.2272 6.6416 2.0616 2.0616 3 3
Total 2.5893 3.7370 4.7976 (Total berdasarkan kelompok sebelumnya)

 Cek : Iterasi = IterasiMax. Stop Iterasi !


 Atau Cek : F baru = 2.5893 + 3.7370 + 4.7976 = 11.1239
Delta = | F baru – F lama | = | 11.1239 – 11.8464 | = 0.7224 ( < T=0.8) , Stop Iterasi !
Jika kondisi stop, maka buat Tabel Hasil Akhir Clustering Data dan Visualisasinya.
Contoh Studi Kasus (Cont.)
• Hasil Akhir Clustering Data :
Data Fx Fy Kelompok Baru
1 1 1 1
2 4 1 2
3 6 1 2
4 1 2 1
5 2 3 1
6 5 3 2
7 2 5 3
8 3 5 3
9 2 6 3
10 3 8 3

• Visualisasi Hasil Akhir Clustering :


Selesai

Anda mungkin juga menyukai