支撑向量机 SVM 学习笔记一 (Matlab代码)

 


 

% 支持向量机SVM分类算法



clear all
% ------------------------------------------------------------%
% 构造两类训练数据集     x2=aa*x1+bb+(-)b1

aa=3;
bb=6;
b1=0.2;       

x1(:,1) = -1:0.1:1;

n = length(x1(:,1));

x1(:,2) = aa.*x1(:,1) + bb + b1 + abs(randn(n,1));
y1 = ones(n,1);

x2(:,1) =  -1:0.1:1;
x2(:,2) = aa.*x2(:,1) + bb - b1 - abs(randn(n,1));

y2 = -ones(n,1);


figure;
plot(x1(:,1),x1(:,2),'bx',x2(:,1),x2(:,2),'k.');
hold on;

X = [x1;x2]; % 训练样本
Y = [y1;y2]; % 训练目标,n×1的矩阵,n为样本个数,值为+1或-1
% ------------------------------------------------------------%

tic
% 解二次优化方城
n = length(Y);
H = (Y*Y').*(X*X'); % liner kernel
f = -ones(n,1);
A = [];
b = [];
Aeq = Y';
beq = 0;
lb = zeros(n,1);
ub = 100*ones(n,1);
a0 = zeros(n,1);
options = optimset;
options.LargeScale = 'off';
options.Display = 'off';
[a,fval,eXitflag,output,lambda] = quadprog(H,f,A,b,Aeq,beq,lb,ub,a0,options);
eXitflag

time=toc


   
%先复制上面的代码 运行后  再复制下面的代码 运行 呵呵 (对Matlab不熟,不知道为什么一次执行和分开执行看到的结果不同)

% 以下是分类平面:
Y2=a.*Y;
 W(1)=sum(Y2.*(X(:,1)));
 W(2)=sum(Y2.*(X(:,2)));

aLarge=find(a>0.1);
j=aLarge(1);

S(:,1)=Y.*a.*X(:,1);
S(:,2)=Y.*a.*X(:,2);

S2=S*(X(j,:)');

b=Y(j)-sum(S2);


 xx1=x1(:,1);
 xx2=-(W(1)*xx1+b)/W(2);
 plot(xx1,xx2);              


matlab 没怎么用过 呵呵   写的很烂

感谢 安金龙 的 支持向量机若干问题的研究    以及 神威异度空间 的部分代码

 

 

转载于:https://www.cnblogs.com/feathersky/archive/2009/06/16/1504392.html

MATLAB用于支持向量-svm_v251.rar MATLAB用于支持向量 Support Vector Machine toolbox for Matlab Version 2.51, January 2002 Contents.m contains a brief description of all parts of this toolbox. Main features are: - Except for the QP solver, all parts are written in plain Matlab. This   guarantees for easy modification. Special kinds of kernels that require   much computation (such as the Fisher kernel, which is based on a model of   the data) can easily be incorporated. - Extension to multi-class problems via error correcting output codes is   included. - Unless many other SVM toolboxes, this one can handle SVMs with 1norm   or 2norm of the slack variables. - For both cases, a decomposition algorithm is implemented for the training   routine, together with efficient working set selection strategies.   The training algorithm uses many of the ideas proposed by Thorsten   Joachims for his SVMlight. It thus should exhibit a scaling behaviour that   is comparable to SVMlight. This toolbox optionally makes use of a Matlab wrapper for an interior point code in LOQO style . To compile the wrapper, run   mex loqo.c pr_loqo.c Make sure you have turned on the compiler optimizations in mexopts.sh The LOQO code can be retrieved from   http://www.kernel-machines.org/code/prloqo.tar.gz The wrapper comes directly from Steve Gunn. Copyright Anton Schwaighofer mailto:anton.schwaighofer@gmx.net This program is released unter the GNU General Public License. See License.txt for details. Changes in version 2.51: - fixed bug in SVMTRAIN that prevented correct initialisation with   NET.recompute==Inf Changes in version 2.5: - Handling of multi-class problems with ECOC - NET.recompute is set to Inf by default, thus all training is done   incrementally by default. - Handling the case of all training examples being -1 or 1 correctly Changes in version 2.4: - Better selection of the initial working set - Added workaround for a Matlab quadprog bug with badly conditioned   matrices - There is now a new kernel function 'rbffull' where a full matrix    C may be put into an RBF kernel:   K = exp'*C*) Changes in version 2.3: - slightly more compact debug output Changes in version 2.2: - New default values for parameter qpsize that make the whole toolbox   *much* faster - Workaround for a Matlab bug with sparse matrices - Changed the definition of the RBF-Kernel: from |x-y|^2/   to |x-y|^2/. This means that all parameter settings for old   versions need to be updated! - A few minor things I can't remember Changes in version 2.1: Fixed a nasty bug at the KKT check Changes in version 2.0: All relevant routines have been updated to allow the use of a SVM with 2norm of the slack variables .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值