K-Nearest Neighbour
Overview
Teaching: 20 min
Exercises: 0 minQuestions
How to use K-Nearest Neighbour in Machine Learning model
Objectives
Learn how to use KNN in ML model
13 K-Nearest Neighbour
- Simplicity but powerful and fast for certain task
- Work for both classification and regression
- Named as Instance Based Learning; Non-parametrics; Lazy learner
- Work well with small number of inputs

13.1 Explanation

- In KNN, the most important parameter is the K group and the distance computed between points.
- Euclide distance:

13.2 Implementation
library(caret)
data(iris)
set.seed(123)
indT <- createDataPartition(y=iris$Species,p=0.6,list=FALSE)
training <- iris[indT,]
testing <- iris[-indT,]
ModFit_KNN <- train(Species~.,training,method="knn",preProc=c("center","scale"),tuneLength=20)
ggplot(ModFit_KNN$results,aes(k,AccuracySD))+
geom_point(color="blue")+
labs(title=paste("Optimum K is ",ModFit_KNN$bestTune),
y="Error")
predict_KNN<- predict(ModFit_KNN,newdata=testing)
confusionMatrix(testing$Species,predict_KNN)

Key Points
KNN