Function Repository Resource:

DecisionBoundaryPlot

Source Notebook

Plot the decision boundaries of a classifier

Contributed by: Felipe Amorim

ResourceFunction["DecisionBoundaryPlot"][data]

plots the decision boundaries of a classifier trained on data.

ResourceFunction["DecisionBoundaryPlot"][data, classifier]

plots the decision boundaries of a pre-trained ClassifierFunction classifier trained on data.

Details and Options

A decision boundary is the dividing line or hypersurface that separates different classes in the input data based on a classifier's predictions.
ResourceFunction["DecisionBoundaryPlot"] only handles the case of two dimensional data (so boundaries are lines). Moreover data must be labeled. It can be provided as an Association, a single Rule or a List of rules.
ResourceFunction["DecisionBoundaryPlot"] supports all options of ArrayPlot, along with the following additional options:
MethodAutomaticclassification algorithm used to classify the data
"DataColors"ColorData[97,"ColorList"]color scheme representing data classes
PointSizeLargesize of points representing the data
ResourceFunction["DecisionBoundaryPlot"] takes the same Method options as supported by Classify.
The "DataColors" option can take any list of colors having sufficiently many to cover each data class.

Examples

Basic Examples (2) 

Generate labeled 2D data:

In[1]:=
data = <|-1 -> RandomReal[{-2, -1}, {4, 2}], 1 -> RandomReal[{1, 2}, {4, 2}]|>
Out[1]=

Plot their classification regions:

In[2]:=
ResourceFunction["DecisionBoundaryPlot"][data]
Out[2]=

Generate labeled 2D data with three class labels:

In[3]:=
data = <|-1 -> RandomReal[{-2, -1}, {4, 2}], 0 -> RandomReal[{-1, 1}, {4, 2}], 1 -> RandomReal[{1, 2}, {4, 2}]|>
Out[3]=

Plot their classification boundaries:

In[4]:=
ResourceFunction["DecisionBoundaryPlot"][data]
Out[4]=

Scope (3) 

Plot the classification boundary of data as a List of rules:

In[5]:=
ResourceFunction[
 "DecisionBoundaryPlot"][{{1, 4} -> "A", {0.5, 1} -> "A", {4, 3.5} -> "B", {5, 4} -> "B", {3, 1.5} -> "B"}]
Out[5]=

Plot the classification boundary of data as a Rule between points and classes:

In[6]:=
ResourceFunction[
 "DecisionBoundaryPlot"][{{1, 4}, {0.5, 1}, {4, 3.5}, {5, 4}, {3, 1.5}} -> {"A", "A", "B", "B", "B"}]
Out[6]=

Generate clusters of 2D data:

In[7]:=
cluster1 = RandomVariate[MultinormalDistribution[{0, 0}, IdentityMatrix[2]], 200];
cluster2 = MapThread[{#1 Cos[#2], #1 Sin[#2]} &, {RandomReal[{4, 5}, 200], RandomReal[{0, 2 Pi}, 200]}];
cluster3 = MapThread[{#1 Cos[#2], #1 Sin[#2]} &, {RandomReal[{6, 7}, 200], RandomReal[{0, 2 Pi}, 200]}];
data = <|1 -> cluster1, 2 -> cluster2, 3 -> cluster3|>;

Classify the data:

In[8]:=
cl = Classify[data]
Out[8]=

Plot the decision regions:

In[9]:=
ResourceFunction["DecisionBoundaryPlot"][data, cl]
Out[9]=

Options (3) 

Generate data and use the Method option to specify the classification algorithm used to classify the data:

In[10]:=
data = <|-1 -> RandomReal[{-2, -1}, {4, 2}], 0 -> RandomReal[{-1, 1}, {4, 2}], 1 -> RandomReal[{1, 2}, {4, 2}]|>
Out[10]=
In[11]:=
ResourceFunction["DecisionBoundaryPlot"][data, Method -> "DecisionTree"]
Out[11]=

Compare it with a different classification method:

In[12]:=
ResourceFunction["DecisionBoundaryPlot"][data, Method -> "SupportVectorMachine"]
Out[12]=

Change the color of each class using the "DataColors" option:

In[13]:=
data = <|-1 -> RandomReal[{-2, -1}, {4, 2}], 0 -> RandomReal[{-1, 1}, {4, 2}], 1 -> RandomReal[{1, 2}, {4, 2}]|>;
In[14]:=
ResourceFunction["DecisionBoundaryPlot"][data, "DataColors" -> {Orange, Cyan, Purple}]
Out[14]=

Change the size of data points with the PointSize option:

In[15]:=
data = <|-1 -> RandomReal[{-2, -1}, {10, 2}], 0 -> RandomReal[{-1, 1}, {10, 2}], 1 -> RandomReal[{1, 2}, {10, 2}]|>;
In[16]:=
ResourceFunction["DecisionBoundaryPlot"][data, PointSize -> 0.1]
Out[16]=

Applications (2) 

Generate data and compare different classification algorithms:

In[17]:=
data = Flatten[Table[Module[{\[Mu], \[Sigma], pts},
     \[Mu] = RandomReal[{-3, 3}, 2];
     \[Sigma] = 0.2;
     pts = RandomVariate[
       MultinormalDistribution[\[Mu], \[Sigma] IdentityMatrix[2]], 10];
     Rule @@@ Thread[pts -> ConstantArray[class, Length[pts]]]], {class, {"A",
       "B", "C"}}], 1];
In[18]:=
With[{methods = {"LogisticRegression", "NeuralNetwork", "RandomForest",
     "SupportVectorMachine"}}, GraphicsGrid[
  Partition[
   Table[ResourceFunction["DecisionBoundaryPlot"][data, Method -> method, PlotLabel -> method], {method, methods}], 2]]]
Out[18]=

Each classification method accepts its own suboptions. Use the Method option to specify them and visualize how the classification regions change:

In[19]:=
cluster1 = RandomVariate[MultinormalDistribution[{0, 0}, IdentityMatrix[2]], 200];
cluster2 = MapThread[{#1 Cos[#2], #1 Sin[#2]} &, {RandomReal[{4, 5}, 200], RandomReal[{0, 2 Pi}, 200]}];
data = <|1 -> cluster1, 2 -> cluster2|>;
In[20]:=
With[{kernelTypes = {"Linear", "RadialBasisFunction"}}, GraphicsGrid[{Table[
    ResourceFunction["DecisionBoundaryPlot"][data, Method -> {"SupportVectorMachine", "KernelType" -> kernelType}, PlotLabel -> "SVM, " <> kernelType <> " kernel"], {kernelType, kernelTypes}]}]]
Out[20]=

Possible Issues (2) 

The training data must be two dimensional:

In[21]:=
ResourceFunction[
 "DecisionBoundaryPlot"][{{1, 1, 1} -> "A", {2, 2, 2} -> "B"}]
Out[21]=

The training data must be labeled and formatted correctly:

In[22]:=
ResourceFunction["DecisionBoundaryPlot"][{{1, 1}, {2, 2}}]
Out[22]=

Neat Examples (2) 

Generate data for four different classes:

In[23]:=
data = Flatten[Table[Module[{\[Mu], \[Sigma], pts},
     \[Mu] = RandomReal[{-3, 3}, 2];
     \[Sigma] = 0.2;
     pts = RandomVariate[
       MultinormalDistribution[\[Mu], \[Sigma] IdentityMatrix[2]], 15];
     Rule @@@ Thread[pts -> ConstantArray[class, Length[pts]]]], {class, {"A",
       "B", "C", "D"}}], 1];

Specify different "GammaScalingParameter" values in the "SupportVectorMachine" classifier and notice how it overfits the data as this parameter increases:

In[24]:=
With[{gammaValues = {0.1, 1, 10, 100}}, GraphicsGrid[
  Partition[
   Table[ResourceFunction["DecisionBoundaryPlot"][data, Method -> {"SupportVectorMachine", "KernelType" -> "RadialBasisFunction", "GammaScalingParameter" -> gamma}, PlotLabel -> \[Gamma] == gamma], {gamma, gammaValues}], 2]]]
Out[24]=

Publisher

Felipe Amorim

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.0.0 – 12 May 2025

Related Resources

License Information