SMO称选择第1个变量的过程为外层循环。外层循环在训练样本中选取违反KKT条件最严重的样本点,并将其对应的变量作为第1个变量$\alpha_1$。
2.第2个变量的选择
SMO称选择第2个变量的过程为内层循环。第2个变量选择的标准是希望能使$\alpha_2$有足够大的变化。
Matlab 二分类支持项
>> load fisheriris
inds = ~strcmp(species,\'setosa\');
X = meas(inds,3:4);
y = species(inds);
>> SVMModel = fitcsvm(X,y)
SVMModel =
ClassificationSVM
ResponseName: \'Y\'
CategoricalPredictors: []
ClassNames: {\'versicolor\' \'virginica\'}
ScoreTransform: \'none\'
NumObservations: 100
Alpha: [24×1 double]
Bias: -14.4149
KernelParameters: [1×1 struct]
BoxConstraints: [100×1 double]
ConvergenceInfo: [1×1 struct]
IsSupportVector: [100×1 logical]
Solver: \'SMO\'
>> sv = SVMModel.SupportVectors;
figure
gscatter(X(:,1),X(:,2),y)
hold on
plot(sv(:,1),sv(:,2),\'ko\',\'MarkerSize\',10)
legend(\'versicolor\',\'virginica\',\'Support Vector\')
hold off
支持向量
load fisheriris
X = meas(:,3:4);
Y = species;
figure
gscatter(X(:,1),X(:,2),Y);
h = gca;
lims = [h.XLim h.YLim]; % Extract the x and y axis limits
title(\'{\bf Scatter Diagram of Iris Measurements}\');
xlabel(\'Petal Length (cm)\');
ylabel(\'Petal Width (cm)\');
legend(\'Location\',\'Northwest\');
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);
figure
h(1:3) = gscatter(xGrid(:,1),xGrid(:,2),maxScore,...
[0.1 0.5 0.5; 0.5 0.1 0.5; 0.5 0.5 0.1]);
hold on
h(4:6) = gscatter(X(:,1),X(:,2),Y);
title(\'{\bf Iris Classification Regions}\');
xlabel(\'Petal Length (cm)\');
ylabel(\'Petal Width (cm)\');
legend(h,{\'setosa region\',\'versicolor region\',\'virginica region\',...
\'observed setosa\',\'observed versicolor\',\'observed virginica\'},...
\'Location\',\'Northwest\');
axis tight
hold off