1 Pendahuluan

1.1 Customer Segmentation

Segmentasi pasar merupakan pembagian kelompok konsumen yang memiliki kebutuhan, karateristik dan prilaku yang berbedabeda (heterogen) di dalam pasar tertentu sehingga menjadi satuan pasar yang homogen, dalam hal ini sangat membantu dalam proses pemasaran yang lebih terarah sehingga sumber daya perusahaan dapat digunakan secara efektif dan efisien contohnya memudahkan dalam membedakan pasar dan mengenal kompetitor dengan segmen yang sama.

1.2 Setup

Berikut beberapa packages yang digunakan pada artikel ini.

library(dplyr)
library(tidyverse)
library(corrplot)
library(gridExtra)
library(GGally)
library(FactoMineR)

# clustering libs
library(dbscan)
library(factoextra)
library(cluster)
# visualization libs
library(ggplot2)
library(ggthemes)

2 Catatan

Pada LBB ini kita akan buat beberapa model clustering, Kmeans,Kmedoids dan DBSCAN sumber data dari Kaggle

3 Load Data

customer <- read.csv("data/Mall_Customers.csv", stringsAsFactors = T)
glimpse(customer)
## Rows: 200
## Columns: 5
## $ CustomerID             <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, …
## $ Gender                 <fct> Male, Male, Female, Female, Female, Female, Fem…
## $ Age                    <int> 19, 21, 20, 23, 31, 22, 35, 23, 64, 30, 67, 35,…
## $ Annual.Income..k..     <int> 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, 19,…
## $ Spending.Score..1.100. <int> 39, 81, 6, 77, 40, 76, 6, 94, 3, 72, 14, 99, 15…
head(customer, 10)
str(customer)
## 'data.frame':    200 obs. of  5 variables:
##  $ CustomerID            : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ Gender                : Factor w/ 2 levels "Female","Male": 2 2 1 1 1 1 1 1 2 1 ...
##  $ Age                   : int  19 21 20 23 31 22 35 23 64 30 ...
##  $ Annual.Income..k..    : int  15 15 16 16 17 17 18 18 19 19 ...
##  $ Spending.Score..1.100.: int  39 81 6 77 40 76 6 94 3 72 ...

4 Preprocessing

4.1 mengecheck missing value

cat("berapa missing value = ", sum(is.na(customer)), "data")
## berapa missing value =  0 data

4.2 menghapus kolom yang tidak digunakan (untuk modelling)

customer_clean <- customer %>% 
  select(-c(CustomerID, Gender))
glimpse((customer_clean))
## Rows: 200
## Columns: 3
## $ Age                    <int> 19, 21, 20, 23, 31, 22, 35, 23, 64, 30, 67, 35,…
## $ Annual.Income..k..     <int> 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, 19,…
## $ Spending.Score..1.100. <int> 39, 81, 6, 77, 40, 76, 6, 94, 3, 72, 14, 99, 15…

5 EDA

customer %>% 
  group_by(Gender) %>% 
  summarise(count = n()) %>% 
  mutate(perc = count/sum(count)*100) %>% 
  ggplot(aes(x = Gender,
             y= perc,
             fill = Gender)) +
  geom_bar(stat = 'identity',
           alpha = .5,
           show.legend = F)+
  geom_text(aes(label = paste0(perc, '%')),
            size = 4,
            vjust = -.5) +
  labs(title = "Gender Distribution",
       y = 'Percent')

table(customer$Gender)
## 
## Female   Male 
##    112     88
  • insight berdasarkan visualisasi diatas dapat diketahui proporsi pada kolom Gender ,ternyata Wanita mendominasi dibanding Pria yaitu 56% atau 112 dibading pria 44% atau 88

6 PCA

6.1 menyiapkan data

quanti <- customer %>%
  select_if(is.numeric) %>%
  colnames()
quantivar <- which(colnames(customer) %in% quanti)
quali <- customer %>% 
  select_if(is.factor) %>% 
  colnames()
