Att utvärdera olika värden på K visuellt

Klusteranalys i R

Dmitriy (Dima) Gorenshteyn

Lead Data Scientist, Memorial Sloan Kettering Cancer Center

Total kvadratsumma inom kluster: k = 1

Klusteranalys i R

Total kvadratsumma inom kluster: k = 2

Klusteranalys i R

Total kvadratsumma inom kluster: k = 3

Klusteranalys i R

Total kvadratsumma inom kluster: k = 4

Klusteranalys i R

Armbågsplot

Klusteranalys i R

Armbågsplot

Klusteranalys i R

Skapa armbågsplotten

model <- kmeans(x = lineup, centers = 2)
model$tot.withinss
[1] 1434.5
Klusteranalys i R

Skapa armbågsplotten

library(purrr)

tot_withinss <- map_dbl(1:10,  function(k){
  model <- kmeans(x = lineup, centers = k)
  model$tot.withinss
})

elbow_df <- data.frame( k = 1:10, tot_withinss = tot_withinss ) print(elbow_df)
    k tot_withinss
1   1    3489.9167
2   2    1434.5000
3   3     881.2500
4   4     637.2500
... ...   ...
Klusteranalys i R

Skapa armbågsplotten

ggplot(elbow_df, aes(x = k, y = tot_withinss)) +
  geom_line() +
  scale_x_continuous(breaks = 1:10)

Klusteranalys i R

Nu kör vi en övning!

Klusteranalys i R

Preparing Video For Download...