Lesson 6 of 9
Visualizing Multi-Class Decision Boundaries & Model Capacity
Decision boundaries are the geometric hyper-surfaces separating predicted class regions in feature space. Visualizing these boundaries across algorithms reveals their underlying structural inductive bias: Logistic Regression produces straight linear hyperplanes; Decision Trees produce axis-aligned orthogonal step functions; KNN produces complex Voronoi tessellations; and RBF SVM produces smooth non-linear contours.
Decision Boundary Comparison Engine in 2D Feature Space
import numpy as np
import pandas as pd
from sklearn.datasets import make_moons
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# Generate non-linear intertwining half-moons
X, y = make_moons(n_samples=400, noise=0.2, random_state=42)
classifiers = {
'Logistic Regression (Linear)': LogisticRegression(),
'Decision Tree (Axis-Aligned)': DecisionTreeClassifier(max_depth=4, random_state=42),
'K-Nearest Neighbors (Voronoi)': KNeighborsClassifier(n_neighbors=7),
'RBF Kernel SVM (Smooth Non-Linear)': SVC(kernel='rbf', C=1.0, probability=True, random_state=42)
}
results = []
for name, clf in classifiers.items():
clf.fit(X, y)
acc = accuracy_score(y, clf.predict(X))
# Mathematical boundary characteristics
boundary_type = (
'Linear Hyperplane (w^T x = 0)' if 'Logistic' in name
else 'Orthogonal Axis-Aligned Rectangles' if 'Tree' in name
else 'Local Neighborhood Tessellation' if 'Neighbors' in name
else 'Non-Linear Smooth Manifold'
)
results.append({
'Classifier': name,
'Training Accuracy': f"{acc*100:.2f}%",
'Boundary Geometry': boundary_type
})
print("=== CLASSIFIER DECISION BOUNDARY CHARACTERISTICS ===")
print(pd.DataFrame(results).to_string())Pro Tip: Using `DecisionBoundaryDisplay` in Scikit-Learn
In modern Scikit-Learn (1.1+), use `sklearn.inspection.DecisionBoundaryDisplay.from_estimator(clf, X, response_method='predict', alpha=0.8)` to render publication-quality decision boundary plots with two lines of code.
MlDecisionBoundaries Visual Representation
Hands-On Challenge: Plot Decision Boundaries Across Algorithm Families
Using `DecisionBoundaryDisplay`, generate a 2x2 subplot comparing Logistic Regression, Decision Tree, KNN, and RBF SVM on the `make_moons` dataset. Visually inspect how each algorithm partitions the central overlap region.
Quick Quiz & Key Concept Checks
Common Pitfall: Projecting High-Dimensional Boundaries to 2D via PCA
Plotting decision boundaries in 2D using PCA projections can create the visual illusion of overlapping classes even if the classes are 100% linearly separable in the full 50-dimensional feature space. Be cautious when interpreting 2D projections of high-dimensional data.