0

I am using R to plot the "decision boundary surfaces" for different machine learning algorithms.

First, I simulated some data:

#load library
library(RSSL)

#generate data
d <- generateCrescentMoon(1000,2,1)

Then, I trained some different machine learning algorithms on this data:

#load library
library(mlr)

#specify data
aa = makeClassifTask(data = d, target = "Class")

#specify and train machine learning algorithms
learners = list(
    makeLearner("classif.svm", kernel = "linear"),
    makeLearner("classif.svm", kernel = "polynomial"),
    makeLearner("classif.svm", kernel = "radial"),
    "classif.rpart",
    "classif.randomForest",
    "classif.knn"
)

Now, when I decide to visualize the results:

 plotLearnerPrediction(learner = learners[[5]], task = aa)
 plotLearnerPrediction(learner = learners[[4]], task = aa)

enter image description here

For the plot on the left (rpart), can someone please help me understand the meaning of the "pale colored regions"? I understand that "blue" is supposed to be for the "triangle class" and "red" is supposed to be for the "circle class" - but what are the "pinkish" and "light blueish" regions? Are these supposed to represent overlapping areas?

Can someone please help me understand this?

1 Answer 1

2

From the documentation for plotLearnerPrediction:

...potentially through color alpha blending the posterior probabilities are shown.

...

prob.alpha
(logical(1)) For classification: Set alpha value of background to probability for predicted class? Allows visualization of “confidence” for prediction. If not, only a constant color is displayed in the background for the predicted label. Default is TRUE.

Your models are less certain about those regions, giving estimated probabilities further from 0 and 1.

For more details, it looks like we need to go into the source. There we find this snippet:

    if (taskdim == 2L) {
      p = ggplot(grid, aes_string(x = x1n, y = x2n))
      if (hasLearnerProperties(learner, "prob") && prob.alpha) {
        # max of rows is prob for selected class
        prob = apply(getPredictionProbabilities(pred.grid, cl = td$class.levels), 1, max)
        grid$.prob.pred.class = prob
        p = p + geom_raster(data = grid, mapping = aes_string(fill = target, alpha = ".prob.pred.class"),
          show.legend = TRUE) + scale_fill_discrete(drop = FALSE)
        p = p + scale_alpha(limits = range(grid$.prob.pred.class))
      } else {
        p = p + geom_raster(mapping = aes_string(fill = target))
      }
...

So the alpha value of the colors gets set according to the predicted probability for the winning class. If you really need to dig into how that's done, you'll need to understand ggplot much better than I do; for a start, see the docs pages for geom_raster, aes_string, and scale_alpha.

1
  • thank you for confirming this! do you have any more information behind how these paler colors are decided?
    – stats_noob
    Jun 18, 2021 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.