Multiclass SVM using fitcsvm

4 次查看(过去 30 天)
Tabassum
Tabassum 2021-3-19
load fisheriris
X = meas(:,3:4);
Y = species;
SVMModels = cell(3,1);
classes = unique(Y);
rng(1); % For reproducibility
for j = 1:numel(classes);
indx = strcmp(Y,classes(j)); % Create binary classes for each classifier
SVMModels{j} = fitcsvm(X,indx,'ClassNames',[false true],'Standardize',true,...
'KernelFunction','rbf','BoxConstraint',1);
end
d = 0.02;
[x1Grid,x2Grid] = meshgrid(min(X(:,1)):d:max(X(:,1)),...
min(X(:,2)):d:max(X(:,2)));
xGrid = [x1Grid(:),x2Grid(:)];
N = size(xGrid,1);
Scores = zeros(N,numel(classes));
for j = 1:numel(classes);
[~,score] = predict(SVMModels{j},xGrid);
Scores(:,j) = score(:,2); % Second column contains positive-class scores
end
[~,maxScore] = max(Scores,[],2);
Here in this example, 2 out of 4 features are considered. It shows error while considering all four features. If I want to cosider all four features, what would change in the coding?

回答(1 个)

Ayush Aniket
Ayush Aniket 2024-6-14
Hi Tabassum,
The error you are encountering comes from the fact the SVM model is trained on two features i.e. two columns of meas data:
X = meas(:,3:4);
; hence it expects the same number of features (dimension of xGrid) when predict function is called.
The latter part of the code computes the prediction of the trained SVM model on a grid covering the 2D feature space of petal length and width, and then determines the winning class for each point by selecting the model with the highest score, probably for visualising the decision boundary.
If you want to use all of the features, the model needs to be trained on all columns of X.
X = meas;
You could create a 4D grid and compute scores on all points in the 4D feature space, but you wont be able to visualize the decision boundary on a plot. For creating a 4D grid, you can use the ndgrid function. Refer to the following link: https://www.mathworks.com/help/matlab/ref/ndgrid.html

类别

Help CenterFile Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by