<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/density-based-clustering-exercises/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/06/10/density-based-clustering-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=density-based-clustering-exercises"> 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/2017/06/dbscan-exercises-5.png" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/06/dbscan-exercises-5-300x300.png?resize=300%2C300" alt="" class="wp-image-6352" data-recalc-dims="1"></a><br>
Density-based clustering is a technique that allows to partition data into groups with similar characteristics (clusters) but does not require specifying the number of those groups in advance. In density-based clustering, clusters are defined as dense regions of data points separated by low-density regions. Density is measured by the number of data points within some radius.<br>
Advantages of density-based clustering:</p>
<ul>
<li>as mentioned above, it does not require a predefined number of clusters,</li>
<li>clusters can be of any shape, including non-spherical ones,</li>
<li>the technique is able to identify noise data (outliers).</li>
</ul>
<p>Disadvantages:</p>
<ul>
<li>density-based clustering fails if there are no density drops between clusters,</li>
<li>it is also sensitive to parameters that define density (radius and the minimum number of points); proper parameter setting may require domain knowledge.</li>
</ul>
<p>There are different methods of density-based clustering. The most popular are DBSCAN (density-based spatial clustering of applications with noise), which assumes constant density of clusters, OPTICS (ordering points to identify the clustering structure), which allows for varying density, and “mean-shift”.<br>
This set of exercises covers basic techniques for using the DBSCAN method, and allows to compare its result to the results of the k-means clustering algorithm by means of the silhouette analysis.<br>
The set requires the packages <code>dbscan</code>, <code>cluster</code>, and <code>factoextra</code> to be installed. The exercises make use of the <code>iris</code> data set, which is supplied with R, and the <code>wholesale customers</code> data set from the University of California, Irvine (UCI) machine learning repository (<a rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/06/Wholesale-customers-data.csv" target="_blank">download here</a>).<br>
Answers to the exercises are available <a rel="nofollow" href="http://r-exercises.com/2017/06/10/density-based-clustering-exercises-solutions/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong><br>
Create a new data frame using all but the last variable from the <code>iris</code> data set, which is supplied with R.</p>
<p><strong>Exercise 2</strong><br>
Use the <code>scale</code> function to normalize values of all variables in the new data set (with default settings). Ensure that the resulting object is of class <code>data.frame</code>.</p>
<p><strong>Exercise 3</strong><br>
Plot the distribution of distances between data points and their fifth nearest neighbors using the <code>kNNdistplot</code> function from the <code>dbscan</code> package.<br>
Examine the plot and find a tentative threshold at which distances start increasing quickly. On the same plot, draw a horizontal line at the level of the threshold.</p>
<p><strong>Exercise 4</strong><br>
Use the <code>dbscan</code> function from the package of the same name to find density-based clusters in the data. Set the size of the epsilon neighborhood at the level of the found threshold, and set the number of minimum points in the epsilon region equal to 5.<br>
Assign the value returned by the function to an object, and print that object.</p>
<p><strong>Exercise 5</strong><br>
Plot the clusters with the <code>fviz_cluster</code> function from the <code>factoextra</code> package. Choose the geometry type to draw only points on the graph, and assign the <code>ellipse</code> parameter value such that an outline around points of each cluster is not drawn.<br>
(Note that the <code>fviz_cluster</code> function produces a 2-dimensional plot. If the data set contains two variables those variables are used for plotting, if the number of variables is bigger the first two principal components are drawn.)</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/06/10/density-based-clustering-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=density-based-clustering-exercises"></aside>
<div><strong>Learn more</strong> about Data Pre-Processing in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/learning-data-mining-with-r/" target="_blank">R Data Pre-Processing &amp; Data Management – Shape your Data!</a>. In this course you will learn how to:
<ul>
<li>Delve into various algorithms for classification such as KNN and see how they are applied in R</li>
<li>Evaluate k-Means, Connectivity, Distribution, and Density based clustering</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 6</strong><br>
Examine the structure of the cluster object obtained in Exercise 4, and find the vector with cluster assignments. Make a copy of the data set, add the vector of cluster assignments to the data set, and print its first few lines.</p>
<p><strong>Exercise 7</strong><br>
Now look at what happens if you change the epsilon value.</p>
<ol>
<li>Plot again the distribution of distances between data points and their fifth nearest neighbors (with the <code>kNNdistplot</code> function, as in Exercise 3). On that plot, draw horizontal lines at levels 1.8, 0.5, and 0.4.</li>
<li>Use the <code>dbscan</code> function to find clusters in the data with the epsilon set at these values (as in Exercise 4).</li>
<li>Plot the results (as in the Exercise 5, but now set the <code>ellipse</code> parameter value such that an outline around points is drawn).</li>
</ol>
<p><strong>Exercise 8</strong><br>
This exercise shows how the DBSCAN algorithm can be used as a way to detect outliers:</p>
<ol>
<li>Load the <code>Wholesale customers</code> data set, and delete all variables with the exception of <code>Fresh</code> and <code>Milk</code>. Assign the data set to the <code>customers</code> variable.</li>
<li>Discover clusters using the steps from Exercises 2-5: scale the data, choose an epsilon value, find clusters, and plot them. Set the number of minimum points to 5. Use the <code>db_clusters_customers</code> variable to store the output of the <code>dbscan</code> function.</li>
</ol>
<p><strong>Exercise 9</strong><br>
Compare the results obtained in the previous exercise with the results of the k-means algorithm. First, find clusters using this algorithm:</p>
<ol>
<li>Use the same data set, but get rid of outliers for both variables (here the outliers may be defined as values beyond 2.5 standard deviations from the mean; note that the values are already expressed in unit of standard deviation about the mean). Assign the new data set to the <code>customers_core</code> variable.</li>
<li>Use <code>kmeans</code> function to obtain an object with cluster assignments. Set the number of centers equal to 4, and the number of initial random sets (the <code>nstart</code> parameter) equal to 10. Assign the obtained object to the variable <code>km_clusters_customers</code> variable.</li>
<li>Plot clusters using the <code>fviz_cluster</code> function (as in the previous exercise).</li>
</ol>
<p><strong>Exercise 10</strong><br>
Now compare the results of DBSCAN and k-means using silhouette analysis:</p>
<ol>
<li>Retrieve a vector of cluster assignments from the <code>db_clusters_customers</code> object.</li>
<li>Calculate distances between data points in the <code>customers</code> data set using the <code>dist</code> function (with the default parameters).</li>
<li>Use the vector and the distances object as inputs into the <code>silhouette</code> function from the <code>cluster</code> package to get a silhouette information object.</li>
<li>Plot that object with the <code>fviz_silhouette</code> function from the <code>factoextra</code> package.</li>
<li>Repeat the steps described above for the <code>km_clusters_customers</code> object and the <code>customers_core</code> data sets.</li>
<li>Compare two plots and the average silhouette width values.</li>
</ol>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/04/21/data-science-for-doctors-cluster-analysis-exercises/" title="Data science for Doctors: Cluster Analysis Exercises" target="_blank">Data science for Doctors: Cluster Analysis Exercises </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/12/14/hierarchical-clustering-exercises-beginner/" title="Hierarchical Clustering exercises (beginner)" target="_blank">Hierarchical Clustering exercises (beginner) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/02/19/building-shiny-app-exercises-part-7/" title="Building Shiny App exercises part 7" target="_blank">Building Shiny App exercises part 7 </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="http://www.r-exercises.com/2017/06/10/density-based-clustering-exercises/?utm_source=rss&utm_medium=rss&utm_campaign=density-based-clustering-exercises"> 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/bexjSGWIbtc" height="1"></a></div>