# # Given a classifier (w, w0), visualizes the corresponding # linear functional using a filled contour plot. # plot.classifier = function(w, w0=0) { x1s = seq(-1, 7, 0.2) x2s = seq(-1, 7, 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(2, 2, 2+w[1], 2+w[2], length=0.2, lwd=4) abline(-w0/w[2], -w[1]/w[2], lwd=4) } # # Creates the sample data used in the first exercises. # Example: # d = load.data() # plot(d) # d\$X # d\$y # load.data = function() { x1 = c(1, 1, 5, 5); x2 = c(1, 5, 1, 5); y = c(-1, 1, 1, 1); data = list(cbind(x1, x2), y) names(data) = c("X", "y") class(data) = "data" data } # # 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) } # # Given an SVM model that was trained on a data # object (the one provided by load.data) and the data # object itself, visualizes the corresponding functional # using a filled contour plot. # plot.svm.functional = function(model, data, title) { grid = expand.grid(x1=seq(0, 6, 0.1), x2=seq(0, 6, 0.1)) preds = attr(predict(model, grid, decision.values=TRUE), "decision.values") image(seq(0, 6, 0.1), seq(0, 6, 0.1), matrix(preds, nrow=61),asp=1, xlab=expression(X[1]), ylab=expression(X[2]), main=title) contour(seq(0, 6, 0.1), seq(0, 6, 0.1), matrix(preds, nrow=61), add=T) plot(data, add=T) }