Path: blob/master/2019-spring/materials/tutorial_11/multiple_initializations.R
2051 views
library(tidyverse)1library(Rtsne)23pokemon <- read_csv('data/pokemon.csv') %>%4select(Speed, Defense)567891011generate_random_clusters <- function(df, num_clusters, num_iters){1213num_seed <- round(runif(1, min = 1, max = 1e6))1415set.seed(num_seed)161718kmeans_results <- df %>%19kmeans(centers = num_clusters, iter.max = num_iters)2021df$cluster <- as.factor(kmeans_results$cluster)2223cluster_viz <- ggplot(df, aes_string(x = 'Speed', y = 'Defense', colour = 'cluster')) +24geom_point(alpha = 0.5) +25labs(x = "Speed",26y = "Defense") +27theme(legend.position = "none")28return(cluster_viz)29}3031plot_list <- list()3233for (i in 1:6){34plot_list[[i]] <- generate_random_clusters(pokemon, 4, 1)35}3637library(cowplot)38plot_matrix <- do.call(plot_grid, plot_list)3940cowplot::ggsave(plot_matrix, filename = 'imgs/multiple_initializations.png',41units = 'in', width = 8, height = 4)4243