Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2019-spring/materials/tutorial_11/multiple_initializations.R
2051 views
1
library(tidyverse)
2
library(Rtsne)
3
4
pokemon <- read_csv('data/pokemon.csv') %>%
5
select(Speed, Defense)
6
7
8
9
10
11
12
generate_random_clusters <- function(df, num_clusters, num_iters){
13
14
num_seed <- round(runif(1, min = 1, max = 1e6))
15
16
set.seed(num_seed)
17
18
19
kmeans_results <- df %>%
20
kmeans(centers = num_clusters, iter.max = num_iters)
21
22
df$cluster <- as.factor(kmeans_results$cluster)
23
24
cluster_viz <- ggplot(df, aes_string(x = 'Speed', y = 'Defense', colour = 'cluster')) +
25
geom_point(alpha = 0.5) +
26
labs(x = "Speed",
27
y = "Defense") +
28
theme(legend.position = "none")
29
return(cluster_viz)
30
}
31
32
plot_list <- list()
33
34
for (i in 1:6){
35
plot_list[[i]] <- generate_random_clusters(pokemon, 4, 1)
36
}
37
38
library(cowplot)
39
plot_matrix <- do.call(plot_grid, plot_list)
40
41
cowplot::ggsave(plot_matrix, filename = 'imgs/multiple_initializations.png',
42
units = 'in', width = 8, height = 4)
43