qualivar <- customer %>%
  select_if(is.factor) %>%
  colnames()
qualivar <- which(colnames(customer) %in% quali)
customer_pca <- PCA(
  X = customer, 
  scale.unit = T, 
  quali.sup = qualivar, 
  ncp = 4, 
  graph = F  
  )

customer_pca
## **Results for the Principal Component Analysis (PCA)**
## The analysis was performed on 200 individuals, described by 5 variables
## *The results are available in the following objects:
## 
##    name                description                                          
## 1  "$eig"              "eigenvalues"                                        
## 2  "$var"              "results for the variables"                          
## 3  "$var$coord"        "coord. for the variables"                           
## 4  "$var$cor"          "correlations variables - dimensions"                
## 5  "$var$cos2"         "cos2 for the variables"                             
## 6  "$var$contrib"      "contributions of the variables"                     
## 7  "$ind"              "results for the individuals"                        
## 8  "$ind$coord"        "coord. for the individuals"                         
## 9  "$ind$cos2"         "cos2 for the individuals"                           
## 10 "$ind$contrib"      "contributions of the individuals"                   
## 11 "$quali.sup"        "results for the supplementary categorical variables"
## 12 "$quali.sup$coord"  "coord. for the supplementary categories"            
## 13 "$quali.sup$v.test" "v-test of the supplementary categories"             
## 14 "$call"             "summary statistics"                                 
## 15 "$call$centre"      "mean of the variables"                              
## 16 "$call$ecart.type"  "standard error of the variables"                    
## 17 "$call$row.w"       "weights for the individuals"                        
## 18 "$call$col.w"       "weights for the variables"
customer_pca$eig
##        eigenvalue percentage of variance cumulative percentage of variance
## comp 1 1.97911134              49.477783                          49.47778
## comp 2 1.32577374              33.144344                          82.62213
## comp 3 0.67276940              16.819235                          99.44136
## comp 4 0.02234552               0.558638                         100.00000
plot.PCA(
  x = customer_pca,        #model dari pca
  choix = "ind",    #jenis visualisasi yang akan ditampilkan
  habillage = "Age",#pewarnaan observasi berdasarkan variable categorical
  select = "contrib 5"#menghilangkan label variable categorical
)

* insight dapat diketahui outlier antara lain 9,11, 199, 198 , 200

plot.PCA(
  x = customer_pca,
  choix = "var"
)

# Modelling

7 KMEANS

fviz_nbclust(
  x = customer_clean,
  FUNcluster = kmeans,
  method = "wss"
)

* insight berdasarkan plot elbow method kita akan mengambil K sebanyak 5

customer_matrix <- data.matrix(customer[c("Gender")])
customer <- data.frame(customer, customer_matrix)
customer_field<-c("Gender.1", "Age", "Annual.Income..k..", "Spending.Score..1.100.")
customer
set.seed(100)
segmentation <-kmeans(x=customer[c(customer_field)],centers=5,nstart=25)
segmentation$centers
##   Gender.1      Age Annual.Income..k.. Spending.Score..1.100.
## 1 1.417722 43.08861           55.29114               49.56962
## 2 1.461538 32.69231           86.53846               82.12821
## 3 1.527778 40.66667           87.75000               17.58333
## 4 1.391304 25.52174           26.30435               78.56522
## 5 1.391304 45.21739           26.30435               20.91304
segmentation
## K-means clustering with 5 clusters of sizes 79, 39, 36, 23, 23
## 
## Cluster means:
##   Gender.1      Age Annual.Income..k.. Spending.Score..1.100.
## 1 1.417722 43.08861           55.29114               49.56962
## 2 1.461538 32.69231           86.53846               82.12821
## 3 1.527778 40.66667           87.75000               17.58333
## 4 1.391304 25.52174           26.30435               78.56522
## 5 1.391304 45.21739           26.30435               20.91304
## 
## Clustering vector:
##   [1] 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5
##  [38] 4 5 4 5 4 5 4 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [112] 1 1 1 1 1 1 1 1 1 1 1 1 2 3 2 1 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 1 2 3 2 3 2
## [149] 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2 3
## [186] 2 3 2 3 2 3 2 3 2 3 2 3 2 3 2
## 
## Within cluster sum of squares by cluster:
## [1] 30157.266 13982.051 17678.472  4627.739  8954.087
##  (between_SS / total_SS =  75.6 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
ggplot(customer, aes(x = Annual.Income..k.. , y = Spending.Score..1.100.)) + 
  geom_point(stat = "identity", aes(color = as.factor(segmentation$cluster))) +
  scale_color_discrete(name=" ",
                       breaks=c("1", "2", "3", "4", "5"),
                       labels=c("Cluster 1", "Cluster 5", "Cluster 3", "Cluster 4", "Cluster 2")) +
  ggtitle("Mall Customer Segmens", subtitle = "K-means Clustering")

