Skip to content

Instantly share code, notes, and snippets.

@Kornel
Created March 25, 2016 09:50
Show Gist options
  • Save Kornel/e4f4ecb13d502af5cfd8 to your computer and use it in GitHub Desktop.
Save Kornel/e4f4ecb13d502af5cfd8 to your computer and use it in GitHub Desktop.
Decision boundary for MLP
set.seed(1)
pts <- seq(0, 360, 10)
x1 <- c(sapply(pts, function(x) cos(x)),
sapply(pts, function(x) cos(x) * 4),
sapply(pts, function(x) cos(x) * 6))
x2 <- c(sapply(pts, function(x) sin(x)),
sapply(pts, function(x) sin(x) * 4),
sapply(pts, function(x) sin(x) * 6))
data <- data.frame(x1, x2)
data$y <- as.factor(c(rep(0, length(pts)), rep(1, length(pts)), rep(2, length(pts))))
fit <- nnet(y ~ ., data, size = 40)
library(ggplot2)
py <- seq(-10, 10, 0.1)
px <- seq(-10, 10, 0.1)
grid <- expand.grid(px, py)
colnames(grid) <- c('x1', 'x2')
grid$pred <- as.numeric(predict(fit, grid, type = 'class'))
colnames(grid) <- c('x', 'y', 'z')
ggplot() +
stat_contour(data = grid, aes(x = x, y = y, z = z), alpha = 0.9) +
geom_point(data = data, aes(x = x1, y = x2, color = y)) +
coord_fixed() +
theme_bw() +
xlab('x1') +
ylab('x2')
@Kornel
Copy link
Author

Kornel commented Mar 25, 2016

rplot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment