Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(e1071)
- #
- # Given a classifier (w, w0), visualizes the corresponding
- # linear functional using a filled contour plot.
- #
- plot.classifier = function(w, w0=0) {
- x1s = seq(-5, 5, 0.2)
- x2s = seq(-1, 1, 0.2)
- fvals = outer(x1s, x2s,
- Vectorize(function(x1, x2) { x1*w[1] + x2*w[2] + w0 }))
- image(x1s, x2s, fvals,
- col=terrain.colors(40), breaks=-20:20, asp=1,
- xlab=expression(X[1]), ylab=expression(X[2]))
- contour(x1s, x2s, fvals, levels=-40:40, add=T)
- arrows(0, 0, w[1], w[2], length=0.2, lwd=4)
- if (w[2] == 0) abline(v = -w0/w[1], lwd=4)
- else abline(-w0/w[2], -w[1]/w[2], lwd=4)
- }
- #
- # Plots the data created by the load.data function.
- # When add=T, the data is added to the current plot
- # cex specifies the size of the points.
- # Example:
- # d = load.data()
- # plot.classifier(w, w0)
- # plot(d, add=T)
- #
- plot.data = function(data, add=F, cex=3) {
- lbl = (data$y+1)/2 # {-1..1} -> {0..1}
- if (add)
- points(data$X[,1],data$X[,2], bg=lbl, pch=21+lbl, cex=cex)
- else
- plot(data$X[,1], data$X[,2], bg=lbl, pch=21+lbl, cex=cex, asp=1)
- text(data$X[,1], data$X[,2], col=(1-lbl), cex=0.2*cex)
- }
- load.data = function() {
- x1 = c(-1.5,-1,-0.5,3, 0.5,1,2,2,-3);
- x2 = c(0, 0, 0, 0);
- y = c(-1,-1,-1,-1,1,1,1,1,1);
- data = list(cbind(x1, x2), y)
- names(data) = c("X", "y")
- class(data) = "data"
- data
- }
- data = load.data()
- m = svm(data$X, data$y, scale=FALSE,
- kernel="linear", type="C-classification", cost=1)
- alphas = m$coefs
- w = t(m$SV)%*% alphas
- w0 = -m$rho
- plot.classifier(w, w0)
- plot.data(data, T)
- w
- alphas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement