建立smo.m

  1. % function [alpha,bias] = smo(X, y, C, tol)
  2.  
  3. function model = smo(X, y, C, tol)
  4.  
  5. % SMO: SMO algorithm for SVM
  6. %
  7. %Implementation of the Sequential Minimal Optimization (SMO)
  8. %training algorithm for Vapnik's Support Vector Machine (SVM)
  9. %
  10. % This is a modified code from Gavin Cawley's MATLAB Support
  11. % Vector Machine Toolbox
  12. % (c) September 2000.
  13. %
  14. % Diego Andres Alvarez.
  15. %
  16. % USAGE: [alpha,bias] = smo(K, y, C, tol)
  17. %
  18. % INPUT:
  19. %
  20. % K: n x n kernel matrix
  21. % y: 1 x n vector of labels, -1 or 1
  22. % C: a regularization parameter such that 0 <= alpha_i <= C/n
  23. % tol: tolerance for terminating criterion
  24. %
  25. % OUTPUT:
  26. %
  27. % alpha: 1 x n lagrange multiplier coefficient
  28. % bias: scalar bias (offset) term
  29.  
  30. % Input/output arguments modified by JooSeuk Kim and Clayton Scott, 2007
  31.  
  32. global SMO;
  33.  
  34. y = y';
  35.  
  36. ntp = size(X,1);
  37. %recompute C
  38. % C = C/ntp;
  39.  
  40. %initialize
  41. ii0 = find(y == -1);
  42. ii1 = find(y == 1);
  43.  
  44. i0 = ii0(1);
  45. i1 = ii1(1);
  46.  
  47. alpha_init = zeros(ntp, 1);
  48. alpha_init(i0) = C;
  49. alpha_init(i1) = C;
  50. bias_init = C*(X(i0,:)*X(i1,:)' -X(i0,:)*X(i1,:)') + 1;
  51.  
  52. %Inicializando las variables
  53. SMO.epsilon = 10^(-6); SMO.tolerance = tol;
  54. SMO.y = y'; SMO.C = C;
  55. SMO.alpha = alpha_init; SMO.bias = bias_init;
  56. SMO.ntp = ntp; %number of training points
  57.  
  58. %CACHES:
  59. SMO.Kcache = X*X'; %kernel evaluations
  60. SMO.error = zeros(SMO.ntp,1); %error
  61.  
  62. numChanged = 0; examineAll = 1;
  63.  
  64. %When all data were examined and no changes done the loop reachs its
  65. %end. Otherwise, loops with all data and likely support vector are
  66. %alternated until all support vector be found.
  67. while ((numChanged > 0) || examineAll)
  68. numChanged = 0;
  69. if examineAll
  70. %Loop sobre todos los puntos
  71. for i = 1:ntp
  72. numChanged = numChanged + examineExample(i);
  73. end;
  74. else
  75. %Loop sobre KKT points
  76. for i = 1:ntp
  77. %Solo los puntos que violan las condiciones KKT
  78. if (SMO.alpha(i)>SMO.epsilon) && (SMO.alpha(i)<(SMO.C-SMO.epsilon))
  79. numChanged = numChanged + examineExample(i);
  80. end;
  81. end;
  82. end;
  83.  
  84. if (examineAll == 1)
  85. examineAll = 0;
  86. elseif (numChanged == 0)
  87. examineAll = 1;
  88. end;
  89. end;
  90. alpha = SMO.alpha';
  91. alpha(alpha < SMO.epsilon) = 0;
  92. alpha(alpha > C-SMO.epsilon) = C;
  93. bias = -SMO.bias;
  94.  
  95. model.w = (y.*alpha)* X; %%%%%%%%%%%%%%%%%%%%%%
  96. model.b = bias;
  97.  
  98. return;
  99.  
  100. function RESULT = fwd(n)
  101. global SMO;
  102. LN = length(n);
  103. RESULT = -SMO.bias + sum(repmat(SMO.y,1,LN) .* repmat(SMO.alpha,1,LN) .* SMO.Kcache(:,n))';
  104. return;
  105.  
  106. function RESULT = examineExample(i2)
  107. %First heuristic selects i2 and asks to examineExample to find a
  108. %second point (i1) in order to do an optimization step with two
  109. %Lagrange multipliers
  110.  
  111. global SMO;
  112. alpha2 = SMO.alpha(i2); y2 = SMO.y(i2);
  113.  
  114. if ((alpha2 > SMO.epsilon) && (alpha2 < (SMO.C-SMO.epsilon)))
  115. e2 = SMO.error(i2);
  116. else
  117. e2 = fwd(i2) - y2;
  118. end;
  119.  
  120. % r2 < 0 if point i2 is placed between margin (-1)-(+1)
  121. % Otherwise r2 is > 0. r2 = f2*y2-1
  122.  
  123. r2 = e2*y2;
  124. %KKT conditions:
  125. % r2>0 and alpha2==0 (well classified)
  126. % r2==0 and 0% r2<0 and alpha2==C (support vectors between margins)
  127. %
  128. % Test the KKT conditions for the current i2 point.
  129. %
  130. % If a point is well classified its alpha must be 0 or if
  131. % it is out of its margin its alpha must be C. If it is at margin
  132. % its alpha must be between 0%take action only if i2 violates Karush-Kuhn-Tucker conditions
  133. if ((r2 < -SMO.tolerance) && (alpha2 < (SMO.C-SMO.epsilon))) || ...
  134. ((r2 > SMO.tolerance) && (alpha2 > SMO.epsilon))
  135. % If it doens't violate KKT conditions then exit, otherwise continue.
  136.  
  137. %Try i2 by three ways; if successful, then immediately return 1;
  138. RESULT = 1;
  139. % First the routine tries to find an i1 lagrange multiplier that
  140. % maximizes the measure |E1-E2|. As large this value is as bigger
  141. % the dual objective function becames.
  142. % In this first test, only support vectors will be tested.
  143.  
  144. POS = find((SMO.alpha > SMO.epsilon) & (SMO.alpha < (SMO.C-SMO.epsilon)));
  145. [MAX,i1] = max(abs(e2 - SMO.error(POS)));
  146. if ~isempty(i1)
  147. if takeStep(i1, i2, e2), return;
  148. end;
  149. end;
  150.  
  151. %The second heuristic choose any Lagrange Multiplier that is a SV and tries to optimize
  152. for i1 = randperm(SMO.ntp)
  153. if (SMO.alpha(i1) > SMO.epsilon) & (SMO.alpha(i1) < (SMO.C-SMO.epsilon))
  154. %if a good i1 is found, optimise
  155. if takeStep(i1, i2, e2), return;
  156. end;
  157. end
  158. end
  159.  
  160. %if both heuristc above fail, iterate over all data set
  161. for i1 = randperm(SMO.ntp)
  162. if ~((SMO.alpha(i1) > SMO.epsilon) & (SMO.alpha(i1) < (SMO.C-SMO.epsilon)))
  163. if takeStep(i1, i2, e2), return;
  164. end;
  165. end
  166. end;
  167. end;
  168.  
  169. %no progress possible
  170. RESULT = 0;
  171. return;
  172.  
  173. function RESULT = takeStep(i1, i2, e2)
  174. % for a pair of alpha indexes, verify if it is possible to execute
  175. % the optimisation described by Platt.
  176.  
  177. global SMO;
  178. RESULT = 0;
  179. if (i1 == i2), return;
  180. end;
  181.  
  182. % compute upper and lower constraints, L and H, on multiplier a2
  183. alpha1 = SMO.alpha(i1); alpha2 = SMO.alpha(i2);
  184. y1 = SMO.y(i1); y2 = SMO.y(i2);
  185. C = SMO.C; K = SMO.Kcache;
  186.  
  187. s = y1*y2;
  188. if (y1 ~= y2)
  189. L = max(0, alpha2-alpha1); H = min(C, alpha2-alpha1+C);
  190. else
  191. L = max(0, alpha1+alpha2-C); H = min(C, alpha1+alpha2);
  192. end;
  193.  
  194. if (L == H), return;
  195. end;
  196.  
  197. if (alpha1 > SMO.epsilon) & (alpha1 < (C-SMO.epsilon))
  198. e1 = SMO.error(i1);
  199. else
  200. e1 = fwd(i1) - y1;
  201. end;
  202.  
  203. %if (alpha2 > SMO.epsilon) & (alpha2 < (C-SMO.epsilon))
  204. % e2 = SMO.error(i2);
  205. %else
  206. % e2 = fwd(i2) - y2;
  207. %end;
  208.  
  209. %compute eta
  210. k11 = K(i1,i1); k12 = K(i1,i2); k22 = K(i2,i2);
  211. eta = 2.0*k12-k11-k22;
  212.  
  213. %recompute Lagrange multiplier for pattern i2
  214. if (eta < 0.0)
  215. a2 = alpha2 - y2*(e1 - e2)/eta;
  216.  
  217. %constrain a2 to lie between L and H
  218. if (a2 < L)
  219. a2 = L;
  220. elseif (a2 > H)
  221. a2 = H;
  222. end;
  223. else
  224. %When eta is not negative, the objective function W should be
  225. %evaluated at each end of the line segment. Only those terms in the
  226. %objective function that depend on alpha2 need be evaluated...
  227.  
  228. ind = find(SMO.alpha>0);
  229.  
  230. aa2 = L; aa1 = alpha1 + s*(alpha2-aa2);
  231.  
  232. Lobj = aa1 + aa2 + sum((-y1*aa1/2).*SMO.y(ind).*K(ind,i1) + (-y2*aa2/2).*SMO.y(ind).*K(ind,i2));
  233.  
  234. aa2 = H; aa1 = alpha1 + s*(alpha2-aa2);
  235. Hobj = aa1 + aa2 + sum((-y1*aa1/2).*SMO.y(ind).*K(ind,i1) + (-y2*aa2/2).*SMO.y(ind).*K(ind,i2));
  236.  
  237. if (Lobj>Hobj+SMO.epsilon)
  238. a2 = H;
  239. elseif (Lobj<Hobj-SMO.epsilon)
  240. a2 = L;
  241. else
  242. a2 = alpha2;
  243. end;
  244. end;
  245.  
  246. if (abs(a2-alpha2) < SMO.epsilon*(a2+alpha2+SMO.epsilon))
  247. return;
  248. end;
  249.  
  250. % recompute Lagrange multiplier for pattern i1
  251. a1 = alpha1 + s*(alpha2-a2);
  252.  
  253. w1 = y1*(a1 - alpha1); w2 = y2*(a2 - alpha2);
  254.  
  255. %update threshold to reflect change in Lagrange multipliers
  256. b1 = SMO.bias + e1 + w1*k11 + w2*k12;
  257. bold = SMO.bias;
  258.  
  259. if (a1>SMO.epsilon) & (a1<(C-SMO.epsilon))
  260. SMO.bias = b1;
  261. else
  262. b2 = SMO.bias + e2 + w1*k12 + w2*k22;
  263. if (a2>SMO.epsilon) & (a2<(C-SMO.epsilon))
  264. SMO.bias = b2;
  265. else
  266. SMO.bias = (b1 + b2)/2;
  267. end;
  268. end;
  269.  
  270. % update error cache using new Lagrange multipliers
  271. SMO.error = SMO.error + w1*K(:,i1) + w2*K(:,i2) + bold - SMO.bias;
  272. SMO.error(i1) = 0.0; SMO.error(i2) = 0.0;
  273.  
  274. % update vector of Lagrange multipliers
  275. SMO.alpha(i1) = a1; SMO.alpha(i2) = a2;
  276.  
  277. %report progress made
  278. RESULT = 1;
  279. return;

画图文件:start_SMOforSVM.m(点击自动生成二维两类数据,画图,这里只是线性的,非线性的可以对应修改)

  1. clear
  2. X = []; Y=[];
  3. figure;
  4. % Initialize training data to empty; will get points from user
  5. % Obtain points froom the user:
  6. trainPoints=X;
  7. trainLabels=Y;
  8. clf;
  9. axis([-5 5 -5 5]);
  10. if isempty(trainPoints)
  11. % Define the symbols and colors we'll use in the plots later
  12. symbols = {'o','x'};
  13. classvals = [-1 1];
  14. trainLabels=[];
  15. hold on; % Allow for overwriting existing plots
  16. xlim([-5 5]); ylim([-5 5]);
  17.  
  18. for c = 1:2
  19. title(sprintf('Click to create points from class %d. Press enter when finished.', c));
  20. [x, y] = getpts;
  21.  
  22. plot(x,y,symbols{c},'LineWidth', 2, 'Color', 'black');
  23.  
  24. % Grow the data and label matrices
  25. trainPoints = vertcat(trainPoints, [x y]);
  26. trainLabels = vertcat(trainLabels, repmat(classvals(c), numel(x), 1));
  27. end
  28.  
  29. end
  30.  
  31. % C = 10;tol = 0.001;
  32. % par = SMOforSVM(trainPoints, trainLabels , C, tol );
  33. % p=length(par.b); m=size(trainPoints,2);
  34. % if m==2
  35. % % for i=1:p
  36. % % plot(X(lc(i)-l(i)+1:lc(i),1),X(lc(i)-l(i)+1:lc(i),2),'bo')
  37. % % hold on
  38. % % end
  39. % k = -par.w(1)/par.w(2);
  40. % b0 = - par.b/par.w(2);
  41. % bdown=(-par.b-1)/par.w(2);
  42. % bup=(-par.b+1)/par.w(2);
  43. % for i=1:p
  44. % hold on
  45. % h = refline(k,b0(i));
  46. % set(h, 'Color', 'r')
  47. % hdown=refline(k,bdown(i));
  48. % set(hdown, 'Color', 'b')
  49. % hup=refline(k,bup(i));
  50. % set(hup, 'Color', 'b')
  51. % end
  52. % end
  53. % xlim([-5 5]); ylim([-5 5]);
  54. %
  55. % pause
  56.  
  57. C = 10;tol = 0.001;
  58. par = smo(trainPoints, trainLabels, C, tol);
  59. p=length(par.b); m=size(trainPoints,2);
  60. if m==2
  61. % for i=1:p
  62. % plot(X(lc(i)-l(i)+1:lc(i),1),X(lc(i)-l(i)+1:lc(i),2),'bo')
  63. % hold on
  64. % end
  65. k = -par.w(1)/par.w(2);
  66. b0 = - par.b/par.w(2);
  67. bdown=(-par.b-1)/par.w(2);
  68. bup=(-par.b+1)/par.w(2);
  69. for i=1:p
  70. hold on
  71. h = refline(k,b0(i));
  72. set(h, 'Color', 'r')
  73. hdown=refline(k,bdown(i));
  74. set(hdown, 'Color', 'b')
  75. hup=refline(k,bup(i));
  76. set(hup, 'Color', 'b')
  77. end
  78. end
  79. xlim([-5 5]); ylim([-5 5]);

  

