plotDecisionBoundary <- function (theta, X, y) { # Plots the data points X and y into a new figure with # the decision boundary defined by theta # # plotDecisionBoundary(theta, X,y) plots the data points with + for the # positive examples and o for the negative examples. X is assumed to be # a either # 1) Mx3 matrix, where the first column is an all-ones column for the # intercept. # 2) MxN, N>3 matrix, where the first column is all-ones plot(X[,2:3], xlab = "", ylab = "", col = ifelse(y == 1, "gold", "black"), pch = ifelse(y == 1, 3, 1)) if(ncol(X) <= 3){ # Only need 2 points to define a line, so choose two endpoints plot_x = c(min(X[,2])-2, max(X[,2]+2)) # Calculate the decision boundary line plot_y = (-1/theta[3])*(theta[2]*plot_x + theta[1]) # Plot boundary lines(plot_x, plot_y, col = "red") # Axes & legend, specific for the exercise title(xlab = "Biomarker 1", ylab = "Biomarker 2") legend("topright", c("Malaria", "No"), col = c("gold", "black"), pch = c(3, 1)) } else { # Here is the grid range u <- v <- seq(-1, 1.5, 0.05) z <- matrix(0, nrow = length(u), ncol = length(v)) for (i in 1:length(u)){ for (j in 1:length(v)){ z[i,j] <- mapFeature(u[i], v[j])%*%theta } } contour(u, v, z, levels = 0, lwd = 2, add = TRUE, drawlabels = FALSE, col = "green") # Axis and legend, specific for the exercise title(main = paste("lambda = ",lambda), xlab = "Test 1", ylab = "Test 2") legend("topright", c("Affected", "No"), col = c("gold", "black"), pch = c(3, 1)) } }