SlideShare a Scribd company logo
K Nearest Neighbours
Choosing the best K
#rstats
#ML
#Classification
In knn, choosing the best k value (nearest neighbors) is critical.
In this post, i write a function which can choose the best k from a range of k values e.g., a range
from 1 to 100.
This function above plots two graphs, one for percentage accuracy, and the other for percentage
error, and it returns a data frame containing the k values and their percentage accuracies and
errors.
The below image illustrates the plots, and we can see the best k is 1 with a accuracy of above
85%. You could make this plot interactive with ggplot2 or plotly packages.
The code is shown below:
analyze_k<-function(train, test, train_labels, test_labels, k_range)
{
# train - the training dataset
#test - the test dataset
#train_labels
#test_labels
#k_range - the max number of k values to
# use(should be numeric and greater than 0)
## the classification package
require(class)
prediction_table<-data.frame() # to store predicted classes
prediction_table[1:nrow(test), 1]<-seq(1, nrow(test), 1)
for (i in 1:k_range)
{ ### storing predicted-class columns
prediction_table[, i]<-knn(train, test, train_labels, k=i)
}
# a list of all tables comparing actual and predicted classes
tab_list<-list()
### storing crosstable lists for all values of k
for (i in 1:k_range)
{
tab_list[[i]]<-table(prediction_table[, i], test_labels)
}
l<-length(unique(train_labels))
sq<-seq(1, (l**2), l+1) # indexer sequence
# stores percentage accuracy and errors
d_f<-data.frame()
d_f[1:k_range, 1]<-1:k_range
# storing percentage accuracy and errors
for (i in 1:k_range)
{
d_f[i, 2]<-sum(tab_list[[i]][sq])
}
d_f[, 3]<-(d_f[, 2]/nrow(test))*100
d_f[, 4]<-100 - d_f[, 3]
colnames(d_f)<-c("k values", "index vector", "Percentage_accuracy",
"Percentage_error")
par(bg="black",mfrow=c(1, 2))
plot(d_f[, 1], d_f[, 3], type="l", xlab="k values", col="blue",
ylab="Percentage Accuracy", main="ACCURACY PLOT", col.main="white",
ylim=c(10, 100), lwd=2, col.axis="azure3", col.lab="azure3")
abline(h=max(d_f[, 3]), lty=1)
grid(,lty=1, col="wheat4")
plot(d_f[, 1], d_f[, 4], type="l", xlab="k values", col="red",
ylab="Percentage error",
main="ERROR PLOT",
col.main="white",
ylim=c(0, 100), lwd=2, col.axis="azure3", col.lab="azure3")
abline(h=min(d_f[, 4]), lty=1)
grid(,lty=1, col="wheat4")
analyze_k_table<-d_f[-2]
## returns an object of type dataframe containing k values and their
## respective accuracy and errors
return(analyze_k_table)
}

More Related Content

What's hot

Chapter 18,19
Chapter 18,19Chapter 18,19
Chapter 18,19
heba_ahmad
 
ECET350 Week 3 Homework Assignment
ECET350 Week 3 Homework AssignmentECET350 Week 3 Homework Assignment
ECET350 Week 3 Homework Assignment
BirleRubin
 
Anudbms
AnudbmsAnudbms
Anudbms
Anurag Maurya
 
3.3 the math object
3.3   the math object3.3   the math object
3.3 the math objectallenbailey
 
Mediation in R's lavaan package
Mediation in R's lavaan packageMediation in R's lavaan package
Mediation in R's lavaan package
George Mount
 
Bank loan purchase modeling
Bank loan purchase modelingBank loan purchase modeling
Bank loan purchase modeling
Saleesh Satheeshchandran
 
Identification des systémes dynamiques
Identification des systémes dynamiquesIdentification des systémes dynamiques
Identification des systémes dynamiques
Ayoub Moufid
 
Reliability problems with solutions
Reliability problems with solutionsReliability problems with solutions
Reliability problems with solutions
SOMNATHGANGULY11
 
Graph cut based discrete-valued image reconstruction
Graph cut based discrete-valued image reconstructionGraph cut based discrete-valued image reconstruction
Graph cut based discrete-valued image reconstruction
I3E Technologies
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Meghaj Mallick
 
Kajal Gaharwal , BCA Third Year
Kajal Gaharwal , BCA Third YearKajal Gaharwal , BCA Third Year
Kajal Gaharwal , BCA Third Year
Dezyneecole
 
Bt0082, visual basic
Bt0082, visual basicBt0082, visual basic
Bt0082, visual basic
smumbahelp
 
Linear & Non-Linear Systems |Solved problems|
Linear & Non-Linear Systems |Solved problems|Linear & Non-Linear Systems |Solved problems|
Linear & Non-Linear Systems |Solved problems|
National Engineering College
 
RBHF_SDM_2011_Jie
RBHF_SDM_2011_JieRBHF_SDM_2011_Jie
RBHF_SDM_2011_Jie
MDO_Lab
 

What's hot (17)

