Drawing decision boundaries in R
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Decision-boundary plots help you see how a classifier splits feature space into predicted classes. In R, the standard workflow is to train a model on two numeric features, generate a dense grid of points across that plane, predict the class or probability for every grid point, and then plot the result with the original observations on top.
A Simple Logistic Regression Example
To keep the visualization readable, start with two features from the iris dataset:
This gives a binary classifier. The decision boundary is where the predicted probability is 0.5.
Build A Prediction Grid
Create a grid that spans the feature space:
The expand.grid call creates a dense set of points, and the predict call assigns model output to each one.
Plot The Boundary With ggplot2
Now draw the class regions and the separating line:
geom_tile colors the regions by predicted class, and geom_contour draws the actual boundary where the probability crosses 0.5.
Non-Linear Boundaries With SVM
For curved boundaries, use a non-linear model such as an SVM with a radial kernel:
The overall plotting pattern is the same. Only the model and prediction step change.
Keep The Problem Two-Dimensional
Decision boundaries are easiest to interpret in two dimensions. If your original model uses many features, choose two representative features for a visualization or reduce the data to two components before plotting. Otherwise, the plot becomes a projection rather than a literal picture of the model.
This is an important conceptual limit. A boundary plot is excellent for teaching, debugging, and exploratory work, but it does not always capture the full behavior of a high-dimensional model.
Common Pitfalls
The most common mistake is trying to plot boundaries for more than two predictors without deciding how to project the feature space. The resulting graphic often looks precise but hides too much of the model.
Another pitfall is forgetting to use the same preprocessing on the grid that you used during training. If you scaled or transformed the training data, apply that same transformation before predicting on grid points.
Grid resolution also matters. A coarse grid can make a smooth boundary look jagged or inaccurate. Increase length.out if the boundary appears blocky.
Finally, keep the class labels consistent. For binary models, be clear about which class corresponds to probability values above 0.5.
Summary
- Train a classifier on two numeric features when you want a clean decision-boundary plot.
- Use
expand.gridto create a dense mesh of input values. - Predict on the grid, then plot regions with
geom_tileand boundaries withgeom_contour. - The same approach works for linear and non-linear models.
- Be careful with preprocessing, grid resolution, and high-dimensional feature sets.
Related reading
- Drop a dimension of a tensor in Tensorflow
- Dropout behavior in Keras with rate1 dropping all input units not as expected
- Dropout layer before or after LSTM. What is the difference?
- Dummy variables when not all categories are present
- Drop all duplicate rows across multiple columns in Python Pandas
- Drop columns whose name contains a specific string from pandas DataFrame
- duplicate a tensorflow graph
- DuplicateFlagError when trying to train tensorflow object detection api on google collaboratory
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.