61 lines
2.1 KiB
Mathematica
61 lines
2.1 KiB
Mathematica
|
%generate two-class data
|
||
|
rng(1); % For reproducibility
|
||
|
N = 20;
|
||
|
r = sqrt(1*rand(N,1)); % Radius
|
||
|
t = 2*pi*rand(N,1); % Angle
|
||
|
data1 = [r.*cos(t), r.*sin(t)]; % Points
|
||
|
|
||
|
r2 = sqrt(8*rand(N,1)+1); % Radius
|
||
|
t2 = 2*pi*rand(N,1); % Angle
|
||
|
data2 = [r2.*cos(t2), r2.*sin(t2)]; % points
|
||
|
|
||
|
%combine the two class data
|
||
|
data3 = [data1;data2];
|
||
|
theclass = ones(2*N,1);
|
||
|
theclass(1:N) = -1;
|
||
|
figure;
|
||
|
|
||
|
for degree = [1 2 4]
|
||
|
%TO DO: replace fitcsvm with kernel_perceptron
|
||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||
|
%SVM classification demo;
|
||
|
% alphas = fitcsvm(data3,theclass,'KernelFunction','polynomial','PolynomialOrder',degree);%tune the sigma parameter here, 1, 3, 0.1
|
||
|
[alphas, b] = kernel_perceptron(data3, theclass, degree);
|
||
|
|
||
|
%[alphas,b] = kernel_perceptron(data3,theclass,degree);
|
||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||
|
|
||
|
d = 0.02;
|
||
|
[x1Grid,x2Grid] = meshgrid(min(data3(:,1)):d:max(data3(:,1)),...
|
||
|
min(data3(:,2)):d:max(data3(:,2)));
|
||
|
xGrid = [x1Grid(:),x2Grid(:)];
|
||
|
|
||
|
%TO DO: replace predict with kp_predict
|
||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||
|
%SVM prediction demo; replace with kernel perceptron
|
||
|
|
||
|
% [~,scores1] = predict(alphas,data3); %replace it with mypredict(...)
|
||
|
% [~,scores2] = predict(alphas,xGrid); %replace it with mypredict(...)
|
||
|
|
||
|
% scores1 = kp_predict(alphas, b, degree, data3, theclass, data3)
|
||
|
|
||
|
% scores2 = scores2(:,2);
|
||
|
% training_error = CalculateErrorRate(theclass,sign(scores1(:,2)));
|
||
|
|
||
|
scores1 = kp_predict(alphas,b,degree,data3,theclass,data3); %return the prediction scores
|
||
|
scores2 = kp_predict(alphas,b,degree,data3,theclass,xGrid); %return the prediction scores
|
||
|
training_error = CalculateErrorRate(theclass,sign(scores1));
|
||
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||
|
|
||
|
subplot(5,1,degree);
|
||
|
h(1:2) = gscatter(data3(:,1),data3(:,2),theclass,'rb','.');
|
||
|
hold on
|
||
|
contour(x1Grid,x2Grid,reshape(scores2,size(x1Grid)),[0 0],'k');
|
||
|
plottitle = sprintf("Degree = %d; Training Error = %f",degree,training_error);
|
||
|
title(plottitle);
|
||
|
end
|
||
|
|
||
|
function error_rate = CalculateErrorRate(y_pred,y_label)
|
||
|
N = length(y_label);
|
||
|
error_rate = sum(y_pred ~= y_label)/N;
|
||
|
end
|