8 KMEDOIDS

segmentation_pam <- pam(customer,5)
segmentation_pam
## Medoids:
##       ID CustomerID Gender Age Annual.Income..k.. Spending.Score..1.100.
## [1,]  22         22      2  25                 24                     73
## [2,]  43         43      2  48                 39                     36
## [3,]  97         97      1  47                 60                     47
## [4,] 162        162      1  29                 79                     83
## [5,] 167        167      2  42                 86                     20
##      Gender.1
## [1,]        2
## [2,]        2
## [3,]        1
## [4,]        1
## [5,]        2
## Clustering vector:
##   [1] 1 1 2 1 1 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2
##  [38] 1 2 1 2 1 2 1 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3
##  [75] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## [112] 3 3 3 3 3 3 3 3 3 3 3 3 4 3 4 3 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4
## [149] 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4 5
## [186] 4 5 4 5 4 5 4 5 4 5 4 5 4 5 4
## Objective function:
##    build     swap 
## 27.83178 25.91679 
## 
## Available components:
##  [1] "medoids"    "id.med"     "clustering" "objective"  "isolation" 
##  [6] "clusinfo"   "silinfo"    "diss"       "call"       "data"
segmentation_pam$medoids
##      CustomerID Gender Age Annual.Income..k.. Spending.Score..1.100. Gender.1
## [1,]         22      2  25                 24                     73        2
## [2,]         43      2  48                 39                     36        2
## [3,]         97      1  47                 60                     47        1
## [4,]        162      1  29                 79                     83        1
## [5,]        167      2  42                 86                     20        2
fviz_cluster(
  segmentation_pam,
  data = customer,
  choose.vars = NULL,
  stand = TRUE,
  axes = c(1, 2),
  geom = c("point", "text"),
  repel = FALSE,
  show.clust.cent = TRUE,
  ellipse = TRUE,
  ellipse.type = "convex",
  ellipse.level = 0.95,
  ellipse.alpha = 0.2,
  shape = NULL,
  pointsize = 1.5,
  labelsize = 12,
  main = "Cluster plot",
  xlab = NULL,
  ylab = NULL,
  outlier.color = "black",
  outlier.shape = 19,
  outlier.pointsize = pointsize,
  outlier.labelsize = labelsize,
  ggtheme = theme_grey()
)

9 DBSCAN

library(dbscan)
segmentation_dbscan <- dbscan(customer_clean, eps = 3, minPts = 5)
segmentation_dbscan
## DBSCAN clustering for 200 objects.
## Parameters: eps = 3, minPts = 5
## Using euclidean distances and borderpoints = TRUE
## The clustering contains 0 cluster(s) and 200 noise points.
## 
##   0 
## 200 
## 
## Available fields: cluster, eps, minPts, dist, borderPoints
hullplot(customer_clean,segmentation_dbscan$cluster)

10 Kesimpulan

