#-*- R -*- library(MASS) library(class) library(nnet) K <- 10 FileName=paste("TestA-C-",K) #decrease len if you have little memory. decplot <- function(xp, yp, Z, t, Err=0, N=-1) { plot(Pop[, 1], Pop[, 2], xlab = "x1", ylab = "x2", col=Pop$y+1) title(paste(t,K)) if(N>0) mtext(paste("Error=",100*Err/N,"%")) zp <- Z[, 1] - Z[, 2] contour(xp, yp, matrix(zp, np), add = T, levels = 0, labex = 0) invisible() } TrainA1<-read.table("TrainA1.dat") names(TrainA1)<-c("x1","x2","y") p <- as.matrix(TrainA1[, -3]) xp <- seq(min(TrainA1$x1), max(TrainA1$x1), length = 50); np <- length(xp) yp <- seq(min(TrainA1$x2), max(TrainA1$x2), length = 50); pt <- expand.grid(x1 = xp, x2 = yp) par(mfcol=c(2,2)) tp <- TrainA1$y yhat <- knn(p, p, tp, k = K) CErr = 0 for( i in 1:50 ) { if( yhat[i] == 0 ) CErr = CErr + 1 } for( i in 51:100 ) { if( yhat[i] == 1 ) CErr = CErr + 1 } Z <- knn(p, pt, tp, k = K) Pop<-TrainA1 decplot(xp, yp, class.ind(Z),"TrainA1 k=", CErr, length(yhat)) TrainA2<-read.table("TrainA2.dat") names(TrainA2)<-c("x1","x2","y") p2 <- as.matrix(TrainA2[, -3]) xp <- seq(min(TrainA2$x1), max(TrainA2$x1), length = 50); np <- length(xp) yp <- seq(min(TrainA2$x2), max(TrainA2$x2), length = 50); pt <- expand.grid(x1 = xp, x2 = yp) yhat2 <- knn(p, p2, tp, k = K) CErr2 = 0 for( i in 1:50 ) { if( yhat2[i] == 0 ) CErr2 = CErr2 + 1 } for( i in 51:100 ) { if( yhat2[i] == 1 ) CErr2 = CErr2 + 1 } Pop<-TrainA2 Z <- knn(p, pt, tp, k = K) decplot(xp, yp, class.ind(Z),"TrainA2 k=", CErr2, length(yhat2)) TrainA12<-rbind(TrainA1,TrainA2) p12 <- as.matrix(TrainA12[, -3]) tp12 <- TrainA12$y yhat12 <- knn(p12, p12, tp12, k = K) CErr12 = 0 for( i in 1:50 ) { if( yhat12[i] == 0 ) CErr12 = CErr12 + 1 } for( i in 51:100 ) { if( yhat12[i] == 1 ) CErr12 = CErr12 + 1 } for( i in 100:150 ) { if( yhat12[i] == 0 ) CErr12 = CErr12 + 1 } for( i in 151:200 ) { if( yhat12[i] == 1 ) CErr12 = CErr12 + 1 } xp <- seq(min(TrainA12$x1), max(TrainA12$x1), length = 50); np <- length(xp) yp <- seq(min(TrainA12$x2), max(TrainA12$x2), length = 50); pt <- expand.grid(x1 = xp, x2 = yp) Pop<-TrainA2 Z <- knn(p12, pt, tp12, k = K) decplot(xp, yp, class.ind(Z),"TrainA1+A2 k=", CErr12, length(yhat12)) TestA<-read.table("TestA.dat") names(TestA)<-c("x1","x2") y <- knn(p12, TestA, tp12, k=K) write.table(data.frame(TestA,y), FileName, quote=F, col.name=F, row.name=F) Pop<-TestA Z <- knn(p12, pt, tp12, k = K) decplot(xp, yp, class.ind(Z),"TestA k=")