Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Correctness.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function count = Correctness(input)
red = evalin('base','red');
blue = evalin('base','blue');
val_r = ones(1,size(red,2));
val_b = -ones(1,size(blue,2));


trans = evalin('base','trans');
b = evalin('base','b');
gamma = input(1);
if (size(input,2) == 1)
a = evalin('base','a');
else
a = input(2);
end

prediction = SVMLS([red blue],[val_r val_b], [red blue], trans, gamma, a, b);
result = [val_r val_b] + prediction';
count = nnz(~result);
end

35 changes: 35 additions & 0 deletions Optimizer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function Optimizer()
trans = evalin('base','trans');

gammas = zeros(1,5);
values = zeros(1,5);
for i = 1:5
[gammas(i), values(i)] = fminbnd(@Correctness,2*(i-1),2*i);
end
[Y, I] = min(values);
gamma = gammas(I);
assignin('base', 'gamma', gamma);
return;


%{
if (trans == 1)
gammas = zeros(1,5);
values = zeros(1,5);
for i = 1:5
[gammas(i), values(i)] = fminbnd(@Correctness,2*(i-1),2*i);
end
[Y, I] = min(values);
gamma = gammas(I);
assignin('base', 'gamma', gamma);
return;
elseif (trans == 2 || trans == 3)
output = fminsearch(@Correctness, [1, 1]);
gamma = output(1);
a = output(2);
assignin('base', 'gamma', gamma);
assignin('base', 'a', a);
return;
end
%}
end
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
# support_vector_machine
301 Project
# support_vector_machine
301 Project

By Andrii Luchko & Ucha Samadashvili


Usage:

-Run app.mlapp in MATLAB. Make sure all files are in the present working directory

-User can generate new data by pressing �Generate New Data� which will generate new training points randomly.

-While app is running user can input new training data by assigning array of points in console to �red� (class1) and �blue� (class2) where array is the form of: blue = [point1 point2 � pointN]
where point is the form of:
point1 = [x_cord ; y_cord]

-User can also input new data to be classified by assigning array of points in the same format as above to �data�

-User can change parameters �gamma�, �a�, �b� manually for all transformations by sliding drag bars and then clicking �Plot�. For linear transformation user can simply click �Optimize� and then �Plot� button and the program will find the best parameter.

28 changes: 14 additions & 14 deletions SVMLS.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function output = SVMLS(x_data, y_data, unknown, transformation, gamma, a, b, varargin)
if not((size(x_data, 2) == size(y_data, 2)))
x_size = size(x_data, 2);
if not((x_size == size(y_data, 2)))
return
end
if nargin < 4
Expand All @@ -18,7 +19,6 @@
b = 1;
end


if (transformation == 1)
Fi = @(x, xx) dot(x, xx);
elseif (transformation == 2)
Expand All @@ -31,32 +31,32 @@
fprintf('error')
end

Omega = zeros(size(x_data, 2), size(x_data, 2));
for i = 1:size(x_data, 2)
for j = 1:size(x_data, 2)
Omega = zeros(x_size, x_size);
for i = 1:x_size
for j = 1:x_size
Omega(i,j) = y_data(i)*y_data(j)*Fi(x_data(:,i), x_data(:,j));
end
end

gamma_matrix = 1/gamma*eye(size(x_data, 2));
gamma_matrix = 1/gamma*eye(x_size);
Omega = Omega + gamma_matrix;

A = [[0 y_data]' [-1.*y_data; Omega]];
o = [0; ones(size(x_data, 2), 1)];

o = [0; ones(x_size, 1)];
x = A\o;



y = zeros(size(unknown,2), 1);
for j = 1:size(unknown, 2)
sum = 0;
for i = 1:size(x_data, 2)
sum = sum + x(i+1)*y_data(i)*Fi(x_data(:,i), unknown(:,j))+x(1);
for i = 1:x_size
sum = sum + sqrt((x(i+1))^2)*y_data(i)*Fi(x_data(:,i), unknown(:,j))+x(1);
end

y(j) = sign(sum);
end



output = y;
end


end
Binary file added app.mlapp
Binary file not shown.
Binary file removed app1.mlapp
Binary file not shown.
37 changes: 15 additions & 22 deletions generate_data.m
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
%function to generate new set of data
function generate_data()
x1_cord = 1.5*rand(1, 20)+1;
y1_cord = 1.5*rand(1, 20)+1;
x2_cord = 1.5*rand(1, 20)+2;
y2_cord = 1.5*rand(1, 20)+2;
x_data = [[x1_cord; y1_cord] [x2_cord; y2_cord]];
y_data = [ones(1, 20) -ones(1,20)];
assignin('base', 'x_data', x_data)
assignin('base', 'y_data', y_data)
red_x = 4*rand(1, 20);
red_y = 4*rand(1, 20);
blue_x = 4*rand(1, 20)+3;
blue_y = 4*rand(1, 20)+3;
red = [red_x; red_y];
blue = [blue_x; blue_y];
data = [];

x = 1:0.1:4;
y = 1:0.1:4;

%%%create grid
unknown = zeros(2, 961);
for i = 1:31
for j = 1:31
unknown(1, 31*(i-1)+(j)) = x(i);
unknown(2, 31*(i-1)+(j)) = y(j);
end
end
assignin('base', 'unknown', unknown)
assignin('base','red',red);
assignin('base','blue',blue);
assignin('base','data',data);
%assignin('base','trans',3);
%assignin('base','gamma',1);
%assignin('base','a',1);
%assignin('base','b',1);

assignin('base', 'gammaValue', 1)
assignin('base', 'aValue', 1)

end
17 changes: 0 additions & 17 deletions test.m

This file was deleted.