Decision Boundaries in SVM Multiclass Classification (fisheriris dataset)
7 次查看(过去 30 天)
显示 更早的评论
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
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:
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Classification Trees 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!