Professional Documents
Culture Documents
Div: B Batch: B2
Experiment No.6
Title: Classifier.
Code Statement:
Consider 18 points data set referred in theory class. Consider test sample P(3,2). Apply following
algorithms to find the class of this test point.
i. NN
ii. KNN with K=5 and K=7.
iii. MKNN with K=5
iv. R-NN Radius based algorithm with radius as 1.45 units.
data = read.csv("knn1_csv.csv")
euc = sqrt((data$x - 3)**2 + (data$y - 2)**2)
data = cbind(data, euc)
sorted_data = data[order(data$euc), ]
# NN Algorithm
cat("The class of P for NN is:", sorted_data[1,4])
df = sorted_data[1:7,]
l1 = length(which(df$class == 1))
l2 = length(which(df$class == 2))
l3 = length(which(df$class == 3))
# R-NN
r = 1.45
df3 = sorted_data[sorted_data$euc < r,]
l1 = length(which(df3$class == 1))
l2 = length(which(df3$class == 2))
l3 = length(which(df3$class == 3))
#M-KNN
dk = max(df$euc)
dl = min(df$euc)
weight = (dk - df$euc) / (dk - dl)
df1 = cbind(df,weight)
Results: Display the output obtained on R console for all the cases.
Conclusion: (Write the conclusion in your words. Write the answers to the questions asked in all the above code
statements.)
In this experiment, we studied about nearest neighbor algorithms including KNN,MKNN and RNN
taking various possibilities without using the inbuilt functions.