Skip to content

Instantly share code, notes, and snippets.

@zjf
Last active December 20, 2015 17:48
Show Gist options
  • Save zjf/6171334 to your computer and use it in GitHub Desktop.
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
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")
}
@Puriney
Copy link

Puriney commented Aug 7, 2013

g = y * (x %*% w + b) 在加入soft margin理念之后,为什么g可以存在小于1? 支持向量的g应该等于1, 非支持向量的应该大于1. 小于1的,岂不是噪音数据?

@Puriney
Copy link

Puriney commented Aug 7, 2013

line.x = seq(min(x), max(x), 0.1) 不知道这样做line.x = seq(min(x[,1]), max(x[,1]), 0.1)是否更为妥当?我已经fork了你的代码,我附带了一些注释,我的理解大概是如此的,盼指点~

@zjf
Copy link
Author

zjf commented Aug 8, 2013

你的修改是对的,第一个comment已邮件回复。

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