Chapter 18,19
Chapter 18,19Chapter 18,19
Chapter 18,19
 
ECET350 Week 3 Homework Assignment
ECET350 Week 3 Homework AssignmentECET350 Week 3 Homework Assignment
ECET350 Week 3 Homework Assignment
 
2 r algebra
2 r algebra2 r algebra
2 r algebra
 
Adv excel
Adv excelAdv excel
Adv excel
 
Anudbms
AnudbmsAnudbms
Anudbms
 
3.3 the math object
3.3   the math object3.3   the math object
3.3 the math object
 
Mediation in R's lavaan package
Mediation in R's lavaan packageMediation in R's lavaan package
Mediation in R's lavaan package
 
Bank loan purchase modeling
Bank loan purchase modelingBank loan purchase modeling
Bank loan purchase modeling
 
Identification des systémes dynamiques
Identification des systémes dynamiquesIdentification des systémes dynamiques
Identification des systémes dynamiques
 
Reliability problems with solutions
Reliability problems with solutionsReliability problems with solutions
Reliability problems with solutions
 
Graph cut based discrete-valued image reconstruction
Graph cut based discrete-valued image reconstructionGraph cut based discrete-valued image reconstruction
Graph cut based discrete-valued image reconstruction
 
Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...Reference Parameter, Passing object by reference, constant parameter & Defaul...
Reference Parameter, Passing object by reference, constant parameter & Defaul...
 
Kajal Gaharwal , BCA Third Year
Kajal Gaharwal , BCA Third YearKajal Gaharwal , BCA Third Year
Kajal Gaharwal , BCA Third Year
 
Bt0082, visual basic
Bt0082, visual basicBt0082, visual basic
Bt0082, visual basic
 
Linear & Non-Linear Systems |Solved problems|
Linear & Non-Linear Systems |Solved problems|Linear & Non-Linear Systems |Solved problems|
Linear & Non-Linear Systems |Solved problems|
 
FinalReportFoxMelle
FinalReportFoxMelleFinalReportFoxMelle
FinalReportFoxMelle
 
RBHF_SDM_2011_Jie
RBHF_SDM_2011_JieRBHF_SDM_2011_Jie
RBHF_SDM_2011_Jie
 

Similar to Finding the best K- Knn

working with python
working with pythonworking with python
working with python
bhavesh lande
 
Linear Regression (Machine Learning)
Linear Regression (Machine Learning)Linear Regression (Machine Learning)
Linear Regression (Machine Learning)
Omkar Rane
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functions
NIKET CHAURASIA
 
maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VII
Max Kleiner
 
Intro To C++ - Class 13 - Char, Switch, Break, Continue, Logical Operators
Intro To C++ - Class 13 - Char, Switch, Break, Continue, Logical OperatorsIntro To C++ - Class 13 - Char, Switch, Break, Continue, Logical Operators
Intro To C++ - Class 13 - Char, Switch, Break, Continue, Logical Operators
Blue Elephant Consulting
 
Lab 2: Classification and Regression Prediction Models, training and testing ...
Lab 2: Classification and Regression Prediction Models, training and testing ...Lab 2: Classification and Regression Prediction Models, training and testing ...
Lab 2: Classification and Regression Prediction Models, training and testing ...
Yao Yao
 
Machine Learning: Classification Concepts (Part 1)
Machine Learning: Classification Concepts (Part 1)Machine Learning: Classification Concepts (Part 1)
Machine Learning: Classification Concepts (Part 1)
Daniel Chan
 
Classification examp
Classification exampClassification examp
Classification exampRyan Hong
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming hccit
 
K Means Clustering in ML.pptx
K Means Clustering in ML.pptxK Means Clustering in ML.pptx
K Means Clustering in ML.pptx
Ramakrishna Reddy Bijjam
 
Computation Assignment Help
Computation Assignment Help Computation Assignment Help
Computation Assignment Help
Programming Homework Help
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Yao Yao
 
BPstudy sklearn 20180925
BPstudy sklearn 20180925BPstudy sklearn 20180925
BPstudy sklearn 20180925
Shintaro Fukushima
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3
Max Kleiner
 
Xgboost
XgboostXgboost
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
Matlab Assignment Experts
 
Ai_Project_report
Ai_Project_reportAi_Project_report
Ai_Project_reportRavi Gupta
 
Application of combined support vector machines in process fault diagnosis
Application of combined support vector machines in process fault diagnosisApplication of combined support vector machines in process fault diagnosis
Application of combined support vector machines in process fault diagnosis
Dr.Pooja Jain
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
KabilaArun
 

Similar to Finding the best K- Knn (20)

working with python
working with pythonworking with python
working with python
 
Linear Regression (Machine Learning)
Linear Regression (Machine Learning)Linear Regression (Machine Learning)
Linear Regression (Machine Learning)
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functions
 
maXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VIImaXbox starter69 Machine Learning VII
maXbox starter69 Machine Learning VII
 
