Created
March 12, 2016 15:48
-
-
Save Momoumar/6bbe5cb9252a46c26616 to your computer and use it in GitHub Desktop.
linear kernel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%-----------------------------a)----------------------------- | |
data = parseFile('train.data'); | |
m = size(data,1); | |
X = data(:,1:1558); | |
X = str2double(X); | |
yTemp = data(:,1559); | |
y = ones(size(yTemp,1),1); | |
n = size(X,2); | |
% Converting nonad -> -1 and ad -> 1 | |
for i= 1:length(yTemp) | |
string = yTemp{i}; | |
if strcmp(string,'nonad.') == 0 | |
y(i,1) = -1; | |
else | |
y(i,1) = 1; | |
end | |
end | |
Q = (y*y').*(X*X'); | |
% cvx optimization | |
cvx_begin | |
variable alfa(m); | |
minimize( 0.5*alfa'*Q*alfa + (-1).*sum(alfa)); | |
subject to | |
alfa'*y==0; | |
alfa >= zeros(m,1); | |
alfa <= ones(m,1); | |
cvx_end | |
alpha_cvx = alfa; | |
%------------------------------------------------------------% | |
%-----------------------------b)-----------------------------% | |
% calculation of w | |
w = zeros(1,n); | |
for i=1:m | |
w = w + alfa(i) * y(i) .* X(i,:); | |
end | |
% calculation of b | |
indYminus = find(y == -1); | |
indYplus = find(y == 1); | |
setYMinus = zeros(1,length(indYminus)); | |
setYPlus = zeros(1,length(indYplus)); | |
for i=1:length(indYminus) | |
setYMinus(i) = w * X(indYminus(i),:)'; | |
end | |
for i=1:length(indYplus) | |
setYPlus(i) = w * X(indYplus(i),:)'; | |
end | |
b = -(max(setYMinus) + min(setYPlus))/2; | |
% Finding the accuracy of the test data | |
dataTest = parseFile('test.data'); | |
m = size(dataTest,1); | |
X = dataTest(:,1:1558); | |
X = str2double(X); | |
yTemp = dataTest(:,1559); | |
y = ones(size(yTemp,1),1); | |
n = size(X,2); | |
for i = 1:length(yTemp) | |
string = yTemp{i}; | |
if strcmp(string,'nonad.') == 0 | |
y(i,1) = -1; | |
else | |
y(i,1) = 1; | |
end | |
end | |
correct = 0; | |
for i = 1:m | |
prediction = w * X(i,:)' + b; | |
if prediction > 0 && y(i) == 1 | |
correct = correct + 1; | |
end | |
if prediction < 0 && y(i) == 0 | |
correct = correct + 1; | |
end | |
end | |
display('The accuracy is : '); | |
accuracy = (correct / length(y))*100; | |
display(accuracy); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment