Decision Boundaries in SVM Multiclass Classification (fisheriris dataset)
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/161887/image.png)
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);
Risposte (1)
Alain Kuchta
il 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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/173457/image.png)
Vedere anche
Categorie
Scopri di più su Classification Trees in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!