资源简介
pegasos算法以及需要的最新版本cvx工具箱
代码片段和文件信息
% Using pegasos algorithm to solve a simple linear SVM classifier without a bias term (b)
% Note that we use CVX libarary to first optimal value and then report the
% convergence rate for pegasos algorithm
clear;
clc;
% load data
load q1x.dat
load q1y.dat
% load a9a_X.mat;
% load a9a_Y.mat;
tic;
% define variables
X = q1x;
Y = 2*(q1y-0.5);%-11
% X = X1;
% Y = ttrain‘;%-11
[train_num feature_num] = size(X);
lambda = 0.01; % it can be set as 1/C
cvx_begin quiet
variable w(feature_num);
variable xi(train_num);
%minimize 1/2*lambda || x ||^2 + 1/k*sigma(max{01-yw‘x})
minimize( 0.5*lambda*w‘*w + (1 / train_num) *sum(xi));
subject to
Y .* (X*w) >= 1 - xi;
xi >= 0;
cvx_end
fbest = cvx_optval;
maxIter = 5000;
[w fval] = pegasos(XYlambda[]maxIter10);
toc;
% reporting accuracy
t_num = sum(sign(X * w) == Y);
accuracy = 100 * t_num / train_num;
fprintf(‘Accuracy on training set is %.4f %%\n‘ accuracy);
% plot convergence rate
figure(1) clf
step = 100;
semilogy( 1:step:maxIter fval(1:step:end) - fbest ‘r-.‘‘LineWidth‘1.5 );
xlabel(‘# iter‘);
ylabel(‘f - fmin‘);
axis([1 maxIter 1e-8 2e2]);
title(‘Convergence rate for pegasos algorithm‘);
% visualize
figure(2) clf
xp = linspace(min(X(:1)) max(X(:1)) 100);
yp = - w(1) * xp / w(2);
yp1 = - (w(1)*xp - 1) / w(2); % margin boundary for support vectors for y=1
yp0 = - (w(1)*xp + 1) / w(2); % margin boundary for support vectors for y=0
% index of negative samples
idx0 = find(q1y==0);
% idx0 = find(Y==0);
% index of positive samples
idx1 = find(q1y==1);
% idx1 = find(Y==1);
plot(X(idx0 1) X(idx0 2) ‘rx‘);
hold on
plot(X(idx1 1) X(idx1 2) ‘go‘);
plot(xp yp ‘-b‘ xp yp1 ‘--g‘ xp yp0 ‘--r‘);
hold off
title(sprintf(‘decision boundary for a linear SVM classifier with lambda = %g‘ lambda));
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 252892 2015-11-20 10:42 pegasos\a9a_X.mat
文件 6165 2015-11-20 10:42 pegasos\a9a_Y.mat
....... 2365 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\abs.m
....... 1096 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\blkdiag.m
....... 1551 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\builtins.m
....... 2240 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\cat.m
....... 455 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\conj.m
....... 2140 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\conv.m
....... 803 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\ctranspose.m
....... 2816 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\cumprod.m
....... 1844 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\cumsum.m
....... 1112 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\diag.m
....... 438 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\disp.m
....... 532 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\end.m
....... 754 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\eq.m
....... 3398 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\exp.m
....... 1484 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\find.m
....... 368 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\full.m
....... 1224 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\ge.m
....... 1448 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\gt.m
....... 1289 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\hankel.m
....... 225 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\horzcat.m
....... 519 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\imag.m
....... 725 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\ipermute.m
....... 607 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\isreal.m
....... 1398 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\kron.m
....... 932 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\ldivide.m
....... 1269 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\le.m
....... 4070 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\log.m
....... 1493 2015-06-11 09:43 pegasos\cvx\builtins\@cvx\lt.m
............此处省略1404个文件信息
评论
共有 条评论