<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/k-means-clustering-in-r-exercises/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.r-exercises.com/2018/04/13/11256/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2018/04/6966285895_f9a694f2e4_z.jpg?ssl=1" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2018/04/6966285895_f9a694f2e4_z-300x300.jpg?resize=300%2C300&ssl=1" alt="" width="300" class="wp-image-11257" data-recalc-dims="1" height="300"></a></p>
<p>K-means is efficient, and perhaps, the most popular clustering method. It is a way for finding natural groups in otherwise <i>unlabeled</i> data. You specify the number of clusters you want defined and the algorithm minimizes the total within-cluster variance.</p>
<p>In this exercise, we will play around with the base R inbuilt k-means function on some <i>labeled</i> data.</p>
<p>Solutions are available <a rel="nofollow" href="https://www.r-exercises.com/2018/04/13/11260/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong></p>
<p>Feed the columns with sepal measurements in the inbuilt iris data-set to the k-means; save the cluster vector of each observation. Use 3 centers and set the random seed to 1 before.</p>
<p><strong>Exercise 2</strong></p>
<p>Check the proportions of each species by cluster.</p>
<p><strong>Exercise 3</strong></p>
<p>Make a plot with sepal length on the horizontal axis and width on the vertical axis. Find a way to visualize both the actual species and the cluster the algorithm is categorized into.</p>
<p><strong>Exercise 4</strong></p>
<p>Repeat the clustering from step one, but include petal measurements also. Does the clustering reflect the actual species better now?</p>
<div>
<aside><img src="https://www.r-exercises.com/2018/04/13/11256/"></aside>
<div><strong>Learn more</strong> about cluster analysis in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/applied-multivariate-analysis-with-r/" target="_blank">Applied Multivariate Analysis with R</a>. In this course you will learn how to work with hiërarchical clustering, k-means clustering and much more.</div>
</div>
<p><strong>Exercise 5</strong></p>
<p>Create a new data-set identical to iris, but multiply the “Petal.Width” by 2. Are the results different now?</p>
<p><strong>Exercise 6</strong></p>
<p>Standardize your new data-set so that each variable has a mean of 0 and a variance of 1; check once more if multiplying by two makes a difference.</p>
<p><strong>Exercise 7</strong></p>
<p>Read in the Titanic <code>train.csv</code> data-set from <a rel="nofollow" href="https://www.kaggle.com/c/titanic/data" target="_blank">Kaggle.com</a> (you might have to sign up first). Turn the sex column into a dummy variable, <code>=1</code>, if it is male, “0” otherwise, and “Pclass” into a dummy variable for the most common class 3. Using four columns, Sex, SibSp, Parch and Fare, apply the k-means algorithm to get 4 clusters and use nstart=20. Remember to set the seed to 1 so the results are comparable. Note that these four variables have no special meaning to the problem and dummy data in k-means is probably not a good idea in general – we are just playing around.</p>
<p><strong>Exercise 8</strong></p>
<p>Now, how would you describe, in words, each of the clusters and what the survival rates were?</p>
<p><strong>Exercise 9</strong></p>
<p>Now, 4 clusters was an arbitrary choice. Apply k-means clustering using nstart=20, but using k from 2 to 20 and store the result.</p>
<p><strong>Exercise 10</strong></p>
<p>Plot the percent of variance explained by the clusters (between sum of squares over the total sum of squares). What seems like a reasonable number of clusters, according to the <a rel="nofollow" href="https://en.wikipedia.org/wiki/Determining_the_number_of_clusters_in_a_data_set#The_elbow_method" target="_blank">elbow method?</a></p>
<p> </p>
<p>(Picture by <a rel="nofollow" href="https://www.flickr.com/photos/giveawayboy/" target="_blank">giveawayboy)</a></p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="https://www.r-exercises.com/2018/02/28/advanced-techniques-with-raster-data-part-1-unsupervised-classification/" title="Advanced Techniques With Raster Data: Part 1 – Unsupervised Classification" target="_blank">Advanced Techniques With Raster Data: Part 1 – Unsupervised Classification </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/06/10/density-based-clustering-exercises/" title="Density-Based Clustering Exercises" target="_blank">Density-Based Clustering Exercises </a></li>
<li><a rel="nofollow" href="https://www.r-exercises.com/2017/11/29/spatial-data-analysis-introduction-to-raster-processing-part-1/" title="Spatial Data Analysis: Introduction to Raster Processing (Part 1)" target="_blank">Spatial Data Analysis: Introduction to Raster Processing (Part 1) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.r-exercises.com/2018/04/13/11256/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/jOu8Vb0geIc" height="1"></a></div>