Problem with implementation of Linear SVM with quadprog
Ältere Kommentare anzeigen
I have to implement a classificator SVM (Support Vector Machine) in Matlab. The samples are the measurement of diabetes in the Pima Indians. Every sample have 9 features (8 + class):
- Number of times pregnant
- Plasma glucose concentration a 2 hours in an oral glucose tolerance test
- Diastolic blood pressure (mm Hg)
- Triceps skin fold thickness (mm)
- 2-Hour serum insulin (mu U/ml)
- Body mass index (weight in kg/(height in m)^2)
- Diabetes pedigree function
- Age (years)
- Class variable (0 for not diabetes and 1 for diabetes)
In my program I follow this few steps:
- Load data from data.txt (768 samples)
- Delete all incomplete samples (392 samples)
- Implementation of the SVM alghoritms (using quadprog)
- Display results
The problem is that every time quadprog send me this message:
Maximum number of iterations exceeded; increase options.MaxIter.
To continue solving the problem with the current solution as the
starting point, set x0 = x before calling quadprog.
I tried to solve this problem in three ways:
- increasing the number of iterations (currently is set at 1000, but I also tried higher values without results)
- changing UB (if set to infinity the solution is unbounded)
- implementing a PCA algorithm and reducing the number of features (the error launched by quadprog remains unchanged)
Since it is a work for the university, I have to implement a LINEAR SVM with quadprog (this is very very important). Can you help me? Thanks and sorry for my bad english
close all
clear all
clc
%
% load data
filename = 'data.txt';
vsample = importdata(filename,',');
vsample = vsample';
%
% delete incomplete data
vsample(:,vsample(2,:) == 0) = [];
vsample(:,vsample(3,:) == 0) = [];
vsample(:,vsample(4,:) == 0) = [];
vsample(:,vsample(5,:) == 0) = [];
vsample(:,vsample(6,:) == 0) = [];
%
% training data for svm
x = vsample(1:8,:)'; % samples matrix with 8 features
y = (vsample(9,:)'*2)-1; % class array (+1 or -1)
%
% implementation of the LINEAR svm
svm.D = size(x,2);
svm.L = size(x,1);
H = zeros(svm.L,svm.L);
for i=1:svm.L
for j=1:svm.L
H(i,j) = x(i,:)*x(j,:)'*y(i)*y(j);
end
end
f = -ones(svm.L,1);
A = [];
b = [];
Aeq = y';
beq = 0;
LB = zeros(svm.L,1);
UB = inf*ones(svm.L,1);
options = optimset('MaxIter',1000,'LargeScale','off');
alpha = quadprog(H,f,A,b,Aeq,beq,LB,UB,[],options);
svm.sv = find(alpha>0);
svm.ns = length(svm.sv);
svm.w = sum(((alpha.*y)*ones(1,svm.D)).*x)';
svm.b = y(svm.sv(1)) - sum((alpha.*y).*(x*x(svm.sv(1),:)'));
%
% testing with same samples
y_est = sign(x*svm.w+svm.b);
prc = (sum(y_est(:)'== y(:)')/svm.L)*100;
disp([fprintf('\n'), 'Percentage of samples correctly classified: ', num2str(round(prc)), '%']);
Antworten (0)
Kategorien
Mehr zu Quadratic Programming and Cone Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!