<span><span lang="en"><span>We will develop a well-known k-NN exercise originally published in <i>&quot;Machine Learning in R&quot; </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><br><br><span lang="en"><span><span>The exercise we will develop is: &quot;diagnosing breast cancer with the k-NN algorithm&quot;, </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>### libraries</span></span></span><br><span lang="en"><span><span>library(&quot;class&quot;)<br>library(&quot;gmodels&quot;)<br><br>### import data </span></span></span><br><span lang="en"><span><span>wbcd &lt;- read.csv(&quot;wisc_bc_data.csv&quot;, stringsAsFactors = FALSE)<br><br>### exoplore data structure </span></span></span><br><span lang="en"><span><span>str(wbcd)</span></span></span><br><span lang="en"><span><span></span></span></span><br><br><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><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><br><br><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><br><span lang="en"><span><span>table(wbcd$diagnosis)<br><br>### </span></span></span><span><span lang="en"><span>set the levels of the diagnostic variable</span></span></span><br><span lang="en"><span><span>wbcd$diagnosis&lt;- factor(wbcd$diagnosis, levels = c(&quot;B&quot;, &quot;M&quot;),<br>                        labels = c(&quot;Benign&quot;, &quot;Malignant&quot;))<br>### </span></span></span><span lang="en"><span><span><span lang="en"><span>explore the percentages of the diagnoses</span></span> </span></span></span><br><span lang="en"><span><span>round(prop.table(table(wbcd$diagnosis)) * 100, digits = 1)<br> </span></span></span><br><span><span lang="en"><span>### </span></span><span lang="en"><span>explore some other variables</span></span></span><br><span lang="en"><span><span>summary(wbcd[c(&quot;radius_mean&quot;, &quot;area_mean&quot;, &quot;smoothness_mean&quot;)])<br><br>### define normalizing function<br>normalize &lt;- function(x) {<br>  return ((x - min(x)) / (max(x) - min(x)))<br>}<br> </span></span></span><br><span lang="en"><span><span>### normalize the data set</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)<br><br>### data preparation (training and test sets)<br>wbcd_train &lt;- wbcd_n[1:469, ]<br>wbcd_test &lt;- wbcd_n[470:569, ]<br><br>wbcd_train_labels &lt;- wbcd[1:469, 1]<br>wbcd_test_labels &lt;- wbcd[470:569, 1]<br><br>### training the model<br>wbcd_test_pred &lt;- knn(train = wbcd_train, test = wbcd_test,<br>                      cl = wbcd_train_labels, k = 21)<br><br>### evaluating the model<br>CrossTable(x = wbcd_test_labels, y = wbcd_test_pred,<br>           prop.chisq=FALSE)</span></span></span><br><br><div><a href="https://2.bp.blogspot.com/-eyxCeDH7yjI/WP9rdJ3lVZI/AAAAAAAACz8/6HhpxNR2vJco_Pe-imp-VAZ0DBOCLhj7QCLcB/s1600/wbc_1.png"></a></div><span lang="en"><span><span><br>### improving model performance?</span></span></span><br><span lang="en"><span><span>###</span></span></span><span lang="en"><span><span><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> <br><br>### Z-score<br>wbcd_z &lt;- as.data.frame(scale(wbcd[-1]))<br><br>summary(wbcd_z$area_mean)<br><br>### data preparation (training and test sets)<br>wbcd_train &lt;- wbcd_z[1:469, ]<br>wbcd_test &lt;- wbcd_z[470:569, ]</span></span></span><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><br><div><a href="https://1.bp.blogspot.com/-LWV-lCVcR8o/WP9sNpGbt7I/AAAAAAAAC0I/WepI_crOAyMAXqLVgOsGsZAbvlCRvH_DgCLcB/s1600/wbc_2.png"></a></div><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" href="https://github.com/pakinja/Data-R-Value/tree/master/MachineLearning_KNN">https://github.com/pakinja/Data-R-Value</a></span></span></span>