-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin_fold.r
More file actions
43 lines (37 loc) · 1.12 KB
/
Copy pathbin_fold.r
File metadata and controls
43 lines (37 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
##Generalized Code of KNN/// Currently using Breast Cancer data set:
data<- read.csv("wdbc.csv") #Import the data
data<- data[,-1] #Removed the first column
n <- nrow(data) #no of rows in the data
data_s<- data[sample(1:n),] #shuffled the data
knn_fold<-function(x) #x
{
n_test<- floor(n*(1/x)); n_train<-n-n_test #Nuber of rows for test and train
data_test<-data_s[1:n_test,]
data_train<-data_s[(n_test+1):n,]
actual_class<-data_s[1:n_test,1]
dist<-NULL
minimum<-NULL
acc<-NULL
accuracy<-NULL
for (i in 1:n_test){
for (j in 1:n_train){
d<-sqrt(sum((data_test[i,-1]-data_train[j,-1])^2))
dist<-c(dist,d)
}
}
distance<-matrix(dist, nrow=n_test, ncol = n_train, byrow = TRUE)
for(k in c(1,3,5,7,9))
{
for(z in 1:n_test)
{
clas <- as.numeric(data_train$M[(order(distance[z,]))[1:k]])
min<- as.numeric(median(clas))
minimum<-c(minimum, min)
}
actual<-as.numeric(actual_class)
check_label<-(minimum==actual)
acc<-((sum(check_label))/length(minimum))*100
accuracy<-c(accuracy,acc)
}
print(accuracy)
}