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

前端 未结 2 979
陌清茗
陌清茗 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 18:04

    This was my silly attempt at approximation. Clearly the issues raised by @StephenKolassa are valid and not handled by this approximation.

    myCurve1 = function (x)
      abs(x[[1]] * sin(x[[1]]) + x[[2]] * sin(x[[2]]))
    myCurve2 = function (x)
      abs(x[[1]] * cos(x[[1]]) + x[[2]] * cos(x[[2]]))
    myCurve3 = function (x)
      abs(x[[1]] * tan(x[[1]]) + x[[2]] * tan(x[[2]]))
    
    tmp = function (myCurve, seed=99) {
      set.seed(seed)
      points = replicate(100, runif(2))
      colors = ifelse(apply(points, 2, myCurve) > 0.5, "orange", "blue")
      # Confound some
      swapInts = sample.int(length(colors), 6)
      for (i in swapInts) {
        if (colors[[i]] == "orange") {
          colors[[i]] = "blue"
        } else {
          colors[[i]] = "orange"
        }
      }
      gridPoints = seq(0, 1, 0.005)
      gridPoints = as.matrix(expand.grid(gridPoints, gridPoints))
      gridColors = vector("character", nrow(gridPoints))
      gridPch = vector("character", nrow(gridPoints))
      for (i in 1:nrow(gridPoints)) {
        val = myCurve(gridPoints[i, ])
        if (val > 0.505) {
          gridColors[[i]] = "orange"
          gridPch[[i]] = "."
        } else if (val < 0.495) {
          gridColors[[i]] = "blue"
          gridPch[[i]] = "."
        } else {
          gridColors[[i]] = "purple"
          gridPch[[i]] = "*"
        }
      }
      plot(x=gridPoints[ , 1], y=gridPoints[ , 2], col=gridColors, pch=gridPch)
      points(x=points[1, ], y=points[2, ], col=colors, lwd=2)
    }
    
    par(mfrow=c(1, 3))
    tmp(myCurve1)
    tmp(myCurve2)
    tmp(myCurve3)
    

    enter image description here

提交回复
热议问题