Pertama kali diperkenalkan oleh Huang pada tahun 1997. K-Modes merupakan salah satu teknik dalam analisis klaster yang merupakan perluasan dari algoritma k-means untuk mengelompokkan data katagorik. Algoritma k-modes merupakan pengembangan dari algoritmak-means.
Untuk menjalankan algortima K-Modes dibutuhkan library MASS dan library KlaR
library(MASS)
library(klaR)Package readxl menyiapkan fungsi read_excel() untuk import data dari file excel. Pada kasus ini digunakan data contac-lenses.
library(readxl)
data <- read_excel("D:/akhlakul/kuliah/semester 3/DATA MIINING/dataset/contact-lenses.xls")data## # A tibble: 24 × 5
## age `spectacle-prescrip` astigmatism `tear-prod-rate` contact-le…¹
## <chr> <chr> <chr> <chr> <chr>
## 1 young myope no reduced none
## 2 young myope no normal soft
## 3 young myope yes reduced none
## 4 young myope yes normal hard
## 5 young hypermetrope no reduced none
## 6 young hypermetrope no normal soft
## 7 young hypermetrope yes reduced none
## 8 young hypermetrope yes normal hard
## 9 pre-presbyopic myope no reduced none
## 10 pre-presbyopic myope no normal soft
## # … with 14 more rows, and abbreviated variable name ¹`contact-lenses`
Data di atas pada klom ke-5 adalah label,karena dalam Algoritma K-Modes tidak memerlukan label, maka kolom ke-5 dalam data tersebut dihilangkan terlebih dahulu
data_contact_lenses = data [-5]data_contact_lenses## # A tibble: 24 × 4
## age `spectacle-prescrip` astigmatism `tear-prod-rate`
## <chr> <chr> <chr> <chr>
## 1 young myope no reduced
## 2 young myope no normal
## 3 young myope yes reduced
## 4 young myope yes normal
## 5 young hypermetrope no reduced
## 6 young hypermetrope no normal
## 7 young hypermetrope yes reduced
## 8 young hypermetrope yes normal
## 9 pre-presbyopic myope no reduced
## 10 pre-presbyopic myope no normal
## # … with 14 more rows
Memeriksa Missing Value
colSums(is.na(data))## age spectacle-prescrip astigmatism tear-prod-rate
## 0 0 0 0
## contact-lenses
## 0
Tidak ada data yang kosong untuk setiap variabel Setelah data berhasil diimport, jalankan algoritma K-Modes.
cluster.results <- kmodes(data, 3, iter.max = 24, weighted = FALSE)
cluster.output <- cbind(data, cluster.results$cluster)Simpan hasil cluster ouput algoritma K-Modes dalam bentuk CSV
View(cluster.output)