Machine Learning with MATLAB

Visualize Decision Surfaces for Different Classifiers

Each classification algorithm generates different decision making rules. These rules can be visualized in the form of a decision surface. This example demonstrates visualizing the decision surface for different classification algorithms.

Load Data

Load the data to see how the sepal measurements differ between iris species. You can use the two columns containing sepal measurements.

load fisheriris
X = meas(:,1:2);
y = categorical(species);
labels = categories(y);

gscatter(X(:,1), X(:,2), species,'rgb','osd');
xlabel('Sepal length');
ylabel('Sepal width');

Train Four Different Classifiers and Store the Models in a Cell Array

classifier{1} =,y);
classifier{2} =,y);
classifier{3} =,y);
classifier{4} =,y);
classifier_name = {'Naive Bayes','Discriminant Analysis','Classification Tree','Nearest Neighbor'};

Predict Species Using All Classifiers

meshgrid is used to create a grid of points spanning the entire space within some bounds of the actual data values. Each classifier is used to classify all the data in the mesh.

[xx1, xx2] = meshgrid(4:.01:8,2:.01:4.5);

for ii = 1:numel(classifier)
   ypred = predict(classifier{ii},[xx1(:) xx2(:)]);

   h(ii) = subplot(2,2,ii);
   gscatter(xx1(:), xx2(:), ypred,'rgb');

   legend off, axis tight

legend(h(1), labels,'Location',[0.35,0.01,0.35,0.05],'Orientation','Horizontal')

Datasets and References

Fisher's iris data consists of measurements on the sepal length, sepal width, petal length, and petal width for 150 iris specimens. There are 50 specimens from each of three species. This dataset is shipped with the Statistics and Machine Learning Toolbox™ .