支持向量机的smo算法(MATLAB code)
建立smo.m
- % function [alpha,bias] = smo(X, y, C, tol)
- function model = smo(X, y, C, tol)
- % SMO: SMO algorithm for SVM
- %
- %Implementation of the Sequential Minimal Optimization (SMO)
- %training algorithm for Vapnik's Support Vector Machine (SVM)
- %
- % This is a modified code from Gavin Cawley's MATLAB Support
- % Vector Machine Toolbox
- % (c) September 2000.
- %
- % Diego Andres Alvarez.
- %
- % USAGE: [alpha,bias] = smo(K, y, C, tol)
- %
- % INPUT:
- %
- % K: n x n kernel matrix
- % y: 1 x n vector of labels, -1 or 1
- % C: a regularization parameter such that 0 <= alpha_i <= C/n
- % tol: tolerance for terminating criterion
- %
- % OUTPUT:
- %
- % alpha: 1 x n lagrange multiplier coefficient
- % bias: scalar bias (offset) term
- % Input/output arguments modified by JooSeuk Kim and Clayton Scott, 2007
- global SMO;
- y = y';
- ntp = size(X,1);
- %recompute C
- % C = C/ntp;
- %initialize
- ii0 = find(y == -1);
- ii1 = find(y == 1);
- i0 = ii0(1);
- i1 = ii1(1);
- alpha_init = zeros(ntp, 1);
- alpha_init(i0) = C;
- alpha_init(i1) = C;
- bias_init = C*(X(i0,:)*X(i1,:)' -X(i0,:)*X(i1,:)') + 1;
- %Inicializando las variables
- SMO.epsilon = 10^(-6); SMO.tolerance = tol;
- SMO.y = y'; SMO.C = C;
- SMO.alpha = alpha_init; SMO.bias = bias_init;
- SMO.ntp = ntp; %number of training points
- %CACHES:
- SMO.Kcache = X*X'; %kernel evaluations
- SMO.error = zeros(SMO.ntp,1); %error
- numChanged = 0; examineAll = 1;
- %When all data were examined and no changes done the loop reachs its
- %end. Otherwise, loops with all data and likely support vector are
- %alternated until all support vector be found.
- while ((numChanged > 0) || examineAll)
- numChanged = 0;
- if examineAll
- %Loop sobre todos los puntos
- for i = 1:ntp
- numChanged = numChanged + examineExample(i);
- end;
- else
- %Loop sobre KKT points
- for i = 1:ntp
- %Solo los puntos que violan las condiciones KKT
- if (SMO.alpha(i)>SMO.epsilon) && (SMO.alpha(i)<(SMO.C-SMO.epsilon))
- numChanged = numChanged + examineExample(i);
- end;
- end;
- end;
- if (examineAll == 1)
- examineAll = 0;
- elseif (numChanged == 0)
- examineAll = 1;
- end;
- end;
- alpha = SMO.alpha';
- alpha(alpha < SMO.epsilon) = 0;
- alpha(alpha > C-SMO.epsilon) = C;
- bias = -SMO.bias;
- model.w = (y.*alpha)* X; %%%%%%%%%%%%%%%%%%%%%%
- model.b = bias;
- return;
- function RESULT = fwd(n)
- global SMO;
- LN = length(n);
- RESULT = -SMO.bias + sum(repmat(SMO.y,1,LN) .* repmat(SMO.alpha,1,LN) .* SMO.Kcache(:,n))';
- return;
- function RESULT = examineExample(i2)
- %First heuristic selects i2 and asks to examineExample to find a
- %second point (i1) in order to do an optimization step with two
- %Lagrange multipliers
- global SMO;
- alpha2 = SMO.alpha(i2); y2 = SMO.y(i2);
- if ((alpha2 > SMO.epsilon) && (alpha2 < (SMO.C-SMO.epsilon)))
- e2 = SMO.error(i2);
- else
- e2 = fwd(i2) - y2;
- end;
- % r2 < 0 if point i2 is placed between margin (-1)-(+1)
- % Otherwise r2 is > 0. r2 = f2*y2-1
- r2 = e2*y2;
- %KKT conditions:
- % r2>0 and alpha2==0 (well classified)
- % r2==0 and 0% r2<0 and alpha2==C (support vectors between margins)
- %
- % Test the KKT conditions for the current i2 point.
- %
- % If a point is well classified its alpha must be 0 or if
- % it is out of its margin its alpha must be C. If it is at margin
- % its alpha must be between 0%take action only if i2 violates Karush-Kuhn-Tucker conditions
- if ((r2 < -SMO.tolerance) && (alpha2 < (SMO.C-SMO.epsilon))) || ...
- ((r2 > SMO.tolerance) && (alpha2 > SMO.epsilon))
- % If it doens't violate KKT conditions then exit, otherwise continue.
- %Try i2 by three ways; if successful, then immediately return 1;
- RESULT = 1;
- % First the routine tries to find an i1 lagrange multiplier that
- % maximizes the measure |E1-E2|. As large this value is as bigger
- % the dual objective function becames.
- % In this first test, only support vectors will be tested.
- POS = find((SMO.alpha > SMO.epsilon) & (SMO.alpha < (SMO.C-SMO.epsilon)));
- [MAX,i1] = max(abs(e2 - SMO.error(POS)));
- if ~isempty(i1)
- if takeStep(i1, i2, e2), return;
- end;
- end;
- %The second heuristic choose any Lagrange Multiplier that is a SV and tries to optimize
- for i1 = randperm(SMO.ntp)
- if (SMO.alpha(i1) > SMO.epsilon) & (SMO.alpha(i1) < (SMO.C-SMO.epsilon))
- %if a good i1 is found, optimise
- if takeStep(i1, i2, e2), return;
- end;
- end
- end
- %if both heuristc above fail, iterate over all data set
- for i1 = randperm(SMO.ntp)
- if ~((SMO.alpha(i1) > SMO.epsilon) & (SMO.alpha(i1) < (SMO.C-SMO.epsilon)))
- if takeStep(i1, i2, e2), return;
- end;
- end
- end;
- end;
- %no progress possible
- RESULT = 0;
- return;
- function RESULT = takeStep(i1, i2, e2)
- % for a pair of alpha indexes, verify if it is possible to execute
- % the optimisation described by Platt.
- global SMO;
- RESULT = 0;
- if (i1 == i2), return;
- end;
- % compute upper and lower constraints, L and H, on multiplier a2
- alpha1 = SMO.alpha(i1); alpha2 = SMO.alpha(i2);
- y1 = SMO.y(i1); y2 = SMO.y(i2);
- C = SMO.C; K = SMO.Kcache;
- s = y1*y2;
- if (y1 ~= y2)
- L = max(0, alpha2-alpha1); H = min(C, alpha2-alpha1+C);
- else
- L = max(0, alpha1+alpha2-C); H = min(C, alpha1+alpha2);
- end;
- if (L == H), return;
- end;
- if (alpha1 > SMO.epsilon) & (alpha1 < (C-SMO.epsilon))
- e1 = SMO.error(i1);
- else
- e1 = fwd(i1) - y1;
- end;
- %if (alpha2 > SMO.epsilon) & (alpha2 < (C-SMO.epsilon))
- % e2 = SMO.error(i2);
- %else
- % e2 = fwd(i2) - y2;
- %end;
- %compute eta
- k11 = K(i1,i1); k12 = K(i1,i2); k22 = K(i2,i2);
- eta = 2.0*k12-k11-k22;
- %recompute Lagrange multiplier for pattern i2
- if (eta < 0.0)
- a2 = alpha2 - y2*(e1 - e2)/eta;
- %constrain a2 to lie between L and H
- if (a2 < L)
- a2 = L;
- elseif (a2 > H)
- a2 = H;
- end;
- else
- %When eta is not negative, the objective function W should be
- %evaluated at each end of the line segment. Only those terms in the
- %objective function that depend on alpha2 need be evaluated...
- ind = find(SMO.alpha>0);
- aa2 = L; aa1 = alpha1 + s*(alpha2-aa2);
- Lobj = aa1 + aa2 + sum((-y1*aa1/2).*SMO.y(ind).*K(ind,i1) + (-y2*aa2/2).*SMO.y(ind).*K(ind,i2));
- aa2 = H; aa1 = alpha1 + s*(alpha2-aa2);
- Hobj = aa1 + aa2 + sum((-y1*aa1/2).*SMO.y(ind).*K(ind,i1) + (-y2*aa2/2).*SMO.y(ind).*K(ind,i2));
- if (Lobj>Hobj+SMO.epsilon)
- a2 = H;
- elseif (Lobj<Hobj-SMO.epsilon)
- a2 = L;
- else
- a2 = alpha2;
- end;
- end;
- if (abs(a2-alpha2) < SMO.epsilon*(a2+alpha2+SMO.epsilon))
- return;
- end;
- % recompute Lagrange multiplier for pattern i1
- a1 = alpha1 + s*(alpha2-a2);
- w1 = y1*(a1 - alpha1); w2 = y2*(a2 - alpha2);
- %update threshold to reflect change in Lagrange multipliers
- b1 = SMO.bias + e1 + w1*k11 + w2*k12;
- bold = SMO.bias;
- if (a1>SMO.epsilon) & (a1<(C-SMO.epsilon))
- SMO.bias = b1;
- else
- b2 = SMO.bias + e2 + w1*k12 + w2*k22;
- if (a2>SMO.epsilon) & (a2<(C-SMO.epsilon))
- SMO.bias = b2;
- else
- SMO.bias = (b1 + b2)/2;
- end;
- end;
- % update error cache using new Lagrange multipliers
- SMO.error = SMO.error + w1*K(:,i1) + w2*K(:,i2) + bold - SMO.bias;
- SMO.error(i1) = 0.0; SMO.error(i2) = 0.0;
- % update vector of Lagrange multipliers
- SMO.alpha(i1) = a1; SMO.alpha(i2) = a2;
- %report progress made
- RESULT = 1;
- return;
画图文件:start_SMOforSVM.m(点击自动生成二维两类数据,画图,这里只是线性的,非线性的可以对应修改)
- clear
- X = []; Y=[];
- figure;
- % Initialize training data to empty; will get points from user
- % Obtain points froom the user:
- trainPoints=X;
- trainLabels=Y;
- clf;
- axis([-5 5 -5 5]);
- if isempty(trainPoints)
- % Define the symbols and colors we'll use in the plots later
- symbols = {'o','x'};
- classvals = [-1 1];
- trainLabels=[];
- hold on; % Allow for overwriting existing plots
- xlim([-5 5]); ylim([-5 5]);
- for c = 1:2
- title(sprintf('Click to create points from class %d. Press enter when finished.', c));
- [x, y] = getpts;
- plot(x,y,symbols{c},'LineWidth', 2, 'Color', 'black');
- % Grow the data and label matrices
- trainPoints = vertcat(trainPoints, [x y]);
- trainLabels = vertcat(trainLabels, repmat(classvals(c), numel(x), 1));
- end
- end
- % C = 10;tol = 0.001;
- % par = SMOforSVM(trainPoints, trainLabels , C, tol );
- % p=length(par.b); m=size(trainPoints,2);
- % if m==2
- % % for i=1:p
- % % plot(X(lc(i)-l(i)+1:lc(i),1),X(lc(i)-l(i)+1:lc(i),2),'bo')
- % % hold on
- % % end
- % k = -par.w(1)/par.w(2);
- % b0 = - par.b/par.w(2);
- % bdown=(-par.b-1)/par.w(2);
- % bup=(-par.b+1)/par.w(2);
- % for i=1:p
- % hold on
- % h = refline(k,b0(i));
- % set(h, 'Color', 'r')
- % hdown=refline(k,bdown(i));
- % set(hdown, 'Color', 'b')
- % hup=refline(k,bup(i));
- % set(hup, 'Color', 'b')
- % end
- % end
- % xlim([-5 5]); ylim([-5 5]);
- %
- % pause
- C = 10;tol = 0.001;
- par = smo(trainPoints, trainLabels, C, tol);
- p=length(par.b); m=size(trainPoints,2);
- if m==2
- % for i=1:p
- % plot(X(lc(i)-l(i)+1:lc(i),1),X(lc(i)-l(i)+1:lc(i),2),'bo')
- % hold on
- % end
- k = -par.w(1)/par.w(2);
- b0 = - par.b/par.w(2);
- bdown=(-par.b-1)/par.w(2);
- bup=(-par.b+1)/par.w(2);
- for i=1:p
- hold on
- h = refline(k,b0(i));
- set(h, 'Color', 'r')
- hdown=refline(k,bdown(i));
- set(hdown, 'Color', 'b')
- hup=refline(k,bup(i));
- set(hup, 'Color', 'b')
- end
- end
- xlim([-5 5]); ylim([-5 5]);
支持向量机的smo算法(MATLAB code)的更多相关文章
- SVM-非线性支持向量机及SMO算法
SVM-非线性支持向量机及SMO算法 如果您想体验更好的阅读:请戳这里littlefish.top 线性不可分情况 线性可分问题的支持向量机学习方法,对线性不可分训练数据是不适用的,为了满足函数间隔大 ...
- smo算法matlab实现
看完CSDN上结构之法,算法之道的支持向量机通俗导论(理解SVM的三层境界) http://blog.csdn.net/v_july_v/article/details/7624837 参考了 ...
- 机器学习算法整理(七)支持向量机以及SMO算法实现
以下均为自己看视频做的笔记,自用,侵删! 还参考了:http://www.ai-start.com/ml2014/ 在监督学习中,许多学习算法的性能都非常类似,因此,重要的不是你该选择使用学习算法A还 ...
- 支持向量机原理(四)SMO算法原理
支持向量机原理(一) 线性支持向量机 支持向量机原理(二) 线性支持向量机的软间隔最大化模型 支持向量机原理(三)线性不可分支持向量机与核函数 支持向量机原理(四)SMO算法原理 支持向量机原理(五) ...
- [笔记]关于支持向量机(SVM)中 SMO算法的学习(一)理论总结
1. 前言 最近又重新复习了一遍支持向量机(SVM).其实个人感觉SVM整体可以分成三个部分: 1. SVM理论本身:包括最大间隔超平面(Maximum Margin Classifier),拉格朗日 ...
- 支持向量机(Support Vector Machine)-----SVM之SMO算法(转)
此文转自两篇博文 有修改 序列最小优化算法(英语:Sequential minimal optimization, SMO)是一种用于解决支持向量机训练过程中所产生优化问题的算法.SMO由微软研究院的 ...
- 机器学习之支持向量机(二):SMO算法
注:关于支持向量机系列文章是借鉴大神的神作,加以自己的理解写成的:若对原作者有损请告知,我会及时处理.转载请标明来源. 序: 我在支持向量机系列中主要讲支持向量机的公式推导,第一部分讲到推出拉格朗日对 ...
- 统计学习方法c++实现之六 支持向量机(SVM)及SMO算法
前言 支持向量机(SVM)是一种很重要的机器学习分类算法,本身是一种线性分类算法,但是由于加入了核技巧,使得SVM也可以进行非线性数据的分类:SVM本来是一种二分类分类器,但是可以扩展到多分类,本篇不 ...
- 支持向量机(SVM)中的 SMO算法
1. 前言 最近又重新复习了一遍支持向量机(SVM).其实个人感觉SVM整体可以分成三个部分: 1. SVM理论本身:包括最大间隔超平面(Maximum Margin Classifier),拉格朗日 ...
随机推荐
- 05_IOC容器装配Bean(注解方式)
IOC容器装配Bean(注解方式) 1.使用注解方式进行Bean注册 xml 方式: <bean id="" class=""> spring2.5 ...
- 网站安全扫描工具--Netsparker的使用
Netsparker是一款安全简单的web应用安全漏电扫描工具.该软件功能非常强大,使用方便.Netsparker与其他综合 性的web应用安全扫描工具相比的一个特点是它能够更好的检测SQL Inje ...
- How to run an manually installed program from terminals in Linux / Ubuntu
Say we have installed qt programs and we want to run qtcreator from the command line. What we need h ...
- python_way day12 sqlalchemy,原生mysql命令
python_way day12 sqlalchemy,mysql原生命令 1.sqlalchemy 2.mysql 原生命令 一,sqlalchemy SQLAlchemy本身无法操作数据库,其必 ...
- 关于PHP HTML <input type="file" name="img"/>上传图片,图片大小,宽高,后缀名。
在我们的系统中,不免要上传图片,视频等文件,在上传中,需要做的一些判断,文件大小等方面. 注意: 在php.ini 中的post_max_size,upload_max_filesize默认为2M,在 ...
- js 监听监键盘动作(转)
主要分四个部分 第一部分:浏览器的按键事件 第二部分:兼容浏览器 第三部分:代码实现和优化 第四部分:总结 第一部分:浏览器的按键事件 用js实现键盘记录,要关注浏览器的三种按键事件类型,即keydo ...
- varchar(10)与nvarchar(10)有什么区别
前者是非unicode型,存储字符按1个算(内部空间存储占1字节),存储汉字的话按2个算, 就是可以存10个字符或者5个汉字 后者是unicode型,存储什么都是按1个算(内部空间存储占2字节), 就 ...
- Web浏览器的缓存机制
Web缓存的工作原理 所有的缓存都是基于一套规则来帮助他们决定什么时候使用缓存中的副本提供服务(假设有副本可用的情况下,未被销毁回收或者未被删除修改).这些规则有的在协议中有定义(如HTTP协议1.0 ...
- web设计经验<七>13步打造优雅的WEB字体
今天,大多数浏览器已经默认支持Web字体,日趋增多的字体特性被嵌入最新版HTML和CSS标准中,Web字体即将迎来一个趋于复杂的崭新时代.下面是一些基本的关于字体的规则,特别适用于Web字体. 原文地 ...
- Make 教程
Make 命令教程 原文作者: 阮一峰 原文链接:http://www.ruanyifeng.com/blog/2015/02/make.html (在原文基础上稍作修改) 代码变成可执行文件,叫做编 ...