Classification in JavaScript

Classifier decision boundaries

Compare SVC, LDA, QDA, and eight ensemble classifiers on the same two-dimensional data. Add training points, adjust a model, and inspect held-out errors.

Free to useNo upload or sign-inRuns entirely in your browser
Live classification
Powered by @kanaries/ml
1
1

Switch between a linear and an RBF kernel on the moons to see how a margin can follow a curve.

Training accuracy 100% (64 points)Holdout accuracy 94% (16 points)
SVC decision regions for moons-2-20022Feature 1Feature 2

Class 0 Class 1Filled = training · Hollow = held out · × = missed

Add a training point

Choose a class and click the map, or enter coordinates. The holdout points stay fixed.

0 added
Enter coordinates

Read a decision boundary

Imagine a sensor that measures the vibration and temperature of a motor. Each labeled point records the two readings for a motor known to be healthy or faulty. For a new motor the classifier must assign a label using just those two numbers. A decision boundary answers a concrete question about that rule. If you start at one reading and change it slightly, how far can you move before the predicted condition changes?

In this playground the axes are synthetic features and the labels have no physical units. Pick a point in a region of one color and trace a horizontal path out of it. You are varying the first feature while holding the second fixed. Each color change means the fitted model changed its prediction. Look for a second crossing farther along the same path. Two crossings give you a simple test that the model has learned something a single straight separator could never represent.

Compare a line and a curve on the same observations

Start with the blobs and select linear discriminant analysis (LDA). The clouds can stretch in different directions, but LDA pools their within-class variation into one covariance estimate. Between two classes it draws a straight boundary. Switch to quadratic discriminant analysis (QDA) with the data unchanged. Each class can now have its own covariance. Follow the region where the long axis of one cloud approaches the other cloud. A curve can assign the ends of that cloud differently from a line through its middle.

Try adding a training point well away from the others in its class. Consider the effect on a class mean and the effect on the estimated spread as two separate questions. A point far from the mean can have appreciable influence on both. Increase QDA regularization and inspect the outer part of the boundary again. Regions with few observations give the models considerable freedom even if they agree near the centers of the two clouds.

Change the reach of an SVC kernel

Select two moons and a support vector classifier. A linear kernel can put one line through the gap between the arcs, but it cannot follow their full length. Change to the radial basis function (RBF) kernel. Its gamma parameter sets how quickly similarity decays with distance. At larger gamma the influence of a training example is more local. Check whether the outer tips of the moons fall in the expected region and whether isolated pockets form around individual observations.

Hold gamma fixed and vary C. This changes the penalty for margin violations during fitting. A larger C gives violations more weight, so the fitted separator may make room for observations that previously lay on the wrong side. Look at both training and held-out accuracy as you change it. An improvement on one or two ambiguous training cases can move the boundary into a region containing several held-out cases from the other class.

Compare trees that vote with trees that correct earlier predictions

Use XOR to examine a feature interaction. Points in opposite quadrants share a label, so the meaning of the first coordinate depends on the sign of the second one. Try shallow bagging trees, then allow another level of depth. Check all four quadrants and the area around the origin. Increasing tree depth lets a branch ask a second question after the first split. Increasing tree count instead changes how many sampled models contribute to a prediction.

With Extra Trees the candidate cuts themselves are randomized. On a fixed dataset, inspect whether adding trees changes the class in a broad region or only along its edges. Then switch to a boosting method. Boosting trains successive models using feedback from earlier predictions. Compare a short run with a longer one at the same learning rate and depth. AdaBoost gives more weight to misclassified observations. Gradient boosting and XGBoost fit trees using derivatives of a loss, while histogram boosting groups feature values into bins when it searches for cuts.

Inspect a disagreement before judging the model

Voting and stacking can combine models that draw different shapes around these same points. Hard voting counts predicted labels. Soft voting averages class probabilities, so changing that rule can move the boundary even when the individual models make the same predictions as before. Stacking trains a final classifier on member predictions produced for rows held out from each training fold. The fitted combination can favor the outputs of one member more strongly than those of another.

After comparing models, look for a held-out point near a boundary. Count how often it changes class as you adjust one setting, and check whether points far from the boundary ever change. A change of one held-out prediction can visibly move accuracy on a small sample. Repeated tuning to this same sample also lets its answers guide your choices. Use fresh observations to evaluate a model you chose through experimentation. For a real application, collect cases from the feature ranges where competing models disagree and where a mistake carries the greatest cost.

Frequently asked questions

Questions about classifier decision boundaries

What does the background color mean?

Each cell has the class predicted by the fitted model at its center. The place where neighboring cells change color approximates the decision boundary.

What happens when I add a labeled point?

The new point joins the training set. The model fits again and predicts the grid and the held-out observations, so you can measure the effect of the edit on the same test cases.

Why can two different boundaries have the same accuracy?

Holdout accuracy counts predictions at the held-out observations. Two classifiers can disagree in a large empty region yet assign the same label to every held-out point.

Powered by @kanaries/ml — the scikit-learn-style ML library for JavaScriptnpmGitHub