Intro To C++ - Class 13 - Char, Switch, Break, Continue, Logical Operators
Intro To C++ - Class 13 - Char, Switch, Break, Continue, Logical OperatorsIntro To C++ - Class 13 - Char, Switch, Break, Continue, Logical Operators
Intro To C++ - Class 13 - Char, Switch, Break, Continue, Logical Operators
 
Lab 2: Classification and Regression Prediction Models, training and testing ...
Lab 2: Classification and Regression Prediction Models, training and testing ...Lab 2: Classification and Regression Prediction Models, training and testing ...
Lab 2: Classification and Regression Prediction Models, training and testing ...
 
Survey Demo
Survey DemoSurvey Demo
Survey Demo
 
Machine Learning: Classification Concepts (Part 1)
Machine Learning: Classification Concepts (Part 1)Machine Learning: Classification Concepts (Part 1)
Machine Learning: Classification Concepts (Part 1)
 
Classification examp
Classification exampClassification examp
Classification examp
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
 
K Means Clustering in ML.pptx
K Means Clustering in ML.pptxK Means Clustering in ML.pptx
K Means Clustering in ML.pptx
 
Computation Assignment Help
Computation Assignment Help Computation Assignment Help
Computation Assignment Help
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
 
BPstudy sklearn 20180925
BPstudy sklearn 20180925BPstudy sklearn 20180925
BPstudy sklearn 20180925
 
maXbox starter65 machinelearning3
maXbox starter65 machinelearning3maXbox starter65 machinelearning3
maXbox starter65 machinelearning3
 
Xgboost
XgboostXgboost
Xgboost
 
Mechanical Engineering Homework Help
Mechanical Engineering Homework HelpMechanical Engineering Homework Help
Mechanical Engineering Homework Help
 
Ai_Project_report
Ai_Project_reportAi_Project_report
Ai_Project_report
 
Application of combined support vector machines in process fault diagnosis
Application of combined support vector machines in process fault diagnosisApplication of combined support vector machines in process fault diagnosis
Application of combined support vector machines in process fault diagnosis
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 

Recently uploaded

一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
ahzuo
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
Tiktokethiodaily
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
benishzehra469
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
ewymefz
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
balafet
 

Recently uploaded (20)

一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
一比一原版(UIUC毕业证)伊利诺伊大学|厄巴纳-香槟分校毕业证如何办理
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
 

Finding the best K- Knn

  • 1. K Nearest Neighbours Choosing the best K #rstats #ML #Classification In knn, choosing the best k value (nearest neighbors) is critical. In this post, i write a function which can choose the best k from a range of k values e.g., a range from 1 to 100. This function above plots two graphs, one for percentage accuracy, and the other for percentage error, and it returns a data frame containing the k values and their percentage accuracies and errors. The below image illustrates the plots, and we can see the best k is 1 with a accuracy of above 85%. You could make this plot interactive with ggplot2 or plotly packages.
  • 2. The code is shown below: analyze_k<-function(train, test, train_labels, test_labels, k_range) { # train - the training dataset #test - the test dataset #train_labels #test_labels #k_range - the max number of k values to # use(should be numeric and greater than 0) ## the classification package require(class) prediction_table<-data.frame() # to store predicted classes prediction_table[1:nrow(test), 1]<-seq(1, nrow(test), 1) for (i in 1:k_range) { ### storing predicted-class columns prediction_table[, i]<-knn(train, test, train_labels, k=i) } # a list of all tables comparing actual and predicted classes tab_list<-list() ### storing crosstable lists for all values of k for (i in 1:k_range) { tab_list[[i]]<-table(prediction_table[, i], test_labels) } l<-length(unique(train_labels)) sq<-seq(1, (l**2), l+1) # indexer sequence # stores percentage accuracy and errors d_f<-data.frame() d_f[1:k_range, 1]<-1:k_range # storing percentage accuracy and errors for (i in 1:k_range) { d_f[i, 2]<-sum(tab_list[[i]][sq]) } d_f[, 3]<-(d_f[, 2]/nrow(test))*100 d_f[, 4]<-100 - d_f[, 3] colnames(d_f)<-c("k values", "index vector", "Percentage_accuracy", "Percentage_error") par(bg="black",mfrow=c(1, 2)) plot(d_f[, 1], d_f[, 3], type="l", xlab="k values", col="blue", ylab="Percentage Accuracy", main="ACCURACY PLOT", col.main="white", ylim=c(10, 100), lwd=2, col.axis="azure3", col.lab="azure3") abline(h=max(d_f[, 3]), lty=1) grid(,lty=1, col="wheat4")
  • 3. plot(d_f[, 1], d_f[, 4], type="l", xlab="k values", col="red", ylab="Percentage error", main="ERROR PLOT", col.main="white", ylim=c(0, 100), lwd=2, col.axis="azure3", col.lab="azure3") abline(h=min(d_f[, 4]), lty=1) grid(,lty=1, col="wheat4") analyze_k_table<-d_f[-2] ## returns an object of type dataframe containing k values and their ## respective accuracy and errors return(analyze_k_table) }