How to plot non-linear decision boundaries with a grid in R?

前端 未结 2 977
陌清茗
陌清茗 2021-02-06 17:36

I find this particular graph in ISLR (Figure 2.13) or ESL very well done. I can\'t guess how the authors would have made this in R. I know how to get the orange and blue points

2条回答
  •  盖世英雄少女心
    2021-02-06 17:55

    As I indicated in my comment, a solution was provided by @chl here on stats.stackexchange.com. Here it is, applied to your data set.

    library(class)
    set.seed(pi)
    X <- t(replicate(1000, runif(2)))
    g <- ifelse(apply(X, 1, sum) <= 1, 0, 1)
    xnew <- cbind(rep(seq(0, 1, length.out=50), 50),
                  rep(seq(0, 1, length.out=50), each=50))
    m <- knn(X, xnew, g, k=15, prob=TRUE)
    prob <- attr(m, "prob")
    prob <- ifelse(m=="1", prob, 1-prob)
    prob15 <- matrix(prob, 50)
    par(mar=rep(3, 4))
    contour(unique(xnew[, 1]), unique(xnew[, 2]), prob15, levels=0.5, 
            labels="", xlab='', ylab='', axes=FALSE, lwd=2.5, asp=1)
    title(xlab=expression(italic('X')[1]), ylab=expression(italic('X')[2]), 
          line=1, family='serif', cex.lab=1.5)
    points(X, bg=ifelse(g==1, "#CA002070", "#0571B070"), pch=21)
    gd <- expand.grid(x=unique(xnew[, 1]), y=unique(xnew[, 2]))
    points(gd, pch=20, cex=0.4, col=ifelse(prob15 > 0.5, "#CA0020", "#0571B0"))
    box()
    

    decision boundary

    (UPDATE: I changed the colour palette because the blue/yellow/purple thing was pretty hideous.)

提交回复
热议问题