支持向量机的smo算法(MATLAB code)的更多相关文章

  1. SVM-非线性支持向量机及SMO算法

    SVM-非线性支持向量机及SMO算法 如果您想体验更好的阅读:请戳这里littlefish.top 线性不可分情况 线性可分问题的支持向量机学习方法,对线性不可分训练数据是不适用的,为了满足函数间隔大 ...

  2. smo算法matlab实现

    看完CSDN上结构之法,算法之道的支持向量机通俗导论(理解SVM的三层境界) http://blog.csdn.net/v_july_v/article/details/7624837     参考了 ...

  3. 机器学习算法整理(七)支持向量机以及SMO算法实现

    以下均为自己看视频做的笔记,自用,侵删! 还参考了:http://www.ai-start.com/ml2014/ 在监督学习中,许多学习算法的性能都非常类似,因此,重要的不是你该选择使用学习算法A还 ...

  4. 支持向量机原理(四)SMO算法原理

    支持向量机原理(一) 线性支持向量机 支持向量机原理(二) 线性支持向量机的软间隔最大化模型 支持向量机原理(三)线性不可分支持向量机与核函数 支持向量机原理(四)SMO算法原理 支持向量机原理(五) ...

  5. [笔记]关于支持向量机(SVM)中 SMO算法的学习(一)理论总结

    1. 前言 最近又重新复习了一遍支持向量机(SVM).其实个人感觉SVM整体可以分成三个部分: 1. SVM理论本身:包括最大间隔超平面(Maximum Margin Classifier),拉格朗日 ...

  6. 支持向量机(Support Vector Machine)-----SVM之SMO算法(转)

    此文转自两篇博文 有修改 序列最小优化算法(英语:Sequential minimal optimization, SMO)是一种用于解决支持向量机训练过程中所产生优化问题的算法.SMO由微软研究院的 ...

  7. 机器学习之支持向量机(二):SMO算法

    注:关于支持向量机系列文章是借鉴大神的神作,加以自己的理解写成的:若对原作者有损请告知,我会及时处理.转载请标明来源. 序: 我在支持向量机系列中主要讲支持向量机的公式推导,第一部分讲到推出拉格朗日对 ...

  8. 统计学习方法c++实现之六 支持向量机(SVM)及SMO算法

    前言 支持向量机(SVM)是一种很重要的机器学习分类算法,本身是一种线性分类算法,但是由于加入了核技巧,使得SVM也可以进行非线性数据的分类:SVM本来是一种二分类分类器,但是可以扩展到多分类,本篇不 ...

  9. 支持向量机(SVM)中的 SMO算法

    1. 前言 最近又重新复习了一遍支持向量机(SVM).其实个人感觉SVM整体可以分成三个部分: 1. SVM理论本身:包括最大间隔超平面(Maximum Margin Classifier),拉格朗日 ...

