pegasos算法matlab代码


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 %%
‘ 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  pegasosa9a_X.mat

     文件       6165  2015-11-20 10:42  pegasosa9a_Y.mat

    .......      2365  2015-06-11 09:43  pegasoscvxuiltins@cvxabs.m

    .......      1096  2015-06-11 09:43  pegasoscvxuiltins@cvxlkdiag.m

    .......      1551  2015-06-11 09:43  pegasoscvxuiltins@cvxuiltins.m

    .......      2240  2015-06-11 09:43  pegasoscvxuiltins@cvxcat.m

    .......       455  2015-06-11 09:43  pegasoscvxuiltins@cvxconj.m

    .......      2140  2015-06-11 09:43  pegasoscvxuiltins@cvxconv.m

    .......       803  2015-06-11 09:43  pegasoscvxuiltins@cvxctranspose.m

    .......      2816  2015-06-11 09:43  pegasoscvxuiltins@cvxcumprod.m

    .......      1844  2015-06-11 09:43  pegasoscvxuiltins@cvxcumsum.m

    .......      1112  2015-06-11 09:43  pegasoscvxuiltins@cvxdiag.m

    .......       438  2015-06-11 09:43  pegasoscvxuiltins@cvxdisp.m

    .......       532  2015-06-11 09:43  pegasoscvxuiltins@cvxend.m

    .......       754  2015-06-11 09:43  pegasoscvxuiltins@cvxeq.m

    .......      3398  2015-06-11 09:43  pegasoscvxuiltins@cvxexp.m

    .......      1484  2015-06-11 09:43  pegasoscvxuiltins@cvxfind.m

    .......       368  2015-06-11 09:43  pegasoscvxuiltins@cvxfull.m

    .......      1224  2015-06-11 09:43  pegasoscvxuiltins@cvxge.m

    .......      1448  2015-06-11 09:43  pegasoscvxuiltins@cvxgt.m

    .......      1289  2015-06-11 09:43  pegasoscvxuiltins@cvxhankel.m

    .......       225  2015-06-11 09:43  pegasoscvxuiltins@cvxhorzcat.m

    .......       519  2015-06-11 09:43  pegasoscvxuiltins@cvximag.m

    .......       725  2015-06-11 09:43  pegasoscvxuiltins@cvxipermute.m

    .......       607  2015-06-11 09:43  pegasoscvxuiltins@cvxisreal.m

    .......      1398  2015-06-11 09:43  pegasoscvxuiltins@cvxkron.m

    .......       932  2015-06-11 09:43  pegasoscvxuiltins@cvxldivide.m

    .......      1269  2015-06-11 09:43  pegasoscvxuiltins@cvxle.m

    .......      4070  2015-06-11 09:43  pegasoscvxuiltins@cvxlog.m

    .......      1493  2015-06-11 09:43  pegasoscvxuiltins@cvxlt.m

............此处省略1404个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)