inst/scripts/Nonlinear/kNN.R

## "INFOF422 Statistical foundations of machine learning" course
## R package gbcode 
## Author: G. Bontempi

KNN<- function(X,Y,k,q){
  l<-levels(Y)
  N<-nrow(X)
  
  d<-sqrt(apply((X-array(1,c(N,1))%*%q)^2,1,sum)) ## Euclidean metric
  ## d<-sqrt(apply(abs(X-array(1,c(N,1))%*%q),1,sum)) ## Manhattan metric
  ##  d<-1/cor(t(X),q)           ## correlation metric

  index<-sort(d,index.return=TRUE)
  cnt<-numeric(length(l))
  for (i in 1:k){
    cnt[Y[index$ix[i]]]<-cnt[Y[index$ix[i]]]+1
    
  }
  l[which.max(cnt)]
 
}
gbonte/gbcode documentation built on Feb. 27, 2024, 7:38 a.m.