<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/classification-using-nearest-neighbors-k-nn/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://feedproxy.google.com/~r/DataRValue/~3/DUXm99BJwcQ/classification-using-nearest-neighbors.html"> Data R Value</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><span><span lang="en"><span>We will develop a well-known k-NN exercise originally published in <i>“Machine Learning in R” </i>by Brett Lantz, PACKT publishing 2015.</span></span></span><br><span><span lang="en"><span><br></span></span></span><span><span lang="en"><span>K-nearest neighbors is a classification algorithm and is perhaps one of the simplest machine learning algorithms.</span></span></span></p>
<p><span lang="en"><span><span>The exercise we will develop is: “diagnosing breast cancer with the k-NN algorithm”, </span></span></span><span lang="en"><span><span><span lang="en"><span>and we will use the Wisconsin Breast Cancer Diagnostic dataset from the UCI Machine Learning Repository at http://archive.ics.uci.edu/ml.</span></span></span></span></span><br><span lang="en"><span><span><span lang="en"><span></span></span></span></span></span><span lang="en"><span><span><span lang="en"></span></span></span></span><br><span lang="en"><span><span><span lang="en"><span>We will carry out the exercise verbatim as published in the aforementioned reference.</span></span></span></span></span><br><span lang="en"><span></span></span><br><span lang="en"><span><span><br></span></span></span><span lang="en"><span><span><b>### libraries</b></span></span></span><br><span lang="en"><span><span>library(“class”)<br>library(“gmodels”)<br><b><br>### import data </b></span></span></span><br><span lang="en"><span><span>wbcd &lt;- read.csv(“wisc_bc_data.csv”, stringsAsFactors = FALSE)
<p><b>### exoplore data structure </b><br><span lang="en"><span><span>str(wbcd)</span></span></span><br><span lang="en"><span><span></span></span></span></p></span>
<p><b><span lang="en"><span><span>### </span></span></span><span lang="en"><span><span><span lang="en"><span>remove the id column that will not be needed</span></span></span></span></span></b><br><span lang="en"><span><span></span></span></span><br><span lang="en"><span><span>wbcd &lt;- wbcd[-1]</span></span></span><br><span lang="en"><span><span></span></span></span></p>
<p><b><span lang="en"><span><span>### e</span></span></span><span lang="en"><span><span><span lang="en"><span>xplore how many records are classified as benign and how many ### as malignant</span></span></span></span></span></b><br><span lang="en"><span><span>table(wbcd$diagnosis)
<p><b>### </b><span><b><span lang="en"><span>set the levels of the diagnostic variable</span></span></b></span><br><span lang="en"><span><span>wbcd$diagnosis&lt;- factor(wbcd$diagnosis, levels = c(“B”, “M”),<br>                        labels = c(“Benign”, “Malignant”))<br><b>### </b></span></span></span><span lang="en"><span><span><b><span lang="en"><span>explore the percentages of the diagnoses</span></span></b> </span></span></span><br><span lang="en"><span><span>round(prop.table(table(wbcd$diagnosis)) * 100, digits = 1)<br> </span></span></span><br><span><b><span lang="en"><span>### </span></span><span lang="en"><span>explore some other variables</span></span></b></span><br><span lang="en"><span><span>summary(wbcd[c(“radius_mean”, “area_mean”, “smoothness_mean”)])
<p><b>### define normalizing</b> <b>function</b><br>normalize &lt;- function(x) {<br>  return ((x – min(x)) / (max(x) – min(x)))<br>}<br><b> </b><br><span lang="en"><span><span><b>### normalize the data set</b></span></span></span><br><span lang="en"><span><span>wbcd_n &lt;- as.data.frame(lapply(wbcd[2:31], normalize))<br> </span></span></span><br><span lang="en"><span><span>summary(wbcd_n$area_mean)
<p><b>### data preparation (training and test sets)</b><br>wbcd_train &lt;- wbcd_n[1:469, ]<br>wbcd_test &lt;- wbcd_n[470:569, ]</p>
<p>wbcd_train_labels &lt;- wbcd[1:469, 1]<br>wbcd_test_labels &lt;- wbcd[470:569, 1]</p>
<p><b>### training the model</b><br>wbcd_test_pred &lt;- knn(train = wbcd_train, test = wbcd_test,<br>                      cl = wbcd_train_labels, k = 21)</p>
<p><b>### evaluating the model</b><br>CrossTable(x = wbcd_test_labels, y = wbcd_test_pred,<br>           prop.chisq=FALSE)</p></span>
<div><a target="_blank" rel="nofollow" href="https://i2.wp.com/2.bp.blogspot.com/-eyxCeDH7yjI/WP9rdJ3lVZI/AAAAAAAACz8/6HhpxNR2vJco_Pe-imp-VAZ0DBOCLhj7QCLcB/s1600/wbc_1.png?ssl=1"><img src="https://i1.wp.com/2.bp.blogspot.com/-eyxCeDH7yjI/WP9rdJ3lVZI/AAAAAAAACz8/6HhpxNR2vJco_Pe-imp-VAZ0DBOCLhj7QCLcB/s400/wbc_1.png?resize=338%2C400&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p><span lang="en"><span><span><br><b>### improving model performance?</b></span></span></span><br><span lang="en"><span><span><b>###</b></span></span></span><span lang="en"><span><span><b><span lang="en"><span> the exercise will be repeated using z-score to see if there is a ### better performance in the accuracy of the algorithm</span></span> </b>
<p><b>### Z-score</b><br>wbcd_z &lt;- as.data.frame(scale(wbcd[-1]))</p>
<p>summary(wbcd_z$area_mean)</p>
<p><b>### data preparation (training and test sets)</b><br>wbcd_train &lt;- wbcd_z[1:469, ]<br>wbcd_test &lt;- wbcd_z[470:569, ]<br><span lang="en"><span><span><br>wbcd_train_labels &lt;- wbcd[1:469, 1]<br>wbcd_test_labels &lt;- wbcd[470:569, 1]</span></span></span><br><span lang="en"><span><span><br>wbcd_test_pred &lt;- knn(train = wbcd_train, test = wbcd_test,<br>                      cl = wbcd_train_labels, k = 21)<br>CrossTable(x = wbcd_test_labels, y = wbcd_test_pred,<br>           prop.chisq = FALSE)</span></span></span><br><span lang="en"><span><span><br></span></span></span>
<div><a target="_blank" rel="nofollow" href="https://i0.wp.com/1.bp.blogspot.com/-LWV-lCVcR8o/WP9sNpGbt7I/AAAAAAAAC0I/WepI_crOAyMAXqLVgOsGsZAbvlCRvH_DgCLcB/s1600/wbc_2.png?ssl=1"><img src="https://i1.wp.com/1.bp.blogspot.com/-LWV-lCVcR8o/WP9sNpGbt7I/AAAAAAAAC0I/WepI_crOAyMAXqLVgOsGsZAbvlCRvH_DgCLcB/s400/wbc_2.png?resize=343%2C400&ssl=1" border="0" data-recalc-dims="1"></a></div></span>
<p><span><span lang="en"><span>Using z-score no improvement in model accuracy is observed.</span></span></span><br><span lang="en"><span><span></span></span></span><br><span><span lang="en"><span>You can get the exercise and the data set in:</span></span></span><br><span lang="en"><span><span><a target="_blank" rel="nofollow" href="https://github.com/pakinja/Data-R-Value/tree/master/MachineLearning_KNN">https://github.com/pakinja/Data-R-Value</a></span></span></span><img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/DataRValue/~4/DUXm99BJwcQ"></p>
<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://feedproxy.google.com/~r/DataRValue/~3/DUXm99BJwcQ/classification-using-nearest-neighbors.html"> Data R Value</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 title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/5yK4gwYpDCE"></a></div></span></span></span></span></span></span></span></span></span></span></span></span>