Last active
December 20, 2015 17:48
-
-
Save zjf/6171334 to your computer and use it in GitHub Desktop.
Lazy Man's SVM | add bias term and regulation term, based on http://www.weibo.com/1459604443/A3x1VtIQn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(1001) | |
x = rbind(cbind(rnorm(50, 1, 0.3), rnorm(50, 2, 0.3)), cbind(rnorm(50, 2, 0.3), rnorm(50, 1, 0.3))) | |
y = c(rep(1, 50), rep(-1, 50)) | |
h <- function(param, C = 1){ | |
b = param[length(param)] | |
w = param[-length(param)] | |
g = y * (x %*% w + b) | |
0.5 * w %*% w + C * sum(1 - g[g < 1]) | |
} | |
par(mfrow = c(2,2)) | |
regulation = c(1, 5, 10, 100) | |
for(i in 1:4){ | |
res = optim(c(0, 0, 0), h, C = regulation[i], method = "L-BFGS-B") | |
param = res$par | |
w = param[1:2] | |
b = param[3] | |
line.x = seq(min(x[,1]), max(x[,2]), 0.1) | |
line.y1 = (1 - line.x * w[1] - b) / w[2] | |
line.y2 = (-1 - line.x * w[1] - b) / w[2] | |
plot(x, col = y + 2, main = paste("C = ", regulation[i], sep = "")) | |
points(line.x, line.y1, type = "l") | |
points(line.x, line.y2, type = "l") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line.x = seq(min(x), max(x), 0.1)
不知道这样做line.x = seq(min(x[,1]), max(x[,1]), 0.1)
是否更为妥当?我已经fork了你的代码,我附带了一些注释,我的理解大概是如此的,盼指点~