K Means Clustering
K Means Clustering
What is Clustering?
Also called unsupervised learning, sometimes called classification by statisticians and sorting by psychologists and segmentation by people in marketing
Clustering is subjective
Simpson's Family
School Employees
Females
Males
Hierarchical
Partitional
What is Similarity?
The quality or state of being similar; likeness; resemblance; as, a similarity of features.
Webster's Dictionary
Distance : Adalah ukuran kesamaan antar objek yang dihitung berdasarkan rumusan tertentu
0 8 0 8 2 0 7 4 3 0 7 4 3 1 0
D( , ) = 8 D( , ) = 1
Partitional Clustering
Nonhierarchical, setiap objek ditempatkan di salah satu cluster Nonoverlapping cluster Jumlah kluster yang akan dibentuk ditentukan sejak awal
Algorithm k-means
1. Tentukan berapa cluster k yang mau dibuat. 2. Inisialisasi centroid dari tiap cluster (randomly, if necessary). 3. Tentukan keanggotaan objek-objek yang lain dengan mengklasifikasikannya sesuai centroid terdekat (berdasarkan distance ke centroid) 4. Setelah cluster dan anggotanya terbentuk, hitung mean tiap cluster dan jadikan sebagai centroid baru 5. Jika centroid baru tidak sama dengan centroid lama, maka perlu diupdate lagi keanggotaan objek-objeknya(balik ke -3). Sebaliknya jika centroid baru sama dengan yang lama maka selesai.
k1
3
k2
k3
0 0 1 2 3 4 5
k1
3
k2
k3
0 0 1 2 3 4 5
k1
k3
1
k2
0 0 1 2 3 4 5
Jika centroid baru tidak sama dengan centroid lama, maka perlu diupdate lagi keanggotaan objek-objeknya
5
k1
k3
1
k2
0 0 1 2 3 4 5
k1
k2
k3
Weakness
Applicable only when mean is defined, then what about categorical data? Need to specify k, the number of clusters, in advance Unable to handle noisy data and outliers
MATLAB
[IDX,C] = kmeans(X,k) returns the k cluster centroid locations in the k-by-p matrix C
you to specify optional parameter name-value pairs to control the iterative algorithm used by kmeans. The parameters are :
distance start replicates maxiter emptyaction display
'distance
Distance measure, in p-dimensional space, that kmeans minimizes with respect to. kmeans computes centroid clusters differently for the different supported distance measures:
'start'
Method used to choose the initial cluster centroid positions, sometimes known as "seeds". Valid starting values are:
'replicates'
Number of times to repeat the clustering, each with a new set of initial cluster centroid positions. kmeans returns the solution with the lowest value for sumd. You can supply 'replicates' implicitly by supplying a 3-dimensional array as the value for the 'start' parameter.
'maxiter'
Maximum number of iterations. Default is 100.
'emptyaction'
Action to take if a cluster loses all its member observations. Can be one of:
'display'
Controls display of output. 'off : Display no output. 'iter : Display information about each iteration during minimization, including the iteration number, the optimization phase, the number of points moved, and the total sum of distances. 'final : Display a summary of each replication. 'notify : Display only warning and error messages. (default)
Example
dataku =[ 7 26 6 60; 11 56 8 20; ... 11 31 8 47; ... 7 52 6 33; ... 11 55 9 22; ... 3 71 17 6; ... 1 31 22 44; ... 2 54 18 22; ... 21 47 4 26; ... 1 40 23 34; ... 11 66 9 12; ... 10 68 8 12] 1 29 15 52; ...
Result
hasilk = 1 1 2 1 2 2 2 3 2 2 3 2 2