Classification Algorithm

K- Nearest Neighbor

install.packages(“class”) install.packages(“caret”) install.packages(“gmodels”)

wbcd <- read.csv("D:/Users/jayapate/Downloads/wbcd.csv")
#View(wbcd)
str(wbcd)
## 'data.frame':    569 obs. of  32 variables:
##  $ id               : int  87139402 8910251 905520 868871 9012568 906539 925291 87880 862989 89827 ...
##  $ diagnosis        : Factor w/ 2 levels "B","M": 1 1 1 1 1 1 1 2 1 1 ...
##  $ radius_mean      : num  12.3 10.6 11 11.3 15.2 ...
##  $ texture_mean     : num  12.4 18.9 16.8 13.4 13.2 ...
##  $ perimeter_mean   : num  78.8 69.3 70.9 73 97.7 ...
##  $ area_mean        : num  464 346 373 385 712 ...
##  $ smoothness_mean  : num  0.1028 0.0969 0.1077 0.1164 0.0796 ...
##  $ compactness_mean : num  0.0698 0.1147 0.078 0.1136 0.0693 ...
##  $ concavity_mean   : num  0.0399 0.0639 0.0305 0.0464 0.0339 ...
##  $ points_mean      : num  0.037 0.0264 0.0248 0.048 0.0266 ...
##  $ symmetry_mean    : num  0.196 0.192 0.171 0.177 0.172 ...
##  $ dimension_mean   : num  0.0595 0.0649 0.0634 0.0607 0.0554 ...
##  $ radius_se        : num  0.236 0.451 0.197 0.338 0.178 ...
##  $ texture_se       : num  0.666 1.197 1.387 1.343 0.412 ...
##  $ perimeter_se     : num  1.67 3.43 1.34 1.85 1.34 ...
##  $ area_se          : num  17.4 27.1 13.5 26.3 17.7 ...
##  $ smoothness_se    : num  0.00805 0.00747 0.00516 0.01127 0.00501 ...
##  $ compactness_se   : num  0.0118 0.03581 0.00936 0.03498 0.01485 ...
##  $ concavity_se     : num  0.0168 0.0335 0.0106 0.0219 0.0155 ...
##  $ points_se        : num  0.01241 0.01365 0.00748 0.01965 0.00915 ...
##  $ symmetry_se      : num  0.0192 0.035 0.0172 0.0158 0.0165 ...
##  $ dimension_se     : num  0.00225 0.00332 0.0022 0.00344 0.00177 ...
##  $ radius_worst     : num  13.5 11.9 12.4 11.9 16.2 ...
##  $ texture_worst    : num  15.6 22.9 26.4 15.8 15.7 ...
##  $ perimeter_worst  : num  87 78.3 79.9 76.5 104.5 ...
##  $ area_worst       : num  549 425 471 434 819 ...
##  $ smoothness_worst : num  0.139 0.121 0.137 0.137 0.113 ...
##  $ compactness_worst: num  0.127 0.252 0.148 0.182 0.174 ...
##  $ concavity_worst  : num  0.1242 0.1916 0.1067 0.0867 0.1362 ...
##  $ points_worst     : num  0.0939 0.0793 0.0743 0.0861 0.0818 ...
##  $ symmetry_worst   : num  0.283 0.294 0.3 0.21 0.249 ...
##  $ dimension_worst  : num  0.0677 0.0759 0.0788 0.0678 0.0677 ...
dim(wbcd)
## [1] 569  32
#To remove the first column ID which is not required
wbcd <- wbcd [-1]
View(wbcd)
#Table format of Diagnosis
table(wbcd$diagnosis)
## 
##   B   M 
## 357 212
#To label B as "Benign" and M as "Malignant"
wbcd$diagnosis <- factor(wbcd$diagnosis, levels = c("B","M"), labels = c("Benign", "Malignant"))
# To calculate the % of Benign and Malignant in the dataset
round(prop.table(table(wbcd$diagnosis))*100,1)
## 
##    Benign Malignant 
##      62.7      37.3
summary(wbcd[c("radius_mean","texture_mean","perimeter_mean")])
##   radius_mean      texture_mean   perimeter_mean  
##  Min.   : 6.981   Min.   : 9.71   Min.   : 43.79  
##  1st Qu.:11.700   1st Qu.:16.17   1st Qu.: 75.17  
##  Median :13.370   Median :18.84   Median : 86.24  
##  Mean   :14.127   Mean   :19.29   Mean   : 91.97  
##  3rd Qu.:15.780   3rd Qu.:21.80   3rd Qu.:104.10  
##  Max.   :28.110   Max.   :39.28   Max.   :188.50
#To Normanalize or standardize data
#wbcd <- wbcd [-1]
#View(wbcd)
#scaledata <- scale(wbcd, center = TRUE, scale = TRUE)
#wbcd_n <- as.data.frame(lapply(wbcd[2:31], scaledata))
#wbcd_n <- as.data.frame(lapply(wbcd[2:31], norm))
norm <- function(x){ 
  return((x-min(x))/(max(x)-min(x)))
}
#Apply the normalization function to wbcd dataset
wbcd_n <- as.data.frame(lapply(wbcd[2:30], norm))
#View(wbcd_n)

#create training and test datasets
wbcd_train <- wbcd_n[1:469,]
wbcd_test <- wbcd_n[470:569,]

#Get labels for training and test datasets

wbcd_train_labels <- wbcd[1:469,1]
wbcd_test_labels <- wbcd[470:569,1]


# Build a KNN model on taining dataset
library("class")
## Warning: package 'class' was built under R version 3.5.1
library("caret")
## Warning: package 'caret' was built under R version 3.5.1
## Loading required package: lattice
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.5.1
# Building the KNN model on training dataset and also need labels which we are including c1. Once we build the preduction model
# we have to test on test dataset
wbcd_pred <- knn(train = wbcd_train, test = wbcd_test, cl = wbcd_train_labels, k=21)
class(wbcd_train)
## [1] "data.frame"
class(wbcd_test)
## [1] "data.frame"
## Now evualuation the model performance

# install package gmodels
#install.packages("gmodels")
library("gmodels")
## Warning: package 'gmodels' was built under R version 3.5.1
# Create cross table of predicted and actual
CrossTable( x =  wbcd_test_labels, y = wbcd_pred)
## 
##  
##    Cell Contents
## |-------------------------|
## |                       N |
## | Chi-square contribution |
## |           N / Row Total |
## |           N / Col Total |
## |         N / Table Total |
## |-------------------------|
## 
##  
## Total Observations in Table:  100 
## 
##  
##                  | wbcd_pred 
## wbcd_test_labels |    Benign | Malignant | Row Total | 
## -----------------|-----------|-----------|-----------|
##           Benign |        61 |         0 |        61 | 
##                  |    12.353 |    21.960 |           | 
##                  |     1.000 |     0.000 |     0.610 | 
##                  |     0.953 |     0.000 |           | 
##                  |     0.610 |     0.000 |           | 
## -----------------|-----------|-----------|-----------|
##        Malignant |         3 |        36 |        39 | 
##                  |    19.321 |    34.348 |           | 
##                  |     0.077 |     0.923 |     0.390 | 
##                  |     0.047 |     1.000 |           | 
##                  |     0.030 |     0.360 |           | 
## -----------------|-----------|-----------|-----------|
##     Column Total |        64 |        36 |       100 | 
##                  |     0.640 |     0.360 |           | 
## -----------------|-----------|-----------|-----------|
## 
##