#-*- R -*- library(MASS) library(class) library(nnet) Pop<-read.table("ex08.dat") names(Pop)<-c("x1","x2","y") #decrease len if you have little memory. decplot <- function(xp, yp, Z, t) { plot(Pop[, 1], Pop[, 2], xlim=c(-6.0,10), ylim=c(-6,10), type = "n", xlab = "x1", ylab = "x2") title(t) for(il in 1:2) { set <- Pop$y==levels(Pop$y)[il] text(Pop[set, 1], Pop[set, 2], labels = as.character(Pop$y[set]), col = 2 + il) } zp <- Z[, 1] - Z[, 2] contour(xp, yp, matrix(zp, np), add = T, levels = 0, labex = 0) invisible() } p <- as.matrix(Pop[, -3]) tp <- Pop$y xp <- seq(-6.0, 10.0, length = 10); np <- length(xp) yp <- seq(-6.0, 10.0, length = 10) pt <- expand.grid(x1 = xp, x2 = yp) par(mfcol=c(1,2)) Z <- knn(p, pt, tp, k = 1) decplot(xp, yp, class.ind(Z),"k=1") Z <- knn(p, pt, tp, k = 3) decplot(xp, yp, class.ind(Z),"k=3")