Decision Boundaries in SVM Multiclass Classification (fisheriris dataset)

19 views (last 30 days)
I would like to find (plot) the linear SVM decision boundaries in the fisher iris dataset.
Is there any short way of doing that?
The features can be PetalWidth (y-axis) and PetalLength (x-axis).
function [trainedClassifier, validationAccuracy] = trainClassifier(trainingData)
inputTable = trainingData;
predictorNames = {'SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'};
predictors = inputTable(:, predictorNames);
response = inputTable.Species;
isCategoricalPredictor = [false, false, false, false];
template = templateSVM(...
'KernelFunction', 'linear', ...
'PolynomialOrder', [], ...
'KernelScale', 'auto', ...
'BoxConstraint', 1, ...
'Standardize', true);
classificationSVM = fitcecoc(...
predictors, ...
response, ...
Features,...
Labels, 'Learners', template, ...
'Coding', 'onevsone', ...
'ClassNames', {'setosa'; 'versicolor'; 'virginica'});
predictorExtractionFcn = @(t) t(:, predictorNames);
svmPredictFcn = @(x) predict(classificationSVM, x);
trainedClassifier.predictFcn = @(x) svmPredictFcn(predictorExtractionFcn(x));
inputTable = trainingData;
predictorNames = {'SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth'};
predictors = inputTable(:, predictorNames);
response = inputTable.Species;
isCategoricalPredictor = [false, false, false, false];
partitionedModel = crossval(trainedClassifier.ClassificationSVM, 'KFold', 5);
validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');
[validationPredictions, validationScores] = kfoldPredict(partitionedModel);

Answers (1)

Alain Kuchta
Alain Kuchta on 21 Mar 2017
I understand that you want to plot the linear SVM decision boundaries of a ClassificationPartitionedECOC ( partitionedModel in your code).
The general process is to create a mesh grid for the entire area of the coordinate space visible. Then, use each individual
linear SVM to classify all of the points in the mesh grid. Finally draw a contour for each SVM from the classification scores. By limiting the contour plot to just one contour line, it will show the decision boundary of the SVM.
The individual SVMs can be located as follows:
>> Mdl = fitcecoc(X,Y,'Learners',t, ...);
>> CVMdl = crossval(Mdl, 'Kfold', 5, ...);
>> CVMdl.Trained{1}.BinaryLearners{j}
ans =
classreg.learning.classif.CompactClassificationSVM
ResponseName: 'Y'
CategoricalPredictors: []
ClassNames: [-1 1]
...
They can be used to classify data with the predict function:
[~, gridScores] = predict(CVMdl.Trained{i}.BinaryLearners{j}, myGrid);
Finally the grid scores can be plotted as a contour:
contour(gridX, gridY, gridScores, [0 0])
You may want to experiment with different line styles to distinguish the decision boundaries from different SVMs:
  1 Comment
Harry
Harry on 22 Mar 2017
Thanks for your response.
I'm a bit confused about the result.
Shouldn't it look like this (or similar):
I would like to see only the final boundaries that were used for 3 sets of data.
If that can be done.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!