You are on page 1of 3

Bansilal RamnathAgarwal Charitable Trust’s

VISHWAKARMA INSTITUTE OF TECHNOLOGY – PUNE


Department of SY Common

MD2201: Data Science


Name of the student: Saurabh Jadhav Roll No. 27

Div: B Batch: B2

Date of performance: 10-12-2021

Experiment No.6

Title: Classifier.

Aim: To apply Nearest Neighbor algorithm.

Software used: Programming language R.

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.

Code: (Write code for above statements here)

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])

# K-NN for k=5


df = sorted_data[1:5,]
l1 = length(which(df$class == 1))
l2 = length(which(df$class == 2))
l3 = length(which(df$class == 3))

if (l1>l2 & l1>l3)


cat("\n\nThe class of P for KNN for k=5 is:",1)
if (l2>l1 & l2>l3)
cat("\n\nThe class of P for KNN for k=5 is:",2)
Bansilal RamnathAgarwal Charitable Trust’s
VISHWAKARMA INSTITUTE OF TECHNOLOGY – PUNE
if (l3>l1 & l3>l2) Department of SY Common
cat("\n\nThe class of P for KNN for k=5 is:",3)

df = sorted_data[1:7,]
l1 = length(which(df$class == 1))
l2 = length(which(df$class == 2))
l3 = length(which(df$class == 3))

if (l1>l2 & l1>l3)


cat("\n\nThe class of P for KNN for k=7 is:",1)
if (l2>l1 & l2>l3)
cat("\n\nThe class of P for KNN for k=7 is:",2)
if (l3>l1 & l3>l2)
cat("\n\nThe class of P for KNN for k=7 is:",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))

if (l1>l2 & l1>l3)


cat("\n\nThe class of P for RNN is:",1)
if (l2>l1 & l2>l3)
cat("\n\nThe class of P for RNN is:",2)
if (l3>l1 & l3>l2)
cat("\n\nThe class of P for RNN is:",3)

#M-KNN
dk = max(df$euc)
dl = min(df$euc)
weight = (dk - df$euc) / (dk - dl)
df1 = cbind(df,weight)

sum1 = sum(df1$weight[df1$class == 1])


sum2 = sum(df1$weight[df1$class == 2])
sum3 = sum(df1$weight[df1$class == 3])

if (sum1>sum2 & sum1>sum3)


cat("\n\nThe class of P for MKNN for k=5 is:",1)
if (sum2>sum1 & sum2>sum3)
cat("\n\nThe class of P for MKNN for k=5 is:",2)
if (sum3>sum1 & sum3>sum2)
cat("\n\nThe class of P for MKNN for k=5 is:",3)
Bansilal RamnathAgarwal Charitable Trust’s
VISHWAKARMA INSTITUTE OF TECHNOLOGY – PUNE
Department of SY Common

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.

You might also like