Decision Boundaries in SVM Multiclass Classification (fisheriris dataset)

7 ビュー (過去 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 Center および File ExchangeClassification Trees についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by