## demo of k-means clustering... ## Step 1: make up some data x <- rbind( matrix(rnorm(100, mean=0, sd = 0.3), ncol = 2), # cluster 1 matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2), # cluster 2 matrix(c(rnorm(50, mean = 1, sd = 0.3), # cluster 3 rnorm(50, mean = 0, sd = 0.3)), ncol = 2)) colnames(x) <- c("x", "y") ## Step 2: show the data without clustering plot(x) ## Step 3: show the data with the known clusters (this is just so we ## can see how well k-means clustering recreates the original clusters we ## created in step 1) colors <- as.factor(c( rep("c1", times=50), rep("c2", times=50), rep("c3", times=50))) plot(x, col=colors) ## Step 3: cluster the data ## NOTE: nstart=25, so kmeans() will cluster using 25 different starting points ## and return the best cluster. (cl <- kmeans(x, centers=3, nstart=25)) ## Step 4: plot the data, coloring the points with the clusters plot(x, col = cl$cluster)

Advertisements
(function(){var c=function(){var a=document.getElementById("crt-779349427");window.Criteo?(a.parentNode.style.setProperty("display","inline-block","important"),a.style.setProperty("display","block","important"),window.Criteo.DisplayAcceptableAdIfAdblocked({zoneid:388248,containerid:"crt-779349427",collapseContainerIfNotAdblocked:!0,callifnotadblocked:function(){a.style.setProperty("display","none","important");a.style.setProperty("visbility","hidden","important")}})):(a.style.setProperty("display","none","important"),a.style.setProperty("visibility","hidden","important"))};if(window.Criteo)c();else{if(!__ATA.criteo.script){var b=document.createElement("script");b.src="//static.criteo.net/js/ld/publishertag.js";b.onload=function(){for(var a=0;a<__ATA.criteo.cmd.length;a++){var b=__ATA.criteo.cmd[a];"function"===typeof b&&b()}};(document.head||document.getElementsByTagName("head")[0]).appendChild(b);__ATA.criteo.script=b}__ATA.criteo.cmd.push(c)}})();
(function(){var c=function(){var a=document.getElementById("crt-764838659");window.Criteo?(a.parentNode.style.setProperty("display","inline-block","important"),a.style.setProperty("display","block","important"),window.Criteo.DisplayAcceptableAdIfAdblocked({zoneid:837497,containerid:"crt-764838659",collapseContainerIfNotAdblocked:!0,callifnotadblocked:function(){a.style.setProperty("display","none","important");a.style.setProperty("visbility","hidden","important")}})):(a.style.setProperty("display","none","important"),a.style.setProperty("visibility","hidden","important"))};if(window.Criteo)c();else{if(!__ATA.criteo.script){var b=document.createElement("script");b.src="//static.criteo.net/js/ld/publishertag.js";b.onload=function(){for(var a=0;a<__ATA.criteo.cmd.length;a++){var b=__ATA.criteo.cmd[a];"function"===typeof b&&b()}};(document.head||document.getElementsByTagName("head")[0]).appendChild(b);__ATA.criteo.script=b}__ATA.criteo.cmd.push(c)}})();