This lesson is being piloted (Beta version)

Unsupervised Learning

Overview

Teaching: 20 min
Exercises: 0 min
Questions
  • What is Unsupervised Learning in Machine Learning model

Objectives
  • Learn how to use K-mean clustering in ML model

7 Unsupervised Learning

image image

image

image

image

image

image

image

7.1.2 Example with K=3

image

image

7.1.3 Implementation

library(ggplot2)
library(factoextra)
library(purrr)
data(iris)
ggplot(iris,aes(x=Sepal.Length,y=Petal.Width))+
      geom_point(aes(color=Species))
set.seed(123)
km <- kmeans(iris[,3:4],3,nstart=20)

table(km$cluster,iris$Species)
fviz_cluster(km,data=iris[,3:4])

image

7.2 How to find optimal K values:

7.2.1 Elbow approach

The optimal K-values can be found from the Elbow using method=”wss”:

fviz_nbclust(iris[,3:4], kmeans, method = "wss")

image

7.2.2 Gap-Statistics approach

image

E*n: expectation under a sample size of n from the reference distribution image

image

library(cluster)
# B is number of Monte Carlo bootstrap samples
gap_stat <- clusGap(iris[,3:4], FUN = kmeans, nstart=20, K.max = 10, B = 50)
fviz_gap_stat(gap_stat)

image

Key Points

  • K-mean