Professional Documents
Culture Documents
Div: B Batch: B2
Experiment No.7
Code Statement:
Apply KNN to the Wisconsin Breast Cancer data set . Split the data into training and testing
samples. Scale the data and find the following
1. Accuracy
2. Sensitivity
3. Specificity
4. Precision
# K-NN
library(class)
wbc = read.csv("wbc_csv.csv")
wbc$diagnosis = as.factor(wbc$diagnosis)
set.seed(123)
wbc = wbc[order(runif(569)),]
# normalize
wbc_mod = wbc[,3:32]
n2 = function(b){
(b-min(b)) / (max(b) - min(b))
}
wbc_new = as.data.frame (lapply(wbc_mod,n2))
# K-NN function
wbc_train = wbc_new[1:469,]
wbc_test = wbc_new[470:569,]
Bansilal RamnathAgarwal Charitable Trust’s
VISHWAKARMA INSTITUTE OF TECHNOLOGY – PUNE
Department of SY Common
wbc_train_label = wbc[1:469,2]
wbc_test_label = wbc[470:569,2] #actual
p = knn(wbc_train,wbc_test,wbc_train_label,k=3)
t = table(actual = wbc_test_label, predicted = p)
print(t)
Results: Display the output obtained on R console for all the cases.
Conclusion: (Write the conclusion in your words. Write the values you obtained for all metrics).