{"id":445,"date":"2017-03-03T15:49:18","date_gmt":"2017-03-03T07:49:18","guid":{"rendered":"http:\/\/www.mrtblog.cn\/?p=445"},"modified":"2023-03-05T15:25:39","modified_gmt":"2023-03-05T07:25:39","slug":"%e6%94%af%e6%8c%81%e5%90%91%e9%87%8f%e6%9c%ba%e7%ae%97%e6%b3%95%e5%8f%8a%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"http:\/\/www.mrtblog.cn\/?p=445","title":{"rendered":"\u652f\u6301\u5411\u91cf\u673a(SVM)\u7b97\u6cd5\u53ca\u5b9e\u8df5"},"content":{"rendered":"<div class='epvc-post-count'><span class='epvc-eye'><\/span>  <span class=\"epvc-count\"> 1,346<\/span><span class='epvc-label'> Views<\/span><\/div><p><em>\u53c2\u8003\u4e66\u7c4d\u300a\u673a\u5668\u5b66\u4e60\u5b9e\u6218\u300b<\/em><\/p>\n<h2>\u57fa\u4e8e\u6700\u5927\u95f4\u9694\u5206\u5272\u6570\u636e<\/h2>\n<p><strong>\u652f\u6301\u5411\u91cf\u673a<\/strong><br \/>\n\u4f18\u70b9\uff1a\u6cdb\u5316\u9519\u8bef\u7387\u4f4e\uff0c\u8ba1\u7b97\u5f00\u9500\u4e0d\u5927\uff0c\u7ed3\u679c\u6613\u89e3\u91ca<br \/>\n\u7f3a\u70b9\uff1a\u5bf9\u53c2\u6570\u8c03\u8282\u548c\u6838\u51fd\u6570\u9009\u62e9\u654f\u611f\uff0c\u539f\u59cb\u5206\u7c7b\u5668\u4e0d\u52a0\u4fee\u6539\u4ec5\u9002\u7528\u4e8e\u5904\u7406\u4e8c\u5206\u7c7b\u95ee\u9898\u3002<br \/>\n\u4f7f\u7528\u6570\u636e\u7c7b\u578b\uff1a\u6570\u503c\u578b\u548c\u6807\u79f0\u578b\u6570\u636e<\/p>\n<p>\u8003\u86516-1\u4e2dA-D4\u4e2a\u65b9\u6846\u7684\u6570\u636e\u70b9\u5206\u5e03\uff0c\u80fd\u5426\u753b\u51fa\u4e00\u6761\u76f4\u7ebf\u5c06\u4e24\u7ec4\u6570\u636e\u70b9\u5206\u5f00\u5462\uff1f\u518d\u770b\u770b\u56fe6-2\uff0c\u4e24\u7ec4\u6570\u636e\u53c8\u80fd\u4e0d\u80fd\u7528\u4e00\u6761\u76f4\u7ebf\u5206\u5f00\u5462\uff1f<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-446\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-1.png\" alt=\"\" width=\"599\" height=\"492\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-1.png 599w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-1-300x246.png 300w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/a> <a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-447 size-full\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-2.png\" width=\"871\" height=\"485\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-2.png 871w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-2-300x167.png 300w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-2-768x428.png 768w\" sizes=\"auto, (max-width: 871px) 100vw, 871px\" \/><\/a><\/p>\n<p>\u4e0a\u8ff0\u5c06\u6570\u636e\u96c6\u5206\u9694\u5f00\u7684\u76f4\u7ebf\u79f0\u4e3a\u5206\u5272\u8d85\u5e73\u9762\u3002\u5728\u4e0a\u9762\u7ed9\u51fa\u7684\u4f8b\u5b50\u4e2d\uff0c\u7531\u4e8e\u6570\u636e\u70b9\u90fd\u5728\u4e8c\u7ef4\u5e73\u9762\u4e0a\uff0c\u6240\u4ee5\u6b64\u65f6\u5206\u5272\u8d85\u5e73\u9762\u5c31\u53ea\u662f\u4e00\u6761\u76f4\u7ebf\u3002\u4f46\u662f\uff0c\u5982\u679c\u6240\u7ed9\u7684\u6570\u636e\u96c6\u662f\u4e09\u7ef4\u7684\uff0c\u90a3\u4e48\u6b64\u65f6\u5206\u9694\u6570\u636e\u7684\u5c31\u662f\u4e00\u4e2a\u5e73\u9762\u3002<\/p>\n<p>\u6211\u4eec\u5e0c\u671b\u80fd\u91c7\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u6784\u5efa\u5206\u7c7b\u5668\uff0c\u5373\u5982\u679c\u6570\u636e\u70b9\u79bb\u51b3\u7b56\u8fb9\u754c\u8d8a\u8fdc\uff0c\u90a3\u4e48\u6700\u540e\u7684\u7ed3\u679c\u5c31\u8d8a\u53ef\u4fe1\u3002\u56fe6-2\u4e2dB-D\u7684\u4e09\u6761\u76f4\u7ebf\uff0c\u4ed6\u4eec\u90fd\u80fd\u5206\u5f00\u6570\u636e\uff0c\u90a3\u4e48B\u548cC\u7684\u76f4\u7ebf\u662f\u5426\u6bd4D\u7684\u76f4\u7ebf\u66f4\u597d\u5462\u3002<\/p>\n<p>\u652f\u6301\u5411\u91cf\u5c31\u662f\u79bb\u5206\u5272\u8d85\u5e73\u9762\u6700\u8fd1\u7684\u90a3\u4e9b\u70b9\u3002\u63a5\u4e0b\u6765\u8981\u8bd5\u7740\u6700\u5927\u5316\u652f\u6301\u5411\u91cf\u5230\u5206\u9694\u9762\u7684\u8ddd\u79bb\uff0c\u9700\u8981\u627e\u5230\u95ee\u9898\u7684\u4f18\u5316\u6c42\u89e3\u65b9\u6cd5\u3002<\/p>\n<h2>\u5bfb\u627e\u6700\u5927\u95f4\u9694<\/h2>\n<p>\u56fe6-3\uff0c\u5206\u5272\u8d85\u5e73\u9762\u7684\u5f62\u5f0f\u53ef\u4ee5\u5199\u6210<strong>wx<\/strong>+b\uff0c\u70b9A\u5230\u5e73\u9762\u7684\u8ddd\u79bb\u4e3a|<strong>wA<\/strong>+b|\/||<strong>w<\/strong>||<\/p>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-448\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-3.png\" alt=\"\" width=\"619\" height=\"470\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-3.png 619w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-p6-3-300x228.png 300w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/a><\/p>\n<h4>\u5206\u7c7b\u5668\u6c42\u89e3\u7684\u4f18\u5316\u95ee\u9898<\/h4>\n<p>\u8f93\u5165\u6570\u636e\u7ed9\u5206\u7c7b\u5668\u4e00\u4e2a\u7c7b\u522b\u6807\u7b7e\uff0c\u7c7b\u4f3c\u4e8eSigmoid\u7684\u51fd\u6570\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8esigmoid\u7ed9\u51fa\u51fd\u6570\u53d6\u503c\u4e3a0\u62161\uff0c\u800cSVM\u4e2d\u53d6\u503c\u4e3a-1\u6216+1\u3002<\/p>\n<p>\u8ba1\u7b97\u6570\u636e\u70b9\u5230\u5206\u9694\u9762\u7684\u8ddd\u79bb\u65f6\uff0c\u95f4\u9694\u901a\u8fc7label*(<strong>wx<\/strong>+b)\uff0c\u8fd9\u6837\u65e0\u8bba\u662f\u5e73\u9762\u54ea\u4e00\u4fa7\u7684\u70b9\uff0c\u7ed3\u679c\u90fd\u4f1a\u662f\u4e00\u4e2a\u6b63\u6570\u3002<br \/>\n\u73b0\u5728\u7684\u76ee\u6807\u5c31\u662f\u627e\u51fa\u5206\u7c7b\u5176\u5b9a\u4e49\u7684w\u548cb\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u627e\u5230\u5177\u6709\u6700\u5c0f\u95f4\u9694\u7684\u6570\u636e\u70b9\uff0c\u800c\u8fd9\u4e9b\u6570\u636e\u70b9\u5c31\u662f\u524d\u9762\u63d0\u5230\u7684\u652f\u6301\u5411\u91cf\u3002\u4e00\u65e6\u627e\u5230\u5177\u6709\u6700\u5c0f\u95f4\u9694\u7684\u6570\u636e\u70b9\uff0c\u6211\u4eec\u5c31\u9700\u8981\u5c06\u5176\u6700\u5927\u5316\u3002<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-1.png\" alt=\"\" width=\"318\" height=\"80\" class=\"alignnone size-full wp-image-454\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-1.png 318w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-1-300x75.png 300w\" sizes=\"auto, (max-width: 318px) 100vw, 318px\" \/><\/a><br \/>\n\u901a\u8fc7\u5f15\u5165\u62c9\u683c\u6717\u65e5\u4e58\u5b50\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed9\u4e88\u7ea6\u675f\u6761\u4ef6\u6765\u8868\u8ff0\u539f\u6765\u7684\u95ee\u9898\uff0c\u4f18\u5316\u76ee\u6807\u51fd\u6570\u53ef\u4ee5\u5199\u6210<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-2.png\" alt=\"\" width=\"432\" height=\"97\" class=\"alignnone size-full wp-image-455\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-2.png 432w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-2-300x67.png 300w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><\/a><br \/>\n\u7ea6\u675f\u6761\u4ef6\u4e3a\uff1a<br \/>\n<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-3.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-3.png\" alt=\"\" width=\"255\" height=\"70\" class=\"alignnone size-full wp-image-456\"><\/a><br \/>\n\u4e4b\u524d\u7684\u524d\u63d0\u5efa\u7acb\u5728\u6570\u636e100%\u7ebf\u6027\u53ef\u5206\u7684\u60c5\u51b5\u4e0b\u3002\u4f46\u662f\u8fd9\u51e0\u4e4e\u662f\u4e0d\u53ef\u80fd\u7684\u3002\u4e3a\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5f15\u5165\u677e\u5f1b\u53d8\u91cf\uff0c\u5141\u8bb8\u4e00\u4e9b\u6570\u636e\u70b9\u5904\u4e8e\u5206\u9694\u9762\u7684\u9519\u8bef\u4e00\u4fa7\uff0c\u8fd9\u6837\uff0c\u7ea6\u675f\u6761\u4ef6\u6539\u4e3a\uff1a<br \/>\n<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-4.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-4.png\" alt=\"\" width=\"313\" height=\"64\" class=\"alignnone size-full wp-image-457\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-4.png 313w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-formula-4-300x61.png 300w\" sizes=\"auto, (max-width: 313px) 100vw, 313px\" \/><\/a><br \/>\nC\u7528\u4e8e\u63a7\u5236\u201c\u6700\u5927\u5316\u95f4\u9694\u201d\u548c\u201c\u4fdd\u8bc1\u5927\u90e8\u5206\u7684\u5e97\u7684\u51fd\u6570\u95f4\u9694\u5c0f\u4e8e1.0\u201d\u8fd9\u4e24\u4e2a\u76ee\u6807\u7684\u6743\u91cd\u3002C\u4f5c\u4e3a\u53c2\u6570\uff0c\u8c03\u8282C\u5f97\u5230\u4e0d\u540c\u7684\u7ed3\u679c\u3002\u4e00\u65e6\u7403\u51fa\u4e86\u6240\u6709\u7684lapha\uff0c\u90a3\u4e48\u5206\u5272\u8d85\u5e73\u9762\u5c31\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e9balpha\u8868\u8fbe\u3002SVM\u7684\u4e3b\u8981\u5de5\u4f5c\u5c31\u662f\u6c42\u51fa\u8fd9\u4e9balpha\u3002<\/p>\n<h4>SVM\u5e94\u7528\u7684\u4e00\u822c\u6846\u67b6<\/h4>\n<p><strong>SVM\u7684\u4e00\u822c\u6d41\u7a0b<\/strong><br \/>\n\uff081\uff09\u6536\u96c6\u6570\u636e\uff1a\u53ef\u4ee5\u4f7f\u7528\u4efb\u610f\u65b9\u6cd5<br \/>\n\uff082\uff09\u51c6\u5907\u6570\u636e\uff1a\u9700\u8981\u6570\u503c\u578b\u6570\u636e<br \/>\n\uff083\uff09\u5206\u6790\u6570\u636e\uff1a\u6709\u52a9\u4e8e\u53ef\u89c6\u5316\u8d85\u5e73\u9762<br \/>\n\uff084\uff09\u8bad\u7ec3\u7b97\u6cd5\uff1aSVM\u5927\u90e8\u5206\u65f6\u95f4\u90fd\u6e90\u81ea\u8bad\u7ec3\uff0c\u8be5\u8fc7\u7a0b\u4e3b\u8981\u5b9e\u73b0\u4e24\u4e2a\u53c2\u6570\u7684\u8c03\u4f18<br \/>\n\uff085\uff09\u6d4b\u8bd5\u7b97\u6cd5\uff1a\u5341\u5206\u7b80\u5355\u7684\u8ba1\u7b97\u8fc7\u7a0b\u5c31\u53ef\u4ee5\u5b9e\u73b0<br \/>\n\uff086\uff09\u4f7f\u7528\u7b97\u6cd5\uff1a\u51e0\u4e4e\u6240\u6709\u7684\u5206\u7c7b\u95ee\u9898\u90fd\u53ef\u4ee5\u4f7f\u7528SVM\uff0c\u4f46SVM\u5e94\u7528\u4e8e\u591a\u5206\u7c7b\u65f6\u8981\u5bf9SVM\u7a0d\u4f5c\u4fee\u6539<\/p>\n<hr>\n<h3>SMO\u9ad8\u6548\u4f18\u5316\u7b97\u6cd5<\/h3>\n<p>1996\u5e74\uff0cplatt\u53d1\u5e03\u4e86\u4e00\u4e2a\u79f0\u4e3aSMO\u7684\u5f3a\u5927\u7b97\u6cd5\u7528\u4e8e\u8bad\u7ec3SVM\u3002SMO\u8868\u793a\u5e8f\u5217\u6700\u5c0f\u4f18\u5316\u3002\u76f4\u63a5\u7684\u597d\u5904\u5c31\u662f\u628a\u4e00\u4e2a\u5927\u95ee\u9898\u5206\u89e3\u6210\u5f88\u591a\u5bb9\u6613\u89e3\u51b3\u7684\u5c0f\u95ee\u9898\uff0c\u901a\u8fc7\u89e3\u51b3\u8fd9\u4e9b\u5c0f\u95ee\u9898\u4f7f\u95ee\u9898\u6574\u4f53\u7684\u89e3\u51b3\u901f\u5ea6\u53d8\u5feb\u4e86\u3002SMO\u7b97\u6cd5\u7684\u5de5\u4f5c\u539f\u7406\u662f\uff1a\u6bcf\u6b21\u5faa\u73af\u4e2d\u9009\u62e9\u4e24\u4e2aalpha\u8fdb\u884c\u4f18\u5316\u5904\u7406\u3002\u4e00\u65e6\u627e\u5230\u4e00\u5bf9\u5408\u9002\u7684alpha\uff0c\u5c31\u589e\u5927\u4e00\u4e2a\u51cf\u5c0f\u53e6\u4e00\u4e2a\u3002\u8fd9\u91cc\u7684\u201c\u5408\u9002\u201d\u6307\u4e24\u4e2aalpha\u5fc5\u987b\u8981\u7b26\u5408\u4e00\u5b9a\u7684\u6761\u4ef6\uff0c\u6761\u4ef6\u4e4b\u4e00\u5c31\u662f\u8fd9\u4e24\u4e2aalpha\u5fc5\u987b\u8981\u5728\u95f4\u9694\u8fb9\u754c\u4e4b\u5916\uff0c\u7b2c\u4e8c\u4e2a\u6761\u4ef6\u6307\u4e24\u4e2aalpha\u6ca1\u6709\u8fdb\u884c\u8fc7\u533a\u95f4\u5316\u5904\u7406\u6216\u8005\u4e0d\u5728\u8fb9\u754c\u4e0a\u3002<\/p>\n<h4>\u5e94\u7528\u7b80\u5316\u7248SMO\u7b97\u6cd5\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u96c6<\/h4>\n<p>platt SMO\u7b97\u6cd5\u8f83\u4e3a\u590d\u6742\uff0c\u6240\u4ee5\u5728\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u5148\u5c55\u793a\u7b80\u5316\u4ee3\u7801\uff0c\u4e4b\u540e\u518d\u7814\u7a76\u5b8c\u6574\u4ee3\u7801\u3002\u7b80\u5316\u7248\u4f1a\u9996\u5148\u5728\u6570\u636e\u96c6\u4e0a\u904d\u5386\u6bcf\u4e2aalpha\uff0c\u7136\u540e\u5728\u5269\u4e0b\u7684alpha\u96c6\u5408\u4e2d\u968f\u673a\u9009\u62e9\u53e6\u4e00\u4e2aalpha\u5efa\u7acbalpha\u5bf9\u91cd\u8981\u7684\u4e00\u70b9\u662f\uff0c\u6211\u4eec\u8981\u540c\u65f6\u6539\u53d8\u4e24\u4e2aalpha\u3002\u56e0\u4e3a\u6539\u53d8\u4e00\u4e2aalpha\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ea6\u675f\u6761\u4ef6\u5931\u6548\u3002<br \/>\n\u4e3a\u6b64\uff0c\u6784\u5efa\u8f85\u52a9\u51fd\u6570\uff0c\u7528\u4e8e\u5728\u67d0\u4e2a\u533a\u95f4\u8303\u56f4\u5185\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u6574\u6570\uff0c\u4ee5\u53ca\u5728\u6570\u503c\u592a\u5927\u65f6\u5bf9\u5176\u8c03\u6574\u3002\u5c06\u4ee5\u4e0b\u4ee3\u7801\u52a0\u5165SVM.py\u4e2d<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\n#coding=utf-8\ndef loadDataSet(fileName):\n    dataMat = []\n    labelMat = []\n    fr = open(fileName)\n    for line in fr.readlines():\n        lineArr = line.strip().split(&#039;\\t&#039;)\n        dataMat.append([float(lineArr[0]),float(lineArr[1])])\n        labelMat.append(float(lineArr[2]))\n    return dataMat,labelMat\n\ndef selectJrand(i,m):\n    j = i\n    while (j == i):\n        j = int(random.uniform(0,m))\n    return j\n\ndef clipAlpha(aj,H,L):\n    if aj &gt; H:#\u6700\u5927\u503c\u4e0d\u80fd\u8d85\u8fc7H\n        aj = H\n    if L &gt; aj:#\u6700\u5c0f\u503c\u4e0d\u80fd\u5c0f\u4e8eL\n        aj = L\n    return aj\n<\/code><\/pre>\n<p>selectJrand\u4e2di\u65f6alpha\u7684\u4e0b\u6807\uff0cm\u662f\u6240\u6709alpha\u7684\u6570\u76ee\uff0cclipAlpha\u7528\u4e8e\u8c03\u6574\u5927\u4e8eH\u6216\u8005\u5c0f\u4e8eL\u7684alpha\u503c\u3002\u8f93\u5165\u4ee3\u7801\u9a8c\u8bc1\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\nimport SVM\ndataArr,labelArr = svm.loadDataSet(&#039;testSet.txt&#039;)\nprint labelArr\n<\/code><\/pre>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-example-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-467\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-example-1.png\" alt=\"\" width=\"1427\" height=\"278\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-example-1.png 1427w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-example-1-300x58.png 300w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-example-1-768x150.png 768w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/03\/SVM-example-1-1024x199.png 1024w\" sizes=\"auto, (max-width: 1427px) 100vw, 1427px\" \/><\/a><\/p>\n<p>\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u4f7f\u7528SMO\u7b97\u6cd5\u7684\u7b2c\u4e00\u4e2a\u7248\u672c\u4e86\u3002<br \/>\nSMO\u4f2a\u4ee3\u7801\u4e3a\uff1a<br \/>\n\u5f53\u8fed\u4ee3\u6b21\u6570\u5c0f\u4e8e\u6700\u5927\u8fed\u4ee3\u6b21\u6570\u65f6(\u5916\u5faa\u73af)<br \/>\n\u5bf9\u6570\u636e\u96c6\u4e2d\u7684\u6bcf\u4e2a\u6570\u636e\u5411\u91cf(\u5185\u5faa\u73af):<br \/>\n\u5982\u679c\u8be5\u6570\u636e\u5411\u91cf\u53ef\u4ee5\u88ab\u4f18\u5316:<br \/>\n\u968f\u673a\u9009\u62e9\u53e6\u5916\u4e00\u4e2a\u6570\u636e\u5411\u91cf<br \/>\n\u540c\u65f6\u4f18\u5316\u4e24\u4e2a\u5411\u91cf<br \/>\n\u5982\u679c\u4e24\u4e2a\u5411\u91cf\u90fd\u4e0d\u80fd\u88ab\u4f18\u5316\uff0c\u9000\u51fa\u5185\u5faa\u73af<br \/>\n\u5982\u679c\u6240\u6709\u5411\u91cf\u90fd\u6ca1\u88ab\u4f18\u5316\uff0c\u589e\u52a0\u8fed\u4ee3\u6570\u76ee\uff0c\u7ee7\u7eed\u4e0b\u4e00\u6b21\u5faa\u73af<\/p>\n<p>\u6253\u5f00SVM.py\uff0c\u8f93\u5165\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\n#coding=utf-8\nfrom numpy import *\ndef loadDataSet(fileName):\n    dataMat = []\n    labelMat = []\n    fr = open(fileName)\n    for line in fr.readlines():\n        lineArr = line.strip().split(&#039;\\t&#039;)\n        dataMat.append([float(lineArr[0]),float(lineArr[1])])\n        labelMat.append(float(lineArr[2]))\n    return dataMat,labelMat\n\ndef selectJrand(i,m):#\u4ea7\u751f\u4e00\u4e2a0-m\u4e14\u4e0d\u7b49\u4e8ei\u7684\u968f\u673a\u6570\n    j = i\n    while (j == i):\n        j = int(random.uniform(0,m))\n    return j\n\ndef clipAlpha(aj,H,L):\n    if aj &gt; H:#\u6700\u5927\u503c\u4e0d\u80fd\u8d85\u8fc7H\n        aj = H\n    if L &gt; aj:#\u6700\u5c0f\u503c\u4e0d\u80fd\u5c0f\u4e8eL\n        aj = L\n    return aj\n\ndef smoSimple(dataMatIn,classLabels,C,toler,maxIter):#\u8bad\u7ec3\u96c6\uff0c\u6807\u7b7e\uff0c\u5e38\u6570C\uff0c\u5bb9\u9519\u7387\uff0c\u8fed\u4ee3\u6b21\u6570\n    dataMatrix = mat(dataMatIn)\n    labelMat = mat(classLabels).transpose()\n    b = 0\n    m,n = shape(dataMatrix)#\u8bad\u7ec3\u96c6\u6570\uff0c\u5411\u91cf\u7ef4\u6570\n    alphas = mat(zeros((m,1)))#\u9ed8\u8ba4\u51680\u7684alpha\u53c2\u6570\n    iter = 0\n    while ( iter &lt; maxIter):#\u8fed\u4ee3\n        alphaPairsChanged = 0\n        for i in range(m):\n            fXi = float(multiply(alphas,labelMat).T * (dataMatrix * dataMatrix[i,:].T)) + b\n            Ei = fXi - float(labelMat[i])#\u7b2c\u4e00\u4e2aa\u7684\u8bef\u5dee\n            if (labelMat[i] * Ei &lt; -toler) and (alphas[i] &lt; C) or ((labelMat[i] * Ei &gt; toler)) and (alphas[i] &gt; 0):\n                j = selectJrand(i,m)#\u968f\u673a\u9009\u53e6\u4e00\u4e2a\u8bad\u7ec3\u6837\u672c\n                fXj = float(multiply(alphas,labelMat).T * (dataMatrix*dataMatrix[j,:].T)) + b\n                Ej = fXj - float(labelMat[j])#\u7b2c\u4e8c\u4e2aa\u7684\u8bef\u5dee\n                alphaIold = alphas[i].copy()\n                alphaJold = alphas[j].copy()\n            if (labelMat[i] != labelMat[j]):#\u4e00\u4e2a\u662f\u6b63\u6837\u672c\uff0c\u4e00\u4e2a\u662f\u8d1f\u6837\u672c\n                L = max(0,alphas[j] - alphas[i])#a\u4e00\u5b9a\u5c0f\u4e8eC\uff0c\u5219L\u6700\u5927\u4e3aC\uff0c\u6700\u5c0f\u4e3a0\n                H = min(C,C+alphas[j] - alphas[i])#\u540c\u7406\uff0cH\u6700\u5927\u4e3aC\uff0c\u6700\u5c0f\u4e3a0\n            else:#\u4e24\u4e2a\u90fd\u662f\u6b63\u6837\u672c\u6216\u8d1f\u6837\u672c\n                L = max(0,alphas[j] + alphas[i] - C)\n                H = min(C,alphas[j] + alphas[i])\n            if L == H:\n                continue\n            eta = 2.0 * dataMatrix[i,:] * dataMatrix[j,:].T - dataMatrix[i,:] * dataMatrix[i,:].T - dataMatrix[j,:]*dataMatrix[j,:].T\n            if eta &gt;= 0:\n                continue\n            alphas[j] -= labelMat[j] * (Ei - Ej) \/ eta\n            alphas[j] = clipAlpha(alphas[j],H,L)\n            if (abs(alphas[j] - alphaJold) &lt; 0.00001): \n                continue\n            alphas[i] += labelMat[j] * labelMat[i] * (alphaJold - alphas[j])\n            b1 = b - Ei - labelMat[i] * (alphas[i] - alphaIold) * dataMatrix[i,:] * dataMatrix[i,:].T - labelMat[j] * (alphas[j] - alphaJold) * dataMatrix[i,:] * dataMatrix[j,:].T\n            b2 = b - Ej- labelMat[i] * (alphas[i] - alphaIold) * dataMatrix[i,:] * dataMatrix[j,:].T - labelMat[j] * (alphas[j]-alphaJold) * dataMatrix[j,:] * dataMatrix[j,:].T\n            if (0 &lt; alphas[i]) and (C &gt; alphas[i]):\n                b = b1\n            elif (0 &lt; alphas[j]) and (C &gt; alphas[j]):\n                b = b2\n            else:\n                b = (b1 + b2) \/ 2.0\n            alphaPairsChanged += 1\n            if (alphaPairsChanged == 0):\n                iter += 1\n            else:\n                iter = 0\n    return b,alphas\n<\/code><\/pre>\n<p>smo\u51fd\u6570\u4f1a\u4e0d\u65ad\u9009\u62e9\u4e24\u4e2aalpha\u53c2\u6570\uff0c\u5c1d\u8bd5\u6539\u53d8\u5b83\u4eec\u7684\u503c\uff0c\u5e76\u8981\u6c42\u5728\u6539\u53d8\u7684\u8fc7\u7a0b\u4e2d\u6ee1\u8db3\u7ea6\u675f\u6761\u4ef6\uff0c\u76f4\u5230\u8fed\u4ee3\u6b21\u6570\u6ee1\u8db3\u6216alpha\u4e0d\u518d\u6539\u53d8\u3002<br \/>\n\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\u6d4b\u8bd5\uff1a<\/p>\n<pre class=\"highlight\"><code class=\"language-python line-numbers\">\nimport SVM\ndataArr,labelArr = SVM.loadDataSet(&#039;testSet6.txt&#039;)\nb,alphas = SVM.smoSimple(dataArr,labelArr,0.6,0.001,40)\nprint b\nprint alphas[alphas&gt;0]\n<\/code><\/pre>\n<p><a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/04\/SVM-example-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-540\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/04\/SVM-example-2.png\" alt=\"\" width=\"1042\" height=\"297\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/04\/SVM-example-2.png 1042w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/04\/SVM-example-2-300x86.png 300w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/04\/SVM-example-2-768x219.png 768w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/04\/SVM-example-2-1024x292.png 1024w\" sizes=\"auto, (max-width: 1042px) 100vw, 1042px\" \/><\/a><br \/>\n\u5c06\u8fd9\u4e9b\u70b9\u5728\u5706\u56fe\u4e2d\u6807\u8bb0\u51fa\u6765\uff1a<br \/>\n<a href=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/04\/SVM-example-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-541\" src=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/04\/SVM-example-3.png\" alt=\"\" width=\"661\" height=\"557\" srcset=\"http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/04\/SVM-example-3.png 661w, http:\/\/www.mrtblog.cn\/wp-content\/uploads\/2017\/04\/SVM-example-3-300x253.png 300w\" sizes=\"auto, (max-width: 661px) 100vw, 661px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1,346 Views\u53c2\u8003\u4e66\u7c4d\u300a\u673a\u5668\u5b66\u4e60\u5b9e\u6218\u300b \u57fa\u4e8e\u6700\u5927\u95f4\u9694\u5206\u5272\u6570\u636e \u652f\u6301\u5411\u91cf\u673a \u4f18\u70b9\uff1a\u6cdb\u5316\u9519\u8bef\u7387\u4f4e\uff0c\u8ba1\u7b97\u5f00\u9500 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[43,75],"class_list":["post-445","post","type-post","status-publish","format-standard","hentry","category-machine-learning","tag-python","tag-svm"],"_links":{"self":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=445"}],"version-history":[{"count":5,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/445\/revisions"}],"predecessor-version":[{"id":1197,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=\/wp\/v2\/posts\/445\/revisions\/1197"}],"wp:attachment":[{"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=445"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrtblog.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}