library(class) library(nnet) TrainC<-read.table("trainC.dat") names(TrainC)<-c("x1","x2","y") K=11 p <- as.matrix(TrainC[, -3]) xp <- seq(min(TrainC$x1), max(TrainC$x1), length = 50); np <- length(xp) yp <- seq(min(TrainC$x2), max(TrainC$x2), length = 50) tp<-TrainC$y yhat <- knn(p, p, tp, k = K) par(mfcol=c(1,2)) plot(TrainC[, 1], TrainC[, 2], xlab = "x1", ylab = "x2", col=codes(TrainC$y)+1) pt <- expand.grid(x1 = xp, x2 = yp) Z <- knn(p, pt, tp, k = K) zp<-class.ind(Z)[,1] - class.ind(Z)[,2] contour(xp, yp, matrix(zp, np), add = T, levels = 0, labex = 0) CErr=0 for( i in 1:50 ) { if( yhat[i] == 'b' ) CErr = CErr + 1 } for( i in 51:100 ) { if( yhat[i] == 'a' ) CErr = CErr + 1 } title(paste("Knn(",K,") of TrainC")) mtext(paste("Plug-in Error=",format(100*CErr/100,digits=3),"%")) TestC <- read.table("TestC.dat") names(TestC)<-c("x1","x2") N <- length(TestC[,1]) yThat <- knn(p, TestC, tp, k = K) plot(TestC[, 1], TestC[, 2], xlab = "x1", ylab = "x2", col=codes(yThat)+1) title(paste("Knn(",K,") of TestC")) xp <- seq(min(TestC$x1), max(TestC$x1), length = 50); np <- length(xp) yp <- seq(min(TestC$x2), max(TestC$x2), length = 50) pt <- expand.grid(x1 = xp, x2 = yp) Z <- knn(p, pt, tp, k = K) zp<-class.ind(Z)[,1] - class.ind(Z)[,2] contour(xp, yp, matrix(zp, np), add = T, levels = 0, labex = 0) Result<-data.frame(TestC,yThat) write.table(Result,paste("TestC-C-KNN-",K,".dat"), quote=F, col.names=F, row.names=F)