Skip to contents

The Visualizer1DModel and Visualizer2DModel class visualize the predictions of a model on tasks with 1 or 2 features. Let’s start with the boston_housing data set. The goal is to predict the median value of owner-occupied homes in $1000s. We subset the data set to only use the features age and rm. The age feature is the proportion of owner-occupied units built prior to 1940 and rm is the average number of rooms per dwelling.

task = tsk("boston_housing")
task$select(c("age", "rm"))

We load the support vector machine learner for regression.

learner = lrn("regr.svm")

Now we create a visualizer object with the as_visualizer() function.

vis = as_visualizer(task, learner)

First, the learner is trained on the entire task. After that a grid is created for the two features and the predictions of the model are computed for each grid point. The predictions are then visualized using a 3D scatter plot.

vis$plot()

We can also create a 2D contour plot for the predictions.

vis$init_layer_contour()
vis$plot()

To switch back to the 3D scatter plot initialize the scatter layer again.

vis$init_layer_surface()

Draw with contour lines above z dimension.

vis$init_layer_surface(show_contours = TRUE)
vis$plot()

We can add the training points to the plot.

vis$add_training_data()
vis$plot()

It is also possible to visualize classification tasks. We use the pima data set and impute the missing values. We select the features insulin and mass and train a support vector machine for classification.

task = tsk("pima")
task = po("imputemean")$train(list(task))[[1]]
task$select(c("insulin", "mass"))
learner = lrn("classif.svm", predict_type = "prob")

We create a visualizer object and plot the predictions. The z-axis shows the probability of the positive class.

vis = as_visualizer(task, learner)
vis$plot()

We can add the decision boundary to the plot.

vis$add_decision_boundary()
vis$plot()