Pada Project ini membuat 3 model Clustering yaitu KMEANS, KMEDOIDS, DBSCAN menghasilkan segmentasi pelanggan yaitu:

  • Kmeans

HASIL Kluster 1 * Didominasi oleh perempuan, usia rata-rata adalah 43 tahun, pendapatan tahunan rata-rata 55.291 dan skor pengeluaran rata-rata adalah 49 dari 100. Kluster 2 * Didominasi oleh perempuan, usia rata-rata adalah 32 tahun, pendapatan tahunan rata-rata $86, dan skor pengeluaran rata-rata adalah 82 dari 100. Kluster 3 * Didominasi oleh laki-laki, usia rata-rata adalah 40 tahun, pendapatan tahunan rata-rata $88, dan skor pengeluaran rata-rata adalah 17 dari 100. Kluster 4 * Didominasi oleh perempuan, usia rata-rata adalah 25 tahun, pendapatan tahunan rata-rata adalah $25, dan skor pengeluaran rata-rata adalah 78 dari 100. Kluster 5 * Didominasi oleh perempuan, usia rata-rata adalah 45 tahun, pendapatan tahunan rata-rata $25, dan skor pengeluaran rata-rata adalah 20 dari 100.

Model kmeans ini berhasil mengelompokan customer dengan sangat baik.

  • Kmedoids

Algoritma k-medoids merupakan pendekatan clustering yang berhubungan dengan k-means clustering untuk mempartisi suatu kumpulan data menjadi k grup atau cluster. Dalam clustering k-medoids, setiap cluster diwakili oleh salah satu titik data dalam cluster. Titik-titik ini diberi nama cluster medoids. K-medoid adalah alternatif kuat untuk pengelompokan k-means. Artinya, algoritme kurang sensitif terhadap noise dan outlier, dibandingkan dengan k-means, karena menggunakan medoid sebagai pusat klaster alih-alih mean (digunakan dalam k-means). Metode clustering k-medoids yang paling umum adalah algoritma PAM (Partitioning Around Medoids, (Kaufman and Rousseeuw 1990)).

Hasil diketahui 1 = pria 2 = wanita

Kluster 1 Didominasi oleh perempuan, usia 25 tahun, pendapatan tahunan $24 dan skor pengeluaran 73 dari 100. Kluster 2 Didominasi oleh perempuan, usia 48 tahun, pendapatan tahunan $36, dan skor pengeluaran 36 dari 100. Kluster 3 Didominasi oleh laki-laki, usia 47 tahun, pendapatan tahunan $60, dan skor pengeluaran 47 dari 100. Kluster 4 Didominasi oleh perempuan, usia 29 tahun, pendapatan tahunan $79, dan skor pengeluaran 83 dari 100. Kluster 5 Didominasi oleh perempuan, usia 42 tahun, pendapatan tahunan $86, dan skor pengeluaran 20 dari 100.

  • DBSCAN

Algoritma Density-based Spatial Clustering of Application with Noise (DBSCAN) merupakan metode clustering yang berbasis kepadatan (density-based) dari posisi amatan data dengan prinsip mengelompokkan data yang relatif berdekatan. DBSCAN sering diterapkan pada data yang banyak mengandung noise, hal ini dikarenakan DBSCAN tidak akan memasukkan data yang dianggap noise kedalam cluster manapun

HASIL berdasarkan model yang dibuat, gagal mengelompokan dikarenakan Based model algoritma ini adalah kepadatan tidak cocok untuk masalah bisnis ini.

11 sumber referensi

Prakasawati, P. E., Chrisnanto, Y. H. and Hadiana, A. I. (2019) ‘Segmentasi Pelanggan Berdasarkan Produk Menggunakan Metode K- Medoids’, KOMIK (Konferensi Nasional Teknologi Informasi dan Komputer), 3(1), pp. 335–339. doi: 10.30865/komik.v3i1.1610.

https://rpubs.com/Davidlimbong/dbscan