Decision Boundaries in SVM Multiclass Classification (fisheriris dataset)

조회 수: 11 (최근 30일)
Harry
Harry 2017년 3월 19일
댓글: Brendan Hamm 2019년 3월 11일
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);

답변 (1개)

Alain Kuchta
Alain Kuchta 2017년 3월 21일
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
Harry
Harry 2017년 3월 22일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Classification Trees에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by