% Support vector machine (SVM) template file
% Edited by Yafeng Pan, 2016/1/7
% Example: X是特征（有2个特征），由xdata1和xdata2两批样本组成（各50个样本），ylabel标示了两类样本



% A little clean work
clc;clear;close all;format compact;

%=======================================#
%  Generate features (Need to modify)   /
%=======================================#
load fisheriris
xdata1 = [meas(51:100,3) meas(51:100,4)];
xdata2 = [meas(101:150,3) meas(101:150,4)]

% Put the data in one matrix and make a vector of classifications
X = [xdata1;xdata2];
ylabel = ones(100,1);
ylabel(51:end) = -1;


%===========================#
%  Visualization of data    /
%===========================#
figure;
gscatter (X(:,1),X(:,2),ylabel);
title ('Scatter Diagram of Data')

%===========================#
%  Train the SVM classifier /
%===========================#
% SVMModel = fitcsvm (X,ylabel,'KernelFunction','rbf',...
%     'BoxConstraint',Inf,'ClassNames',[-1,1]);

SVMModel = fitcsvm (X,ylabel);%默认算法是linear

%===============================#
%  Predict scores over the grid /
%===============================#
d = 0.02;
[x1Grid,x2Grid] = meshgrid(min(X(:,1)):d:max(X(:,1)),...
    min(X(:,2)):d:max(X(:,2)));
xGrid = [x1Grid(:),x2Grid(:)];
[~,scores] = predict(SVMModel,xGrid);

%==========================================#
%  Plot the data and the decision boundary /
%==========================================#
figure;
h(1:2) = gscatter(X(:,1),X(:,2),ylabel);
hold on
ezpolar(@(x)1);
h(3) = plot(X(SVMModel.IsSupportVector,1),X(SVMModel.IsSupportVector,2),'ko','MarkerSize',10);
contour (x1Grid,x2Grid,reshape(scores(:,2),size(x1Grid)),[0 0],'k');
legend(h,{'-1','+1','Support Vectors'},'Location','Best');
axis equal
hold off

%==============================================================================#
%  Plot posterior probability regions for SVM classification models (optional) /
%==============================================================================#
rng(1);% For reproducibility
[SVMModel1,ScoreParameters] = fitPosterior(SVMModel);

xMax = max(X);
xMin = min(X);
d = 0.01;
[x1Grid1,x2Grid2] = meshgrid(xMin(1):d:xMax(1),xMin(2):d:xMax(2));
[~,PosteriorRegion] = predict(SVMModel1,[x1Grid1(:),x2Grid2(:)]);

figure;
contourf(x1Grid1,x2Grid2,...
    reshape(PosteriorRegion(:,2),size(x1Grid1,1),size(x1Grid1,2)));
k = colorbar;
k.Label.string = 'P({\it{versicolor}})';
k.Ylabel.Fontsize = 16;
caxis([0 1]);

hold on 
gscatter (X(:,1),X(:,2),ylabel,'mc','.x',[15,10]);
sv = X(SVMModel.IsSupportVector,:);
plot(sv(:,1),sv(:,2),'yo','MarkerSize',15,'LineWidth',2);
axis tight
hold off


%=================================#
%  10-fold Cross Validation (CV) /
%=================================#
CVSVMModel = crossval(SVMModel);
misclass = kfoldLoss (CVSVMModel);
Accuracy = 1 - misclass;

disp('==========================================')
disp(['%%%%%%  Prediction Accuracy = ' num2str(Accuracy) '  %%%%%%'])
disp('==========================================')

disp('==========SVM_ANALYSIS_TEMPLATE SUCCESSFULLY FINISHED ==============')










