p1
This commit is contained in:
parent
2e2af889ba
commit
e9a0b13c09
5 changed files with 147 additions and 0 deletions
61
assignments/hwk04/HW4_Q1.m
Normal file
61
assignments/hwk04/HW4_Q1.m
Normal file
|
@ -0,0 +1,61 @@
|
|||
%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
|
19
assignments/hwk04/HW4_sol.typ
Normal file
19
assignments/hwk04/HW4_sol.typ
Normal file
|
@ -0,0 +1,19 @@
|
|||
== Problem 1
|
||||
|
||||
Results:
|
||||
|
||||
#image("prob1.png")
|
||||
|
||||
== Problem 2
|
||||
|
||||
Original:
|
||||
|
||||
$
|
||||
frac(1, 2) w^T S w - nu rho + sum_t C^t xi^t
|
||||
$
|
||||
|
||||
Find the Lagrangian first:
|
||||
|
||||
$
|
||||
frac(1, 2) w^T S w - nu rho + sum_t C^t xi^t
|
||||
$
|
37
assignments/hwk04/kernel_perceptron.m
Normal file
37
assignments/hwk04/kernel_perceptron.m
Normal file
|
@ -0,0 +1,37 @@
|
|||
|
||||
function [alphas, b] = kernel_perceptron(X,Y,d);
|
||||
|
||||
N = size(X,1);
|
||||
err = 1; %error rate
|
||||
max_iter = 10000; %maximum number of iterations
|
||||
alphas = zeros(N,1);
|
||||
b = 0;
|
||||
t = 1;
|
||||
|
||||
while t<=max_iter && err>0
|
||||
for ii = 1 : N %cycle through training set
|
||||
%TO DO: update alpha and b
|
||||
xt = X(ii, :);
|
||||
yt = Y(ii, :);
|
||||
|
||||
sum = 0;
|
||||
for i = 1 : N
|
||||
ai = alphas(i);
|
||||
yi = Y(i);
|
||||
xi = X(i, :);
|
||||
sum = sum + ai * yi * dot(xi, xt) ^ d;
|
||||
end
|
||||
sum = sum + b;
|
||||
|
||||
if sum * yt <= 0
|
||||
alphas(ii) = alphas(ii) + 1;
|
||||
b = b + yt;
|
||||
end
|
||||
|
||||
%*******************************
|
||||
end
|
||||
t = t+1;
|
||||
%TO DO: calculate the error rate
|
||||
|
||||
%*******************************
|
||||
end
|
30
assignments/hwk04/kp_predict.m
Normal file
30
assignments/hwk04/kp_predict.m
Normal file
|
@ -0,0 +1,30 @@
|
|||
function scores = kp_predict(alphas, b, d, X, Y, tst_data)
|
||||
% alphas, b: sample weights and bias term for classification
|
||||
% d: degree of polynomial kernel <x,y>^d
|
||||
% X,Y: training data and label
|
||||
% tst_data: test data for prediction
|
||||
% scores: the perdiction scores.
|
||||
|
||||
N = size(X,1);
|
||||
%TO DO: calculate the predictions
|
||||
|
||||
[tst_size, ~] = size(tst_data);
|
||||
scores = zeros(tst_size, 1);
|
||||
for t = 1 : tst_size
|
||||
xt = tst_data(t, :);
|
||||
|
||||
sum = 0;
|
||||
for i = 1 : N
|
||||
ai = alphas(i);
|
||||
yi = Y(i);
|
||||
xi = X(i, :);
|
||||
sum = sum + ai * yi * dot(xi, xt) ^ d;
|
||||
end
|
||||
sum = sum + b;
|
||||
|
||||
scores(t) = sign(sum);
|
||||
|
||||
end
|
||||
|
||||
|
||||
%********************************
|
BIN
assignments/hwk04/prob1.png
Normal file
BIN
assignments/hwk04/prob1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 37 KiB |
Loading…
Reference in a new issue