Compute the Euclidean distance between all points Observation Y 1Blue 2 Yellow 2
ID: 3352817 • Letter: C
Question
Compute the Euclidean distance between all points
Observation Y 1Blue 2 Yellow 2 1 0 3 Yellow 01 4 Blue 5 Yellow1 1 1 6Yellow 030 0 2 0 (a) Compute the Euclidean distance between all points and a new observation at the origin (0, 0, 0) (b) Perform K-nearest neighbours classification for this new observation with K=1. What are the resulting classification probabilities at the origin? How would KNN classify the origin point? (c) Perform K-nearest neighbours classification for this new observation with K-3. What are the resulting classification probabilities at the origin? How would KNN classify the origin point?Explanation / Answer
USING R
1)Data Importihng in to Data frame
> Y=c("B" ,"Y" ,"Y" ,"B","Y","Y")
> X1=c(0,-2,-1,0,1,0)
> X2=c(2,-1,0,1,1,3)
> X3=c(0,0,1,3,1,0)
> D=data.frame(Y,X1,X2,X3)
> D
Y X1 X2 X3
1 B 0 2 0
2 Y -2 -1 0
3 Y -1 0 1
4 B 0 1 3
5 Y 1 1 1
6 Y 0 3 0
Then Normalize the data
> normalize=function(x){return((x-min(x))/(max(x)-min(x)))}
>
> d=as.data.frame(lapply(D[,c(2,3,4)],normalize))
> d
X1 X2 X3
1 0.6666667 0.75 0.0000000
2 0.0000000 0.00 0.0000000
3 0.3333333 0.25 0.3333333
4 0.6666667 0.50 1.0000000
5 1.0000000 0.50 0.3333333
6 0.6666667 1.00 0.0000000
Then we use lbarary class to find Knn model
> library(class)
> Train=d[1:6,]
> Train
X1 X2 X3
1 0.6666667 0.75 0.0000000
2 0.0000000 0.00 0.0000000
3 0.3333333 0.25 0.3333333
4 0.6666667 0.50 1.0000000
5 1.0000000 0.50 0.3333333
6 0.6666667 1.00 0.0000000
>
> Target=D[,1]
> Target
[1] B Y Y B Y Y
Levels: B Y
> test=data.frame(X1=0,X2=0,X3=0)
> test
X1 X2 X3
1 0 0 0
model 1)
> model=knn(train=Train ,test=test,cl=Target ,k=1)
> model
[1] Y
Levels: B Y
here model 1 predict the Yellow color
> model2=knn(train=Train ,test=test,cl=Target ,k=3)
> model2
[1] Y
Levels: B Y
here model 2 also predict the Yellow color
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.