随机推荐

  1. 05_IOC容器装配Bean(注解方式)

    IOC容器装配Bean(注解方式) 1.使用注解方式进行Bean注册 xml 方式: <bean id="" class=""> spring2.5 ...

  2. 网站安全扫描工具--Netsparker的使用

    Netsparker是一款安全简单的web应用安全漏电扫描工具.该软件功能非常强大,使用方便.Netsparker与其他综合 性的web应用安全扫描工具相比的一个特点是它能够更好的检测SQL Inje ...

  3. 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 ...

  4. python_way day12 sqlalchemy,原生mysql命令

    python_way day12  sqlalchemy,mysql原生命令 1.sqlalchemy 2.mysql 原生命令 一,sqlalchemy SQLAlchemy本身无法操作数据库,其必 ...

  5. 关于PHP HTML <input type="file" name="img"/>上传图片,图片大小,宽高,后缀名。

    在我们的系统中,不免要上传图片,视频等文件,在上传中,需要做的一些判断,文件大小等方面. 注意: 在php.ini 中的post_max_size,upload_max_filesize默认为2M,在 ...

  6. js 监听监键盘动作(转)

    主要分四个部分 第一部分:浏览器的按键事件 第二部分:兼容浏览器 第三部分:代码实现和优化 第四部分:总结 第一部分:浏览器的按键事件 用js实现键盘记录,要关注浏览器的三种按键事件类型,即keydo ...

  7. varchar(10)与nvarchar(10)有什么区别

    前者是非unicode型,存储字符按1个算(内部空间存储占1字节),存储汉字的话按2个算, 就是可以存10个字符或者5个汉字 后者是unicode型,存储什么都是按1个算(内部空间存储占2字节), 就 ...

  8. Web浏览器的缓存机制

    Web缓存的工作原理 所有的缓存都是基于一套规则来帮助他们决定什么时候使用缓存中的副本提供服务(假设有副本可用的情况下,未被销毁回收或者未被删除修改).这些规则有的在协议中有定义(如HTTP协议1.0 ...

  9. web设计经验<七>13步打造优雅的WEB字体

    今天,大多数浏览器已经默认支持Web字体,日趋增多的字体特性被嵌入最新版HTML和CSS标准中,Web字体即将迎来一个趋于复杂的崭新时代.下面是一些基本的关于字体的规则,特别适用于Web字体. 原文地 ...

  10. Make 教程

    Make 命令教程 原文作者: 阮一峰 原文链接:http://www.ruanyifeng.com/blog/2015/02/make.html (在原文基础上稍作修改) 代码变成可执行文件,叫做编 ...