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 the data to see how the sepal measurements differ between iris species. You can use the two columns containing sepal measurements.
clear load fisheriris X = meas(:,1:2); y = categorical(species); labels = categories(y); figure(1) gscatter(X(:,1), X(:,2), species,'rgb','osd'); xlabel('Sepal length'); ylabel('Sepal width');
classifier{1} = NaiveBayes.fit(X,y); classifier{2} = ClassificationDiscriminant.fit(X,y); classifier{3} = ClassificationTree.fit(X,y); classifier{4} = ClassificationKNN.fit(X,y); classifier_name = {'Naive Bayes','Discriminant Analysis','Classification Tree','Nearest Neighbor'};
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); figure(2) for ii = 1:numel(classifier) ypred = predict(classifier{ii},[xx1(:) xx2(:)]); h(ii) = subplot(2,2,ii); gscatter(xx1(:), xx2(:), ypred,'rgb'); title(classifier_name{ii},'FontSize',15) legend off, axis tight end legend(h(1), labels,'Location',[0.35,0.01,0.35,0.05],'Orientation','Horizontal')
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™ .