{"id":3258,"date":"2022-02-27T14:02:23","date_gmt":"2022-02-27T06:02:23","guid":{"rendered":"https:\/\/egonlin.com\/?p=3258"},"modified":"2022-02-27T14:17:23","modified_gmt":"2022-02-27T06:17:23","slug":"%e7%ac%ac%e4%b8%89%e8%8a%82%ef%bc%9a%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e7%ae%97%e6%b3%95%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=3258","title":{"rendered":"\u7b2c\u4e09\u8282\uff1a\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406"},"content":{"rendered":"<h1>\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406<\/h1>\n<h1>1.1 \u611f\u77e5\u673a\u7b97\u6cd5<\/h1>\n<pre><code class=\"language-python\"># \u611f\u77e5\u673a\u7b97\u6cd5\u56fe\u4f8b\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.font_manager import FontProperties\n\n%matplotlib inline\nfont = FontProperties(fname=&#039;\/Library\/Fonts\/Heiti.ttc&#039;)\n\nnp.random.seed(1)\nx1 = np.random.random(20)+1.5\ny1 = np.random.random(20)+0.5\nx2 = np.random.random(20)+3\ny2 = np.random.random(20)+0.5\n\n# \u4e00\u884c\u4e8c\u5217\u7b2c\u4e00\u4e2a\nplt.subplot(121)\nplt.scatter(x1, y1, s=50, color=&#039;b&#039;, label=&#039;\u7537\u5b69(+1)&#039;)\nplt.scatter(x2, y2, s=50, color=&#039;r&#039;, label=&#039;\u5973\u5b69(-1)&#039;)\nplt.vlines(2.8, 0, 2, colors=&quot;r&quot;, linestyles=&quot;-&quot;, label=&#039;$wx+b=0$&#039;)\nplt.title(&#039;\u7ebf\u6027\u53ef\u5206&#039;, fontproperties=font, fontsize=20)\nplt.xlabel(&#039;x&#039;)\nplt.legend(prop=font)\n\n# \u4e00\u884c\u4e8c\u5217\u7b2c\u4e8c\u4e2a\nplt.subplot(122)\nplt.scatter(x1, y1, s=50, color=&#039;b&#039;, label=&#039;\u7537\u5b69(+1)&#039;)\nplt.scatter(x2, y2, s=50, color=&#039;r&#039;, label=&#039;\u5973\u5b69(-1)&#039;)\nplt.scatter(3.5, 1, s=50, color=&#039;b&#039;)\nplt.title(&#039;\u7ebf\u6027\u4e0d\u53ef\u5206&#039;, fontproperties=font, fontsize=20)\nplt.xlabel(&#039;x&#039;)\nplt.legend(prop=font, loc=&#039;upper right&#039;)\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_3_0.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_3_0.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>&emsp;&emsp;\u4ece\u5de6\u56fe\u4e2d\u4e5f\u53ef\u4ee5\u770b\u51fa\u603b\u80fd\u627e\u5230\u4e00\u6761\u76f4\u7ebf\u5c06\u7537\u5b69\u548c\u5973\u5b69\u5206\u5f00\uff0c\u5373\u7537\u5b69\u548c\u5973\u5b69\u5728\u64cd\u573a\u4e0a\u7684\u5206\u5e03\u662f\u7ebf\u6027\u53ef\u5206\u7684\uff0c\u6b64\u65f6\u8be5\u5206\u9694\u76f4\u7ebf\u4e3a<br \/>\n$$<br \/>\n\\omega{x}+b=0<br \/>\n$$<br \/>\n\u5176\u4e2d$\\omega,b$\u662f\u53c2\u6570\uff0c$x$\u662f\u7537\u5b69\u548c\u5973\u5b69\u5171\u6709\u7684\u67d0\u79cd\u7279\u5f81\u3002<\/p>\n<p>&emsp;&emsp;\u5982\u679c\u67d0\u4e2a\u7537\u5b69\u4e0d\u542c\u8bdd\u8dd1\u5230\u5973\u5b69\u90a3\u8fb9\u53bb\u4e86\uff0c\u5982\u4e0b\u56fe\u53f3\u56fe\u6240\u793a\uff0c\u5219\u65e0\u6cd5\u901a\u8fc7\u4e00\u6761\u76f4\u7ebf\u80fd\u591f\u628a\u6240\u6709\u7684\u7537\u5b69\u548c\u5973\u5b69\u5206\u5f00\uff0c\u5219\u79f0\u7537\u5b69\u548c\u5973\u5b69\u5728\u64cd\u573a\u4e0a\u7684\u5206\u5e03\u662f\u7ebf\u6027\u4e0d\u53ef\u5206\u7684\uff0c\u5373\u65e0\u6cd5\u4f7f\u7528\u611f\u77e5\u673a\u7b97\u6cd5\u5b8c\u6210\u8be5\u5206\u7c7b\u8fc7\u7a0b\u3002<\/p>\n<p>&emsp;&emsp;\u4e0a\u8ff0\u6574\u4e2a\u8fc7\u7a0b\u5176\u5b9e\u5c31\u662f\u611f\u77e5\u673a\u5b9e\u73b0\u7684\u4e00\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<h2>1.1.1 \u51b3\u7b56\u51fd\u6570<\/h2>\n<p>&emsp;&emsp;\u611f\u77e5\u673a\u7b97\u6cd5\u7684\u51b3\u7b56\u51fd\u6570\u4e3a<br \/>\n$$<br \/>\nsign(\\omega^Tx)=<br \/>\n\\begin{cases}<br \/>\n1, \\quad \\omega^Tx &gt; 0 \\<br \/>\n-1, \\quad \\omega^Tx &lt; 0<br \/>\n\\end{cases}<br \/>\n$$<\/p>\n<p>\u8be5\u51b3\u7b56\u51fd\u6570\u7684\u610f\u601d\u662f\u70b9$x=x_0$\u5bf9\u4e8e\u8d85\u5e73\u9762$\\omega{x}+b=0$\uff1a<\/p>\n<ul>\n<li>\u5f53$\\omega{x_0}+b&gt;0$\u4e3a\u6b63\u7c7b<\/li>\n<li>\u5f53$\\omega{x_0}+b&lt;0$\u4e3a\u53cd\u7c7b<\/li>\n<\/ul>\n<h3>1.1.1.1 sign\u51fd\u6570\u56fe\u50cf<\/h3>\n<p>&emsp;&emsp;sign\u51fd\u6570\u4e5f\u79f0\u4f5c\u7b26\u53f7\u51fd\u6570\uff0c\u5f53x&gt;0\u7684\u65f6\u5019y=1\uff1b\u5f53x=0\u7684\u65f6\u5019y=0\uff1b\u5f53x&lt;0\u7684\u65f6\u5019y=-1\u3002sign\u51fd\u6570\u516c\u5f0f\u4e3a<br \/>\n$$<br \/>\ny = \\begin{cases}<br \/>\n1,\\quad x&gt;0 \\<br \/>\n0,\\quad x=0 \\<br \/>\n-1,\\quad x&lt;0 \\<br \/>\n\\end{cases}<br \/>\n$$<\/p>\n<pre><code class=\"language-python\"># sign\u51fd\u6570\u56fe\u50cf\nimport matplotlib.pyplot as plt\nimport numpy as np\n%matplotlib inline\n\nx_list = [i for i in range(-100, 100)]\ny_list = np.sign(x_list)\n\nplt.figure()\nplt.plot(x_list, y_list, color=&#039;r&#039;)\nplt.plot(0, 0, color=&#039;k&#039;, marker=&#039;o&#039;)\nplt.xlabel(&#039;x&#039;)\nplt.ylabel(&#039;y&#039;)\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_9_0.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_9_0.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<h2>1.1.2 \u635f\u5931\u51fd\u6570<\/h2>\n<p>&emsp;&emsp;\u611f\u77e5\u673a\u7684\u635f\u5931\u51fd\u6570\u8868\u793a\u8bef\u5206\u7c7b\u70b9\u5230\u8d85\u5e73\u9762\u7684\u8ddd\u79bb\uff0c\u6e90\u81ea\u4e8e\u6570\u5b66\u4e2d\u70b9$(x_0,y_0)$\u5230\u9762$Ax+By+C=0$\u7684\u516c\u5f0f<br \/>\n$$<br \/>\n\\frac{|Ax_0+By_0+C|}{\\sqrt{A^2+B^2}}<br \/>\n$$<\/p>\n<p>&emsp;&emsp;\u5f53\u6837\u672c\u70b9$(x_i,y_i)$\u4e3a\u8bef\u5206\u7c7b\u70b9\u65f6\uff1a<\/p>\n<ul>\n<li>\u5f53$w^Tx_i&gt;0$\u65f6\uff0c\u672c\u6765\u5e94\u8be5\u662f$y_i=1$\uff0c\u4f46\u7531\u4e8e\u8bef\u5206\u7c7b\uff0c\u6240\u4ee5$y_i=-1$\uff0c$-y_i(w^Tx_i)&gt;0$<\/li>\n<li>\u5f53$w^Tx_i&lt;0$\u65f6\uff0c\u672c\u6765\u5e94\u8be5\u662f$y_i=-1$\uff0c\u4f46\u7531\u4e8e\u8bef\u5206\u7c7b\uff0c\u6240\u4ee5$y_i=1$\uff0c$-y_i(w^Tx_i)&gt;0$<\/li>\n<\/ul>\n<p>&emsp;&emsp;\u56e0\u6b64\u611f\u77e5\u673a\u7b97\u6cd5\u7684\u635f\u5931\u51fd\u6570\u4e3a<br \/>\n$$<br \/>\n-{\\frac{y_i(\\omega^Tx_i)}{||\\omega||}}<br \/>\n$$<\/p>\n<h2>1.1.3 \u76ee\u6807\u51fd\u6570<\/h2>\n<p>&emsp;&emsp;\u611f\u77e5\u673a\u7b97\u6cd5\u7684\u76ee\u6807\u51fd\u6570\u4e3a<br \/>\n$$<br \/>\nJ(\\omega)=\\sum_{{x_i}\\in{M}} -{\\frac{y_i(\\omega^Tx_i)}{||\\omega||}}<br \/>\n$$<br \/>\n\u5176\u4e2d$M$\u662f\u6240\u6709\u8bef\u5206\u7c7b\u70b9\u7684\u96c6\u5408\u3002<\/p>\n<p>&emsp;&emsp;\u8be5\u76ee\u6807\u51fd\u6570\u4e0d\u540c\u4e8e\u5176\u4ed6\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u7684\u5747\u503c\uff0c\u5b83\u662f\u5bf9\u6240\u6709\u8bef\u5206\u7c7b\u70b9\u7684\u8ddd\u79bb\u52a0\u548c\u3002<\/p>\n<p>&emsp;&emsp;\u7531\u4e8e$\\omega^Tx_i=\\omega_1x_1 + \\omega_2x_2 + \\cdots + \\omega_nx_n + b$\uff0c\u5982\u679c$\\omega$\u548c$b$\u6210\u6bd4\u4f8b\u7684\u589e\u52a0\uff0c\u5373\u5206\u5b50\u7684$\\omega$\u548c$b$\u6269\u5927$n$\u500d\u65f6\uff0c\u5206\u6bcd\u7684L2\u8303\u6570\u4e5f\u5c06\u6269\u5927$n$\u500d\uff0c\u4e5f\u5c31\u662f\u8bf4\u5206\u5b50\u548c\u5206\u6bcd\u6709\u56fa\u5b9a\u7684\u500d\u6570\u5173\u7cfb\uff0c\u5373\u53ef\u4ee5\u5c06\u5206\u5b50\u6216\u5206\u6bcd\u56fa\u5b9a\u4e3a$1$\uff0c\u7136\u540e\u6c42\u5206\u5b50\u81ea\u5df1\u6216\u5206\u6bcd\u7684\u5012\u6570\u7684\u6700\u5c0f\u5316\u4f5c\u4e3a\u65b0\u7684\u76ee\u6807\u51fd\u6570\u3002(\u6b64\u5904\u8bb2\u89e3\u62ff\u51fa$b$\uff0c\u4e8b\u5b9e\u4e0a$b$\u5bf9\u7ed3\u679c\u7684\u5f71\u54cd\u5f88\u5c0f\uff0c\u540e\u7eed\u4f1a\u7ee7\u7eed\u7528\u5411\u91cf\u7684\u65b9\u5f0f\uff0c\u5e76\u4e14\u5ffd\u7565$b$)\u3002<\/p>\n<p>&emsp;&emsp;\u611f\u77e5\u673a\u5c06\u5206\u6bcd$||\\omega||$\u56fa\u5b9a\u4e3a$1$\uff0c\u7136\u540e\u5c06\u5206\u5b50\u7684\u6700\u5c0f\u5316\u4f5c\u4e3a\u76ee\u6807\u51fd\u6570\uff0c\u56e0\u6b64\u611f\u77e5\u673a\u7684\u76ee\u6807\u51fd\u6570\u66f4\u65b0\u4e3a<br \/>\n$$<br \/>\nJ(\\omega)=-\\sum_{{x_i}\\in{M}}y_i(\\omega^Tx_i)<br \/>\n$$<\/p>\n<h2>1.1.4 \u76ee\u6807\u51fd\u6570\u4f18\u5316\u95ee\u9898<\/h2>\n<p>\u611f\u77e5\u673a\u7b97\u6cd5\u7684\u76ee\u6807\u51fd\u6570\u4e3a<br \/>\n$$<br \/>\n\\underbrace{arg\\,min}<em>{\\omega}J(\\omega)=\\underbrace{arg\\,min}<\/em>{\\omega} -\\sum_{{x_i}\\in{M}} y_i(\\omega^Tx_i)<br \/>\n$$<\/p>\n<h1>1.2 \u7ebf\u6027\u56de\u5f52<\/h1>\n<p>&emsp;&emsp;\u5047\u8bbe\u623f\u4ef7\u548c\u623f\u5b50\u6240\u5728\u5730\u533a$x_1$\u3001\u5360\u5730\u9762\u79ef$x_2$\u3001\u6237\u578b$x_3$\u548c\u91c7\u5149\u5ea6$x_4$\u6709\u5173\uff0c\u90a3\u4e48\u6211\u662f\u4e0d\u662f\u53ef\u4ee5\u628a\u8fd9\u4e9b\u56e0\u7d20\u5047\u60f3\u6210\u623f\u5b50\u7684\u7279\u5f81\uff0c\u7136\u540e\u7ed9\u8fd9\u4e9b\u6bcf\u4e2a\u7279\u5f81\u90fd\u52a0\u4e0a\u4e00\u4e2a\u76f8\u5e94\u7684\u6743\u91cd$\\omega$\uff0c\u65e2\u53ef\u4ee5\u5f97\u5230\u5982\u4e0b\u7684\u51b3\u7b56\u51fd\u6570<br \/>\n$$<br \/>\n\\hat{y} = \\omega_1x_1 + \\omega_2x_2 + \\omega_3x_3 + \\omega_4x_4 + b<br \/>\n$$<br \/>\n\u5176\u4e2d$b$\u53ef\u4ee5\u7406\u89e3\u4e3a\u504f\u5dee\uff0c\u4f60\u4e5f\u53ef\u4ee5\u60f3\u6210\u623f\u5b50\u7684\u8fd9\u4e9b\u7279\u5f81\u518d\u5dee\u4e5f\u53ef\u80fd\u4f1a\u6709\u4e00\u4e2a\u5e95\u4ef7\u3002<\/p>\n<p>&emsp;&emsp;\u57fa\u4e8e\u4e0a\u8ff0\u7ed9\u51fa\u623f\u4ef7\u7684\u51b3\u7b56\u51fd\u6570\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5bf9\u4e00\u4e2a\u67d0\u4e2a\u4e0d\u77e5\u540d\u7684\u623f\u5b50\u8f93\u5165\u5b83\u7684\u8fd9\u4e9b\u7279\u5f81\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u5f97\u5230\u8fd9\u6240\u623f\u5b50\u7684\u9884\u6d4b\u4ef7\u683c\u4e86\u3002<\/p>\n<h2>1.2.1 \u51b3\u7b56\u51fd\u6570<\/h2>\n<p>\u7ebf\u6027\u56de\u5f52\u7684\u51b3\u7b56\u51fd\u6570\u4e3a<br \/>\n$$<br \/>\n\\hat{y} = f(x) = \\omega_1x_1 + \\omega_2x_2 + \\cdots + \\omega_nx_n + b = w^Tx+b<br \/>\n$$<\/p>\n<h2>1.2.2 \u76ee\u6807\u51fd\u6570<\/h2>\n<p>&emsp;&emsp;\u7ebf\u6027\u56de\u5f52\u7684\u76ee\u6807\u51fd\u6570\u4e3a<br \/>\n$$<br \/>\n\\begin{align}<br \/>\nJ(\\omega,b) &amp; = \\sum_{i=1}^m (y<em>i &#8211; \\hat{y<\/em>{i}})^2 \\<br \/>\n&amp; = \\sum_{i=1}^m (y_i &#8211; w^Tx_i &#8211; b)<br \/>\n\\end{align}<br \/>\n$$<\/p>\n<p>\u5176\u4e2d$y_i$\u4e3a\u771f\u5b9e\u503c\uff0c$\\hat{y_i}$\u4e3a\u9884\u6d4b\u503c\u3002<\/p>\n<p>&emsp;&emsp;\u8be5\u76ee\u6807\u51fd\u6570\u4f7f\u7528\u7684\u635f\u5931\u51fd\u6570\u4e3a\u5e73\u65b9\u635f\u5931\u51fd\u6570\u3002<\/p>\n<h2>1.2.3 \u76ee\u6807\u51fd\u6570\u4f18\u5316\u95ee\u9898<\/h2>\n<p>&emsp;&emsp;\u7ebf\u6027\u56de\u5f52\u7684\u76ee\u6807\u51fd\u6570\u4f18\u5316\u95ee\u9898\u4e3a<br \/>\n$$<br \/>\n\\underbrace{arg\\,min}<em>{\\omega}J(\\omega,b) = \\underbrace{arg\\,min}<\/em>{\\omega}\\sum_{i=1}^m (y_i &#8211; w^Tx_i &#8211; b)<br \/>\n$$<\/p>\n<h1>1.3 \u903b\u8f91\u56de\u5f52\u7b80\u4ecb<\/h1>\n<p>&emsp;&emsp;\u611f\u77e5\u673a\u7b97\u6cd5\u4e2d\u8bb2\u5230\uff0c\u5047\u8bbe\u64cd\u573a\u4e0a\u7537\u751f\u548c\u5973\u751f\u7531\u4e8e\u53d7\u4f20\u7edf\u601d\u60f3\u7684\u5f71\u54cd\uff0c\u7537\u751f\u548c\u5973\u751f\u5206\u5f00\u7ad9\u7740\uff0c\u5e76\u4e14\u56e0\u4e3a\u7537\u751f\u548c\u5973\u751f\u6563\u4e71\u5728\u64cd\u573a\u4e0a\u5448\u7ebf\u6027\u53ef\u5206\u7684\u72b6\u6001\uff0c\u56e0\u6b64\u6211\u4eec\u603b\u53ef\u4ee5\u901a\u8fc7\u611f\u77e5\u673a\u7b97\u6cd5\u627e\u5230\u4e00\u6761\u76f4\u7ebf\u628a\u7537\u751f\u548c\u5973\u751f\u5206\u5f00\uff0c\u5e76\u4e14\u6700\u7ec8\u53ef\u4ee5\u5f97\u5230\u611f\u77e5\u673a\u6a21\u578b\u4e3a<br \/>\n$$<br \/>\nf(x)=sign(w^Tx)<br \/>\n$$<\/p>\n<p>&emsp;&emsp;\u5982\u679c\u4f60\u7ec6\u5fc3\u70b9\u4f1a\u53d1\u73b0\uff0c\u7531\u4e8e\u611f\u77e5\u6a21\u578b\u4f7f\u7528\u7684\u662fsign\u51fd\u6570\uff0c\u5982\u679c\u5f53\u8ba1\u7b97\u4e00\u4e2a\u6837\u672c\u70b9$w^Tx=0.001$\u7684\u65f6\u5019\uff0c$sign(w^Tx)=1$\uff0c\u5373\u8be5\u51fd\u6570\u4f1a\u628a\u8be5\u6837\u672c\u5f52\u4e3a$1$\uff0c\u4f46\u662f\u4e3a\u4ec0\u4e48\u4ed6\u4e0d\u80fd\u662f$0$\u7c7b\u5462\uff1f\u5e76\u4e14\u7531\u4e8esign\u51fd\u6570\u5728$x=0$\u5904\u6709\u4e00\u4e2a\u9636\u8dc3\uff0c\u5373\u51fd\u6570\u4e0d\u8fde\u7eed\uff0c\u8be5\u51fd\u6570\u5728\u6570\u5b66\u4e0a\u4e5f\u662f\u4e0d\u65b9\u4fbf\u5904\u7406\u7684\u3002<\/p>\n<p>&emsp;&emsp;\u7531\u6b64\u903b\u8f91\u51fd\u6570\u4f7f\u7528sigmoid\u51fd\u6570\u5bf9$w^Tx$\u505a\u5904\u7406\uff0c\u5e76\u4e14\u628asigmoid\u51fd\u6570\u5f97\u5230\u7684\u503c$\\hat{y}$\u5f53\u6210\u6982\u7387\u8fdb\u884c\u4e0b\u4e00\u6b65\u5904\u7406\uff0c\u8fd9\u4e5f\u6b63\u662f\u903b\u8f91\u56de\u5f52\u5bf9\u611f\u77e5\u673a\u7684\u6539\u8fdb\u3002<\/p>\n<p>&emsp;&emsp;\u4e0a\u8ff0\u6574\u4e2a\u8fc7\u7a0b\u5176\u5b9e\u5c31\u662f\u903b\u8f91\u56de\u5f52\u4e00\u6b65\u4e00\u6b65\u88ab\u5047\u60f3\u51fa\u6765\u7684\u7684\u4e00\u4e2a\u8fc7\u7a0b\uff0c\u63a5\u4e0b\u6765\u5c06\u4ece\u7406\u8bba\u5c42\u9762\u62bd\u8c61\u7684\u8bb2\u89e3\u903b\u8f91\u56de\u5f52\u3002<\/p>\n<h2>1.3.1 Sigmoid\u51fd\u6570<\/h2>\n<p>&emsp;&emsp;Sigmoid\u51fd\u6570\u516c\u5f0f\u4e3a<br \/>\n$$<br \/>\ng(z) = {\\frac{1}{1+e^{-z}}}<br \/>\n$$<\/p>\n<pre><code class=\"language-python\"># Sigmoid\u51fd\u6570\u56fe\u50cf\u56fe\u4f8b\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\ndef sigmoid(z):\n    return 1 \/ (1 + np.exp(-z))\n\nax = plt.subplot(111)\n\n# \u63cf\u7ed8\u5341\u5b57\u7ebf\nax.spines[&#039;right&#039;].set_color(&#039;none&#039;)\nax.spines[&#039;top&#039;].set_color(&#039;none&#039;)\nax.xaxis.set_ticks_position(&#039;bottom&#039;)\nax.spines[&#039;bottom&#039;].set_position((&#039;data&#039;, 0))\nax.yaxis.set_ticks_position(&#039;left&#039;)\nax.spines[&#039;left&#039;].set_position((&#039;data&#039;, 0))\n\nz = np.linspace(-10, 10, 256)\nhat_y = sigmoid(z)\nplt.plot(z, hat_y, c=&#039;r&#039;, label=&#039;Sigmoid&#039;)\n\n# \u63cf\u7ed8y=0.5\u548cy=1.0\u4e24\u6761\u76f4\u7ebf\nplt.yticks([0.0, 0.5, 1.0])\nax = plt.gca()\nax.yaxis.grid(True)\n\nplt.xlabel(&#039;$w^Tx$&#039;)\nplt.ylabel(&#039;$\\hat{y}$&#039;)\nplt.legend()\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_28_0.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_28_0.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<h2>1.3.2 \u51b3\u7b56\u51fd\u6570<\/h2>\n<p>&emsp;&emsp;\u903b\u8f91\u56de\u5f52\u7684\u51b3\u7b56\u51fd\u6570\u4f7f\u7528\u7684Sigmoid\u51fd\u6570\uff0c\u5373<br \/>\n$$<br \/>\ny = g(\\omega^Tx) = {\\frac{1}{1+e^{-\\omega^Tx}}}<br \/>\n$$<br \/>\n\u5176\u4e2d$g()$\u4e3aSigmoid\u51fd\u6570\u3002<\/p>\n<p>&emsp;&emsp;\u903b\u8f91\u56de\u5f52\u7684\u51b3\u7b56\u51fd\u6570\u4e3a<br \/>\n$$<br \/>\n\\begin{align}<br \/>\n&amp; p(y=1|x,\\omega)=\\pi(x) \\<br \/>\n&amp; p(y=0|x,\\omega)=1-\\pi(x)<br \/>\n\\end{align}<br \/>\n$$<br \/>\n\u5176\u4e2d$1$\u548c$0$\u4ee3\u8868\u4e24\u4e2a\u7c7b\u522b\uff0c\u901a\u8fc7Sigmoid\u51fd\u6570\u56fe\u50cf\u53ef\u4ee5\u770b\u51fa\u5f53$\\omega^Tx&gt;0$\u65f6$y=1$\uff0c\u5f53$\\omega^Tx&lt;0$\u65f6$y=0$\u3002<\/p>\n<h2>1.3.3 \u635f\u5931\u51fd\u6570<\/h2>\n<p>&emsp;&emsp;\u903b\u8f91\u56de\u5f52\u7684\u635f\u5931\u51fd\u6570\u4e3a<br \/>\n$$<br \/>\nL(\\omega) = \\prod_{i=1}^m [\\pi(x_i)]^{y_i}[(1-\\pi(x_i))]^{(1-y_i)}<br \/>\n$$<\/p>\n<ul>\n<li>\u5f53$y<em>i=0$\u65f6\uff0c$L(\\omega) = \\prod<\/em>{i=1}^m [\\pi(x_i)]^{0}[(1-\\pi(x<em>i))]^{(1-0)}=\\prod<\/em>{i=1}^m [(1-\\pi(x_i))]$<\/li>\n<li>\u5f53$y<em>i=1$\u65f6\uff0c$L(\\omega) = \\prod<\/em>{i=1}^m [\\pi(x_i)]^{1}[(1-\\pi(x<em>i))]^{(1-1)}=\\prod<\/em>{i=1}^m [\\pi(x_i)]^{1}$<\/li>\n<\/ul>\n<h2>1.3.4 \u76ee\u6807\u51fd\u6570<\/h2>\n<p>&emsp;&emsp;\u5bf9\u903b\u8f91\u56de\u5f52\u7684\u635f\u5931\u51fd\u6570\u53d6\u5bf9\u6570\uff0c\u5373\u53ef\u5f97\u903b\u8f91\u56de\u5f52\u7684\u76ee\u6807\u51fd\u6570\u4e3a<br \/>\n$$<br \/>\nJ(\\omega) = \\sum_{i=1}^m [y_i\\log\\pi(x_i)+(1-y_i)\\log(1-\\pi(x_i))]<br \/>\n$$<\/p>\n<h2>1.3.5 \u76ee\u6807\u51fd\u6570\u4f18\u5316\u95ee\u9898<\/h2>\n<p>&emsp;&emsp;\u5176\u4e2d\u903b\u8f91\u56de\u5f52\u7684\u76ee\u6807\u51fd\u6570\u7684\u4f18\u5316\u95ee\u9898\u4e3a\u6700\u5927\u5316\u76ee\u6807\u51fd\u6570<br \/>\n$$<br \/>\n\\underbrace{arg\\,max}<em>{\\omega}J(\\omega) = \\underbrace{arg\\,max}<\/em>{\\omega}\\sum_{i=1}^m [y_i\\log\\pi(x_i)+(1-y_i)\\log(1-\\pi(x_i))]<br \/>\n$$<\/p>\n<h1>1.4 \u6734\u7d20\u8d1d\u53f6\u65af\u6cd5\u7b80\u4ecb<\/h1>\n<p>&emsp;&emsp;\u5047\u8bbe\u73b0\u5728\u6709\u4e00\u4e2a\u6709\u4e24\u4e2a\u7c7b\u522b\u7684\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\uff0c\u5e76\u4e14\u5df2\u7ecf\u77e5\u6653\u6bcf\u4e2a\u6570\u636e\u7684\u5206\u7c7b\u60c5\u51b5\uff0c\u5e76\u4e14\u5047\u8bbe\u6570\u636e\u7684\u5206\u5e03\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n<pre><code class=\"language-python\"># \u6734\u7d20\u8d1d\u53f6\u65af\u5f15\u5165\u56fe\u4f8b\nfrom matplotlib.font_manager import FontProperties\nimport matplotlib.pyplot as plt\nfrom sklearn import datasets\n%matplotlib inline\n\nfont = FontProperties(fname=&#039;\/Library\/Fonts\/Heiti.ttc&#039;)\n\niris_data = datasets.load_iris()\nX = iris_data.data[0:100, [0, 1]]\ny = iris_data.target[0:100]\n\nplt.scatter(X[0:50, [0]], X[0:50, [1]], color=&#039;r&#039;,\n            s=50, marker=&#039;o&#039;, label=&#039;\u5c71\u9e22\u5c3e&#039;)\nplt.scatter(X[50:100, [0]], X[50:100, [1]],\n            color=&#039;b&#039;, s=50, marker=&#039;x&#039;, label=&#039;\u6742\u8272\u9e22\u5c3e&#039;)\nplt.scatter(5.5,3.2,color=&#039;g&#039;,marker=&#039;*&#039;,label=&#039;\u65b0\u6837\u672c&#039;,s=100)\n\nplt.xlabel(&#039;\u82b1\u74e3\u957f\u5ea6(cm)&#039;, fontproperties=font)\nplt.ylabel(&#039;\u82b1\u74e3\u5bbd\u5ea6(cm)&#039;, fontproperties=font)\nplt.legend(prop=font)\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_39_0.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_39_0.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>&emsp;&emsp;\u73b0\u5728\u5047\u8bbe\u6709\u4e00\u4e2a\u672a\u77e5\u5206\u7c7b\u7684\u9e22\u5c3e\u82b1\u6570\u636e$(x_1(\u82b1\u74e3\u957f\u5ea6),x_2(\u82b1\u74e3\u5bbd\u5ea6))$\uff0c\u7528$p_1(x_1,x_2)$\u8868\u793a\u6837\u672c\u5c5e\u4e8e\u5c71\u9e22\u5c3e(red)\u7684\u6982\u7387\uff0c\u7528$p_2(x_1,x_2)$\u8868\u793a\u5c5e\u4e8e\u6742\u8272\u9e22\u5c3e(blue)\u7684\u6982\u7387\uff0c$p_1(x_1,x_2) + p_2(x_1,x_2) = 1$\u3002<\/p>\n<p>&emsp;&emsp;\u5047\u8bbe\u5982\u679c$p_1(x_1,x_2) &gt; p_2(x_1,x_2)$\u5219$(x_1,x_2)$\u4e3a\u5c71\u9e22\u5c3e\uff0c\u5426\u5219\u4e3a\u6742\u8272\u9e22\u5c3e\uff0c\u5373\u9009\u62e9\u6982\u7387\u9ad8\u7684\u7c7b\u522b\u4f5c\u4e3a\u65b0\u6837\u672c\u7684\u5206\u7c7b\u7ed3\u679c\u3002\u8fd9\u5c31\u662f\u8d1d\u53f6\u65af\u51b3\u7b56\u7406\u8bba\u7684\u6838\u5fc3\u601d\u60f3\uff0c\u9009\u62e9\u5177\u6709\u6700\u9ad8\u6982\u7387\u7684\u51b3\u7b56\u3002<\/p>\n<p>&emsp;&emsp;\u5982\u679c\u4f7f\u7528\u6761\u4ef6\u6982\u7387\u6765\u8868\u793a\u8fd9\u4e2a\u4e0a\u8ff0\u6240\u8bf4\u7684\u5206\u7c7b\uff0c\u5219\u53ef\u4ee5\u8868\u793a\u4e3a<br \/>\n$$<br \/>\n\\begin{align}<br \/>\n&amp; p(red|x_1,x_2) &gt; p(blue|x_1,x_2) \\quad \\text{\u6837\u672c\u5c5e\u4e8e\u5c71\u9e22\u5c3e} \\<br \/>\n&amp; p(red|x_1,x_2) &lt; p(blue|x_1,x_2) \\quad \\text{\u6837\u672c\u5c5e\u4e8e\u6742\u8272\u9e22\u5c3e}<br \/>\n\\end{align}<br \/>\n$$<br \/>\n\u4e5f\u5c31\u662f\u8bf4\u6c42\u5982\u4e0b\u8868\u8fbe\u5f0f\u7ed3\u679c<br \/>\n$$<br \/>\narg\\,max\\,(p(red|x_1,x_2),p(blue|x_1,x_2))<br \/>\n$$<\/p>\n<p>&emsp;&emsp;\u5982\u679c\u53ea\u6709\u4e24\u4e2a\u7279\u5f81$x_1$\u548c$x<em>2$\uff0c\u90a3\u4e48\u8ba1\u7b97\u5e76\u4e0d\u4f1a\u5f88\u96be\uff0c\u6309\u7167\u6761\u4ef6\u516c\u5f0f\u8ba1\u7b97\u5373\u53ef\uff0c\u4f46\u662f\u4f60\u6709\u6ca1\u6709\u60f3\u8fc7\u5982\u679c\u6709$n$\u7279\u5f81\uff0c$K$\u4e2a\u5206\u7c7b\u5462\uff1f\u5373\u8ba1\u7b97<br \/>\n$$<br \/>\n\\underbrace{arg\\,max}<\/em>{c_k}\\,p(c_j|x_1,x_2,\\ldots,x_n) \\quad(k=1,2,\\cdots,K)<br \/>\n$$<\/p>\n<p>&emsp;&emsp;\u4e0a\u8ff0\u7684\u8ba1\u7b97\u91cf\u662f\u975e\u5e38\u5927\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u6709\u6ca1\u6709\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u80fd\u591f\u6539\u5584\u8be5\u516c\u5f0f\u5462\uff1f\u6709\u662f\u6709\u4e00\u5b9a\u6709\u7684\uff0c\u5373\u6734\u7d20\u8d1d\u53f6\u65af\u6cd5\u3002<\/p>\n<h2>1.4.1 \u8d1d\u53f6\u65af\u516c\u5f0f<\/h2>\n<p>\u82e5\u679c$A$\u548c$B$\u76f8\u4e92\u72ec\u7acb\uff0c\u5e76\u6709\u6761\u4ef6\u6982\u7387\u516c\u5f0f<br \/>\n$$<br \/>\np(A|B) = {\\frac{p(A,B)}{p(B)}}    \\<br \/>\np(B|A) = {\\frac{p(A,B)}{p(A)}}    \\<br \/>\n$$<\/p>\n<p>\u901a\u8fc7\u6761\u4ef6\u6982\u7387\u53ef\u5f97<br \/>\n$$<br \/>\np(A,B) = p(B|A)p(A)               \\<br \/>\np(A|B) = {\\frac{p(B|A)p(A)}{p(B)}} \\quad \\text{\u7b80\u5199\u7684\u8d1d\u53f6\u65af\u516c\u5f0f}<br \/>\n$$<\/p>\n<h2>1.4.2 \u6734\u7d20\u8d1d\u53f6\u65af\u6cd5<\/h2>\n<p>&emsp;&emsp;\u5047\u8bbe\u5df2\u7ecf\u5f97\u5230\u4e86\u8bad\u7ec3\u96c6\u7684$p(c_k)$\u548c$p(x_j|c<em>k)$\u503c\uff0c\u5047\u8bbe\u73b0\u6709\u4e00\u4e2a\u6d4b\u8bd5\u6837\u672c$(x<\/em>{1},x<em>{2},\\ldots,x<\/em>{n})$\uff0c\u5219\u53ef\u4ee5\u6839\u636e\u8d1d\u53f6\u65af\u516c\u5f0f\u6c42\u5f97$K$\u4e2a\u5206\u7c7b$c_1,c_2,\\ldots,c_k$\u5404\u81ea\u7684\u6982\u7387\u3002<br \/>\n$$<br \/>\np(c<em>k|x<\/em>{1},x<em>{2},\\ldots,x<\/em>{n}) = {\\frac{p(x<em>{1},x<\/em>{2},\\ldots,x_{n}|c_k)p(c<em>k)}{p(x<\/em>{1},x<em>{2},\\ldots,x<\/em>{n})}}<br \/>\n$$<\/p>\n<p>&emsp;&emsp;\u5047\u8bbe\u7279\u5f81\u4e4b\u95f4\u76f8\u4e92\u72ec\u7acb\uff0c\u5219\u53ef\u4ee5\u628a\u4e0a\u8ff0\u516c\u5f0f\u8f6c\u5316\u4e3a<br \/>\n$$<br \/>\np(c<em>k|x<\/em>{1},x<em>{2},\\ldots,x<\/em>{n}) = {\\frac{p(x_{1}|c<em>k)p(x<\/em>{2}|c<em>k)\\cdots{p(x<\/em>{n}|c_k)p(c<em>k)}} {p(x<\/em>{1},x<em>{2},\\ldots,x<\/em>{n})}}<br \/>\n$$<\/p>\n<p>&emsp;&emsp;\u6c42\u5f97\u6240\u6709\u5206\u7c7b\u5404\u81ea\u7684\u6982\u7387\u4e4b\u540e\uff0c\u54ea\u4e00\u4e2a\u5206\u7c7b\u7684\u6982\u7387\u6700\u5927\uff0c\u5219\u6837\u672c\u5c5e\u4e8e\u54ea\u4e00\u4e2a\u5206\u7c7b\u3002<br \/>\n$$<br \/>\n\\begin{align}<br \/>\n\u6837\u672c\u7c7b\u522b &amp; = max\\,(p(c<em>1|x<\/em>{1},x<em>{2},\\ldots,x<\/em>{n}),p(c<em>2|x<\/em>{1},x<em>{2},\\ldots,x<\/em>{n}),\\cdots,p(c<em>k|x<\/em>{1},x<em>{2},\\ldots,x<\/em>{n})) \\<br \/>\n&amp; = \\underbrace{arg\\,max}_{c<em>k}\\,{\\frac{p(x<\/em>{1}|c<em>k)p(x<\/em>{2}|c<em>k)\\cdots{p(x<\/em>{n}|c<em>k)}} {p(x<\/em>{1},x<em>{2},\\ldots,x<\/em>{n})}} \\<br \/>\n\\end{align}<br \/>\n$$<br \/>\n\u5176\u4e2d$y = max\\,f(x)$\u8868\u793a$y$\u662f$f(x)$\u4e2d\u6240\u6709\u7684\u503c\u4e2d\u6700\u5927\u7684\u8f93\u51fa\uff1b$y = arg\\,max f(x)$\u8868\u793a$y$\u662f$f(x)$\u4e2d\uff0c\u8f93\u51fa\u7684\u90a3\u4e2a\u53c2\u6570$t$\u3002<\/p>\n<p>&emsp;&emsp;\u7531\u4e8e\u6bcf\u4e00\u4e2a\u7c7b\u522b\u7684\u6982\u7387\u516c\u5f0f\u7684\u5206\u5b50\u90fd\u662f\u76f8\u540c\u7684\uff0c\u628a\u5206\u5b50\u53bb\u6389\u540e\u5219\u53ef\u4ee5\u628a\u4e0a\u8ff0\u516c\u5f0f\u8f6c\u5316\u4e3a\u6734\u7d20\u8d1d\u53f6\u65af\u6a21\u578b\u7684\u57fa\u672c\u516c\u5f0f<br \/>\n$$<br \/>\n\\begin{align}<br \/>\n\u6837\u672c\u7c7b\u522b &amp; = \\underbrace{arg\\,max}_{c<em>k} {p(x<\/em>{1}|c<em>k)p(x<\/em>{2}|c<em>k)\\cdots{p(x<\/em>{n}|c_k)p(c<em>k)}} \\<br \/>\n&amp; = \\underbrace{arg\\,max}<\/em>{c_k}\\,p(c<em>k) \\prod<\/em>{j=1}^{n} p(x_{j}|c_k)<br \/>\n\\end{align}<br \/>\n$$<\/p>\n<h1>1.5 k\u8fd1\u90bb\u7b97\u6cd5\u7b80\u4ecb<\/h1>\n<p>&emsp;&emsp;\u4e0d\u77e5\u9053\u540c\u5b66\u4eec\u5728\u770b\u7535\u5f71\u7684\u65f6\u5019\u6709\u6ca1\u6709\u601d\u8003\u8fc7\u4f60\u662f\u5982\u4f55\u5224\u65ad\u4e00\u90e8\u7535\u5f71\u662f\u7231\u60c5\u7247\u8fd8\u662f\u52a8\u4f5c\u7247\u7684\uff0c\u4f60\u53ef\u4ee5\u505c\u4e0b\u6765\u60f3\u60f3\u8fd9\u4e2a\u95ee\u9898\u518d\u5f80\u4e0b\u770b\u770b\u6211\u7684\u60f3\u6cd5\u3002<\/p>\n<p>&emsp;&emsp;\u6211\u8bf4\u8bf4\u6211\u662f\u5982\u4f55\u5224\u65ad\u4e00\u90e8\u7535\u5f71\u662f\u7231\u60c5\u7247\u8fd8\u662f\u52a8\u4f5c\u7247\u7684\uff0c\u9996\u5148\u7edd\u5bf9\u4e0d\u662f\u9760\u90a3\u4e9b\u9884\u544a\u7247\uff0c\u6bd5\u7adf\u9884\u544a\u7247\u592a\u77ed\u4e86\uff0c\u7231\u60c5\u7247\u7684\u9884\u544a\u53ef\u80fd\u662f\u52a8\u4f5c\u7247\uff0c\u52a8\u4f5c\u7247\u7684\u9884\u544a\u53ef\u80fd\u662f\u7231\u60c5\u7247\u4e5f\u8bf4\u4e0d\u5b9a\u3002<\/p>\n<p>&emsp;&emsp;\u76f8\u6bd4\u8f83\u9884\u544a\u7247\u6211\u7528\u4e86\u4e00\u4e2a\u5f88\u611a\u8822\u5f88\u7e41\u7410\u4f46\u5f88\u6709\u6548\u7684\u65b9\u6cd5\u3002\u9996\u5148\u6211\u7528\u4e00\u90e8\u7535\u5f71\u7684\u63a5\u543b\u955c\u5934\u7684\u6b21\u6570\u4f5c\u4e3a$x$\u8f74\uff0c\u6253\u6597\u7684\u573a\u666f\u6b21\u6570\u4f5c\u4e3a$y$\u8f74\u751f\u6210\u4e86\u4e00\u4e2a\u4e8c\u7ef4\u7a7a\u95f4\uff0c\u5373\u5e73\u9762\u56fe\uff0c\u5982\u4e0b\u56fe\u6240\u793a\u3002(\u6ce8\uff1a\u4ee5\u4e0b\u7535\u5f71\u7684\u63a5\u543b\u955c\u5934\u6b21\u6570\u548c\u6253\u6597\u573a\u666f\u6b21\u6570\u90fd\u662f\u5047\u8bbe\u7684)<\/p>\n<pre><code class=\"language-python\"># k\u8fd1\u90bb\u7b97\u6cd5\u5f15\u5165\u56fe\u4f8b\nimport matplotlib.pyplot as plt\nfrom matplotlib.font_manager import FontProperties\n%matplotlib inline\nfont = FontProperties(fname=&#039;\/Library\/Fonts\/Heiti.ttc&#039;)\n\n# \u52a8\u4f5c\u7247\nplt.scatter(2, 10, marker=&#039;o&#039;, c=&#039;r&#039;, s=50)\nplt.text(2, 9, s=&#039;\u300a\u6218\u72fc2\u300b(2,10)&#039;, fontproperties=font, ha=&#039;center&#039;)\nplt.scatter(4, 12, marker=&#039;o&#039;, c=&#039;r&#039;, s=50)\nplt.text(4, 11, s=&#039;\u300a\u590d\u4ec7\u8005\u8054\u76df2\u300b(4,12)&#039;, fontproperties=font, ha=&#039;center&#039;)\nplt.scatter(2, 15, marker=&#039;o&#039;, c=&#039;r&#039;, s=50)\nplt.text(2, 14, s=&#039;\u300a\u7329\u7403\u5d1b\u8d77\u300b(2,15)&#039;, fontproperties=font, ha=&#039;center&#039;)\n\n# \u7231\u60c5\u7247\nplt.scatter(10, 4, marker=&#039;x&#039;, c=&#039;g&#039;, s=50)\nplt.text(10, 3, s=&#039;\u300a\u6cf0\u5766\u5c3c\u514b\u53f7\u300b(10,4)&#039;, fontproperties=font, ha=&#039;center&#039;)\nplt.scatter(12, 2, marker=&#039;x&#039;, c=&#039;g&#039;, s=50)\nplt.text(12, 1, s=&#039;\u300a\u81f4\u6211\u4eec\u7ec8\u5c06\u901d\u53bb\u7684\u9752\u6625\u300b(12,2)&#039;, fontproperties=font, ha=&#039;center&#039;)\nplt.scatter(15, 5, marker=&#039;x&#039;, c=&#039;g&#039;, s=50)\nplt.text(15, 4, s=&#039;\u300a\u8c01\u7684\u9752\u6625\u4e0d\u8ff7\u832b\u300b(15,5)&#039;, fontproperties=font, ha=&#039;center&#039;)\n\n# \u6d4b\u8bd5\u70b9\nplt.scatter(5, 6, marker=&#039;s&#039;, c=&#039;k&#039;, s=50)\nplt.text(5, 5, s=&#039;\u300a\u672a\u77e5\u7c7b\u578b\u7684\u7535\u5f71\u300b(5,5)&#039;, fontproperties=font, ha=&#039;center&#039;)\n\nplt.xlim(0, 18)\nplt.ylim(0, 18)\nplt.xlabel(&#039;\u63a5\u543b\u955c\u5934(\u6b21)&#039;, fontproperties=font)\nplt.ylabel(&#039;\u6253\u6597\u573a\u666f(\u6b21)&#039;, fontproperties=font)\nplt.title(&#039;k\u8fd1\u90bb\u7b97\u6cd5\u5f15\u5165\u56fe\u4f8b&#039;, fontproperties=font, fontsize=20)\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_47_0.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_47_0.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>&emsp;&emsp;\u901a\u8fc7\u4e0a\u56fe\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\u52a8\u4f5c\u7247\u300a\u6218\u72fc2\u300b\u7684\u6253\u6597\u573a\u666f\u6b21\u6570\u660e\u663e\u591a\u4e8e\u63a5\u543b\u955c\u5934\uff0c\u800c\u7231\u60c5\u7247\u300a\u6cf0\u5766\u5c3c\u514b\u53f7\u300b\u7684\u63a5\u543b\u955c\u5934\u660e\u663e\u591a\u4e8e\u6253\u6597\u573a\u666f\uff0c\u5e76\u4e14\u5176\u4ed6\u7684\u52a8\u4f5c\u7247\u548c\u7231\u60c5\u7247\u90fd\u6709\u8fd9\u6837\u7684\u89c4\u5f8b\uff0c\u56e0\u6b64\u6211\u505a\u4e86\u4e00\u4e2a\u603b\u7ed3\uff1a\u7231\u60c5\u7247\u7684\u63a5\u543b\u955c\u5934\u6b21\u6570\u4f1a\u660e\u663e\u6bd4\u6253\u6597\u7684\u573a\u666f\u6b21\u6570\u591a\uff0c\u800c\u52a8\u4f5c\u7247\u53cd\u4e4b\u3002<\/p>\n<p>&emsp;&emsp;\u901a\u8fc7\u4e0a\u8ff0\u7684\u603b\u7ed3\uff0c\u5982\u679c\u6211\u518d\u53bb\u770b\u4e00\u90e8\u65b0\u7535\u5f71\u7684\u65f6\u5019\uff0c\u6211\u4f1a\u5728\u5fc3\u91cc\u9ed8\u6570\u8fd9\u90e8\u7535\u5f71\u7684\u63a5\u543b\u955c\u5934\u7684\u6b21\u6570\u548c\u6253\u6597\u573a\u666f\uff0c\u7136\u540e\u518d\u53bb\u5224\u65ad\u3002\u8fd9\u79cd\u65b9\u6cd5\u770b\u8d77\u6765\u65e0\u61c8\u53ef\u51fb\uff0c\u4f46\u662f\u5982\u679c\u78b0\u5230\u4e86\u4e0a\u8ff0\u9ed1\u70b9\u300a\u672a\u77e5\u7c7b\u578b\u7684\u7535\u5f71\u300b\u6240\u5728\u7684\u4f4d\u7f6e\uff0c\u5373\u5f53\u63a5\u543b\u955c\u5934\u6b21\u6570\u548c\u6253\u6597\u573a\u666f\u6b21\u6570\u5dee\u4e0d\u591a\u7684\u65f6\u5019\uff0c\u5f80\u5f80\u5f88\u96be\u5224\u65ad\u5b83\u662f\u7231\u60c5\u7247\u8fd8\u662f\u52a8\u4f5c\u7247\u3002<\/p>\n<p>&emsp;&emsp;\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u7684\u4e3b\u89d2k\u8fd1\u90bb\u7b97\u6cd5\u5c31\u5e94\u8be5\u51fa\u573a\u4e86\uff0c\u56e0\u4e3a\u4f7f\u7528k\u8fd1\u90bb\u7b97\u6cd5\u53ef\u4ee5\u5f88\u597d\u7684\u89e3\u51b3\u300a\u672a\u77e5\u7c7b\u578b\u7684\u7535\u5f71\u300b\u8fd9\u79cd\u5c34\u5c2c\u7684\u60c5\u5f62\u3002<\/p>\n<ol>\n<li>\u5047\u8bbe\u6211\u4eec\u628a\u5e73\u9762\u56fe\u4e2d\u7684\u6bcf\u4e00\u4e2a\u7535\u5f71\u770b\u6210\u4e00\u4e2a\u70b9\uff0c\u4f8b\u5982\u300a\u6218\u72fc2\u300b\u662f$(2,10)$\u3001\u300a\u672a\u77e5\u7c7b\u578b\u7684\u7535\u5f71\u300b\u662f$(5,6)$\u3001\u300a\u6cf0\u5766\u5c3c\u514b\u53f7\u300b\u662f$(10,4)$\u2026\u2026<\/li>\n<li>\u6211\u4eec\u4f7f\u7528k\u8fd1\u90bb\u7684\u601d\u60f3\uff0c\u901a\u8fc7\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\u516c\u5f0f\u8ba1\u7b97\u51fa\u6bcf\u4e00\u90e8\u7535\u5f71\u5728\u5e73\u9762\u56fe\u4e2d\u5230\u300a\u672a\u77e5\u7c7b\u578b\u7684\u7535\u5f71\u300b\u7684\u8ddd\u79bb<\/li>\n<li>\u7136\u540e\u5047\u8bbe$k=4$\uff0c\u5373\u83b7\u53d6\u79bb\u300a\u672a\u77e5\u7c7b\u578b\u7684\u7535\u5f71\u300b\u6700\u8fd1\u7684$4$\u90e8\u7535\u5f71<\/li>\n<li>\u5982\u679c\u8fd9$4$\u90e8\u7535\u5f71\u4e2d\u7231\u60c5\u7247\u7c7b\u578b\u7684\u7535\u5f71\u591a\uff0c\u5219\u300a\u672a\u77e5\u7c7b\u578b\u7684\u7535\u5f71\u300b\u662f\u7231\u60c5\u7247\uff1b\u5982\u679c\u52a8\u4f5c\u7247\u7c7b\u578b\u7684\u7535\u5f71\u591a\uff0c\u5219\u300a\u672a\u77e5\u7c7b\u578b\u7684\u7535\u5f71\u300b\u662f\u52a8\u4f5c\u7247\uff1b\u5982\u679c\u4e24\u79cd\u7c7b\u578b\u7684\u7535\u5f71\u6570\u91cf\u4e00\u6837\u591a\uff0c\u5219\u53e6\u9009$k$\u503c<\/li>\n<\/ol>\n<p>&emsp;&emsp;\u4e0a\u8ff0\u6574\u4e2a\u8fc7\u7a0b\u5176\u5b9e\u5c31\u662fk\u8fd1\u90bb\u7b97\u6cd5\u5b9e\u73b0\u7684\u4e00\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<h2>1.5.1 \u8ddd\u79bb\u5ea6\u91cf\u5de5\u5177<\/h2>\n<p>&emsp;&emsp;k\u8fd1\u90bb\u7b97\u6cd5\u5bf9\u4e8e\u8ddd\u79bb\u5ea6\u91cf\u7684\u65b9\u5f0f\uff0c\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u9009\u62e9\uff0c\u4e00\u822c\u9009\u62e9\u6211\u4eec\u7535\u5f71\u5206\u7c7b\u4f8b\u5b50\u4e2d\u8bb2\u5230\u7684\u6b27\u51e0\u91cc\u5f97\u8ddd\u79bb\uff0c\u4e5f\u79f0\u4f5c\u6b27\u6c0f\u8ddd\u79bb\u3002\u540c\u65f6\u8fd8\u53ef\u80fd\u4f1a\u4f7f\u7528\u66fc\u54c8\u987f\u8ddd\u79bb\u6216\u95f5\u53ef\u592b\u65af\u57fa\u8ddd\u79bb\uff0c\u8fd9\u91cc\u7edf\u4e00\u7ed9\u51fa\u5b83\u4eec\u7684\u516c\u5f0f\u3002<\/p>\n<p>&emsp;&emsp;\u5047\u8bbe$n$\u7ef4\u7a7a\u95f4\u4e2d\u6709\u4e24\u4e2a\u70b9$x_i$\u548c$x_j$\uff0c\u5176\u4e2d$x_i = (x_i^{(1)},x_i^{(2)},\\cdots,x_i^{(n)})$\uff0c$x_j = (x_j^{(1)},x_j^{(2)},\\cdots,x_j^{(n)})$\u3002<\/p>\n<p>&emsp;&emsp;\u6b27\u6c0f\u8ddd\u79bb\u4e3a<br \/>\n$$<br \/>\nd(x_i,x<em>j) = \\sqrt{\\sum<\/em>{l=1}^n(x_i^{(l)}-x_j^{(l)})^2}<br \/>\n$$<\/p>\n<p>&emsp;&emsp;\u66fc\u54c8\u987f\u8ddd\u79bb\u4e3a<br \/>\n$$<br \/>\nd(x_i,x<em>j) = \\sum<\/em>{l=1}^n|x_i^{(l)}-x_j^{(l)}|<br \/>\n$$<\/p>\n<p>&emsp;&emsp;\u95f5\u53ef\u592b\u65af\u57fa\u8ddd\u79bb\u4e3a<br \/>\n$$<br \/>\nd(x_i,x<em>j) = \\sqrt[p]{\\sum<\/em>{l=1}^n(|x_i^{(l)}-x_j^{(l)}|)^p}<br \/>\n$$<\/p>\n<p>\u5176\u4e2d\u66fc\u54c8\u987f\u8ddd\u79bb\u662f\u95f5\u53ef\u592b\u65af\u57fa\u8ddd\u79bb$p=1$\u65f6\u7684\u7279\u4f8b\uff0c\u800c\u6b27\u6c0f\u8ddd\u79bb\u662f\u95f5\u53ef\u592b\u65af\u57fa\u8ddd\u79bb$p=2$\u65f6\u7684\u7279\u4f8b\u3002<\/p>\n<h1>1.6 \u51b3\u7b56\u6811\u7b80\u4ecb<\/h1>\n<p>&emsp;&emsp;\u5047\u8bbe\u94f6\u884c\u9700\u8981\u6784\u9020\u4e00\u4e2a\u5f81\u4fe1\u7cfb\u7edf\u7528\u6765\u53d1\u653e\u8d37\u6b3e\uff0c\u5982\u679c\u4f60\u662f\u6784\u5efa\u8be5\u7cfb\u7edf\u7684\u4eba\uff0c\u4f60\u4f1a\u5982\u4f55\u6784\u5efa\u8be5\u7cfb\u7edf\u5462\uff1f<\/p>\n<p>&emsp;&emsp;\u6211\u8bf4\u8bf4\u6211\u5c06\u5982\u4f55\u6784\u5efa\u4e00\u4e2a\u94f6\u884c\u7684\u5f81\u4fe1\u7cfb\u7edf\uff0c\u9996\u5148\uff0c\u6211\u4f1a\u5224\u65ad\u5f81\u4fe1\u4eba\u7684\u5e74\u6536\u5165\u6709\u6ca1\u670950\u4e07\uff0c\u5982\u679c\u6709\u6211\u76f4\u63a5\u5224\u5b9a\u4ed6\u53ef\u4ee5\u8d37\u6b3e\uff0c\u5982\u679c\u6ca1\u6709\u6211\u4f1a\u518d\u505a\u5176\u4ed6\u7684\u5224\u65ad\uff1b\u5176\u6b21\uff0c\u5224\u65ad\u5f81\u4fe1\u4eba\u6709\u6ca1\u6709\u623f\u4ea7\uff0c\u5982\u679c\u6709\u623f\u4ea7\u4e5f\u5c31\u662f\u8bf4\u4ed6\u6709\u4e86\u53ef\u4ee5\u62b5\u62bc\u7684\u4e0d\u52a8\u4ea7\uff0c\u65e2\u53ef\u4ee5\u5224\u5b9a\u4ed6\u53ef\u4ee5\u8d37\u6b3e\uff0c\u5982\u679c\u6ca1\u6709\uff0c\u5219\u4e0d\u80fd\u8d37\u6b3e\u2026\u2026<\/p>\n<p>&emsp;&emsp;\u4e0a\u8ff0\u6574\u4e2a\u8fc7\u7a0b\u5176\u5b9e\u5c31\u662f\u51b3\u7b56\u6811\u5b9e\u73b0\u7684\u4e00\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<h1>1.7 \u652f\u6301\u5411\u91cf\u673a\u7b80\u4ecb<\/h1>\n<pre><code class=\"language-python\"># \u611f\u77e5\u673a\u5f15\u5165\u56fe\u4f8b\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.font_manager import FontProperties\n\n%matplotlib inline\nfont = FontProperties(fname=&#039;\/Library\/Fonts\/Heiti.ttc&#039;)\n\nnp.random.seed(1)\nx1 = np.random.random(20)+1.5\ny1 = np.random.random(20)+0.5\nx2 = np.random.random(20)+3\ny2 = np.random.random(20)+0.5\n\n# \u4e00\u884c\u4e8c\u5217\u7b2c\u4e00\u4e2a\nplt.subplot(121)\nplt.scatter(x1, y1, s=50, color=&#039;b&#039;, label=&#039;\u7537\u5b69(+1)&#039;)\nplt.scatter(x2, y2, s=50, color=&#039;r&#039;, label=&#039;\u5973\u5b69(-1)&#039;)\nplt.vlines(2.8, 0, 2, colors=&quot;r&quot;, linestyles=&quot;-&quot;, label=&#039;$wx+b=0$&#039;)\nplt.title(&#039;\u7ebf\u6027\u53ef\u5206&#039;, fontproperties=font, fontsize=20)\nplt.xlabel(&#039;x&#039;)\nplt.legend(prop=font)\n\n# \u4e00\u884c\u4e8c\u5217\u7b2c\u4e8c\u4e2a\nplt.subplot(122)\nplt.scatter(x1, y1, s=50, color=&#039;b&#039;, label=&#039;\u7537\u5b69(+1)&#039;)\nplt.scatter(x2, y2, s=50, color=&#039;r&#039;, label=&#039;\u5973\u5b69(-1)&#039;)\nplt.scatter(3.5, 1, s=50, color=&#039;b&#039;)\nplt.title(&#039;\u7ebf\u6027\u4e0d\u53ef\u5206&#039;, fontproperties=font, fontsize=20)\nplt.xlabel(&#039;x&#039;)\nplt.legend(prop=font, loc=&#039;upper right&#039;)\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_54_0.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_54_0.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>&emsp;&emsp;\u5728\u4e8c\u7ef4\u7a7a\u95f4\u4e2d\uff0c\u611f\u77e5\u673a\u6a21\u578b\u8bd5\u56fe\u627e\u5230\u4e00\u6761\u76f4\u7ebf\u80fd\u591f\u628a\u4e8c\u5143\u6570\u636e\u5206\u9694\u5f00\uff1b\u5728\u9ad8\u7ef4\u7a7a\u95f4\u4e2d\u611f\u77e5\u673a\u6a21\u578b\u8bd5\u56fe\u627e\u5230\u4e00\u4e2a\u8d85\u5e73\u9762$S$\uff0c\u80fd\u591f\u628a\u4e8c\u5143\u6570\u636e\u9694\u79bb\u5f00\u3002\u8fd9\u4e2a\u8d85\u5e73\u9762$S$\u4e3a$\\omega{x}+b=0$\uff0c\u5728\u8d85\u5e73\u9762$S$\u4e0a\u65b9\u7684\u6570\u636e\u5b9a\u4e49\u4e3a$1$\uff0c\u5728\u8d85\u5e73\u9762$S$\u4e0b\u65b9\u7684\u6570\u636e\u5b9a\u4e49\u4e3a$-1$\uff0c\u5373\u5f53$\\omega{x}&gt;0$\uff0c$\\hat{y}=+1$\uff1b\u5f53$\\omega{x}&lt;0$\uff0c$\\hat{y}=-1$\u3002<\/p>\n<p>&emsp;&emsp;\u4e0a\u5f20\u7ebf\u6027\u53ef\u5206\u548c\u7ebf\u6027\u4e0d\u53ef\u5206\u7684\u533a\u522b\u56fe\u7b2c\u4e00\u5f20\u56fe\u5219\u627e\u5230\u4e86\u4e00\u6761\u76f4\u7ebf\u80fd\u591f\u628a\u4e8c\u5143\u6570\u636e\u5206\u9694\u5f00\uff0c\u4f46\u662f\u80fd\u591f\u53d1\u73b0\u4e8b\u5b9e\u4e0a\u53ef\u80fd\u4e0d\u53ea\u5b58\u5728\u4e00\u6761\u76f4\u7ebf\u5c06\u6570\u636e\u5212\u5206\u4e3a\u4e24\u7c7b\uff0c\u56e0\u6b64\u518d\u627e\u5230\u8fd9\u4e9b\u76f4\u7ebf\u540e\u8fd8\u9700\u8981\u627e\u5230\u4e00\u6761\u6700\u4f18\u76f4\u7ebf\uff0c\u5bf9\u4e8e\u8fd9\u4e00\u70b9\u611f\u77e5\u673a\u6a21\u578b\u4f7f\u7528\u7684\u7b56\u7565\u662f\u8ba9\u6240\u6709\u8bef\u5206\u7c7b\u70b9\u5230\u8d85\u5e73\u9762\u7684\u8ddd\u79bb\u548c\u6700\u5c0f\uff0c\u5373\u6700\u5c0f\u5316\u8be5\u5f0f<br \/>\n$$<br \/>\nJ(\\omega)=\\sum_{{x_i}\\in{M}} {\\frac{- y_i(\\omega{x_i}+b)}{||\\omega||_2}}<br \/>\n$$<\/p>\n<p>&emsp;&emsp;\u4e0a\u5f0f\u4e2d\u53ef\u4ee5\u770b\u51fa\u5982\u679c$\\omega$\u548c$b$\u6210\u6bd4\u4f8b\u7684\u589e\u52a0\uff0c\u5219\u5206\u5b50\u7684$\\omega$\u548c$b$\u6269\u5927$n$\u500d\u65f6\uff0c\u5206\u6bcd\u7684L2\u8303\u6570\u4e5f\u5c06\u6269\u5927$n$\u500d\uff0c\u4e5f\u5c31\u662f\u8bf4\u5206\u5b50\u548c\u5206\u6bcd\u6709\u56fa\u5b9a\u7684\u500d\u6570\u5173\u7cfb\uff0c\u65e2\u53ef\u4ee5\u5206\u6bcd$||\\omega||<em>2$\u56fa\u5b9a\u4e3a$1$\uff0c\u7136\u540e\u6c42\u5206\u5b50\u7684\u6700\u5c0f\u5316\u4f5c\u4e3a\u4ee3\u4ef7\u51fd\u6570\uff0c\u56e0\u6b64\u7ed9\u5b9a\u611f\u77e5\u673a\u7684\u76ee\u6807\u51fd\u6570\u4e3a<br \/>\n$$<br \/>\nJ(\\omega)=\\sum<\/em>{{x_i}\\in{M}} &#8211; y_i(\\omega{x_i}+b)<br \/>\n$$<\/p>\n<p>&emsp;&emsp;\u65e2\u7136\u5206\u5b50\u548c\u5206\u6bcd\u6709\u56fa\u5b9a\u500d\u6570\uff0c\u90a3\u4e48\u53ef\u4e0d\u53ef\u4ee5\u56fa\u5b9a\u5206\u5b50\uff0c\u628a\u5206\u6bcd\u7684\u5012\u6570\u4f5c\u4e3a\u76ee\u6807\u51fd\u6570\u5462\uff1f\u4e00\u5b9a\u662f\u53ef\u4ee5\u7684\uff0c\u56fa\u5b9a\u5206\u5b50\u5c31\u662f\u652f\u6301\u5411\u91cf\u673a\u4f7f\u7528\u7684\u7b56\u7565\u3002<\/p>\n<h2>1.7.1 \u76ee\u6807\u51fd\u6570\u4f18\u5316\u95ee\u9898<\/h2>\n<pre><code class=\"language-python\"># \u95f4\u9694\u6700\u5927\u5316\u56fe\u4f8b\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.font_manager import FontProperties\nfrom sklearn import svm\n%matplotlib inline\nfont = FontProperties(fname=&#039;\/Library\/Fonts\/Heiti.ttc&#039;)\n\nnp.random.seed(8)  # \u4fdd\u8bc1\u6570\u636e\u968f\u673a\u7684\u552f\u4e00\u6027\n\n# \u6784\u9020\u7ebf\u6027\u53ef\u5206\u6570\u636e\u70b9\narray = np.random.randn(20, 2)\nX = np.r_[array-[3, 3], array+[3, 3]]\ny = [0]*20+[1]*20\n\n# \u5efa\u7acbsvm\u6a21\u578b\nclf = svm.SVC(kernel=&#039;linear&#039;)\nclf.fit(X, y)\n\n# \u6784\u9020\u7b49\u7f51\u4e2a\u65b9\u9635\nx1_min, x1_max = X[:, 0].min(), X[:, 0].max(),\nx2_min, x2_max = X[:, 1].min(), X[:, 1].max(),\nx1, x2 = np.meshgrid(np.linspace(x1_min, x1_max),\n                     np.linspace(x2_min, x2_max))\n\n# \u5f97\u5230\u5411\u91cfw: w_0x_1+w_1x_2+b=0\nw = clf.coef_[0]\n# \u52a01\u540e\u624d\u53ef\u7ed8\u5236 -1 \u7684\u7b49\u9ad8\u7ebf [-1,0,1] + 1 = [0,1,2]\nf = w[0]*x1 + w[1]*x2 + clf.intercept_[0] + 1\n\n# \u7ed8\u5236H1\uff0c\u5373wx+b=-1\nplt.contour(x1, x2, f, [0], colors=&#039;k&#039;, linestyles=&#039;--&#039;)\nplt.text(2, -4, s=&#039;$H_2={\\omega}x+b=-1$&#039;, fontsize=10, color=&#039;r&#039;, ha=&#039;center&#039;)\n\n# \u7ed8\u5236\u5206\u9694\u8d85\u5e73\u9762\uff0c\u5373wx+b=0\nplt.contour(x1, x2, f, [1], colors=&#039;k&#039;)\nplt.text(2.5, -2, s=&#039;$\\omega{x}+b=0$&#039;, fontsize=10, color=&#039;r&#039;, ha=&#039;center&#039;)\nplt.text(2.5, -2.5, s=&#039;\u5206\u79bb\u8d85\u5e73\u9762&#039;, fontsize=10,\n         color=&#039;r&#039;, ha=&#039;center&#039;, fontproperties=font)\n\n# \u7ed8\u5236H2\uff0c\u5373wx+b=1\nplt.contour(x1, x2, f, [2], colors=&#039;k&#039;, linestyles=&#039;--&#039;)\nplt.text(3, 0, s=&#039;$H_1=\\omega{x}+b=1$&#039;, fontsize=10, color=&#039;r&#039;, ha=&#039;center&#039;)\n\n# \u7ed8\u5236\u6570\u636e\u6563\u70b9\u56fe\nplt.scatter(X[0:20, 0], X[0:20, 1], cmap=plt.cm.Paired, marker=&#039;x&#039;)\nplt.text(1, 1.8, s=&#039;\u652f\u6301\u5411\u91cf&#039;, fontsize=10, color=&#039;gray&#039;,\n         ha=&#039;center&#039;, fontproperties=font)\n\nplt.scatter(X[20:40, 0], X[20:40, 1], cmap=plt.cm.Paired, marker=&#039;o&#039;)\nplt.text(-1.5, -0.5, s=&#039;\u652f\u6301\u5411\u91cf&#039;, fontsize=10,\n         color=&#039;gray&#039;, ha=&#039;center&#039;, fontproperties=font)\n# plt.scatter(clf.support_vectors_[:,0],clf.support_vectors_[:,1) # \u7ed8\u5236\u652f\u6301\u5411\u91cf\u70b9\n\nplt.xlim(x1_min-1, x1_max+1)\nplt.ylim(x2_min-1, x2_max+1)\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_57_0.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_57_0.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>&emsp;&emsp;\u652f\u6301\u5411\u91cf\u673a\u7684\u76ee\u6807\u51fd\u6570\u7684\u6700\u4f18\u5316\u95ee\u9898\uff0c\u5373\u786c\u95f4\u9694\u6700\u5927\u5316\u4e3a<br \/>\n$$<br \/>\n\\begin{align}<br \/>\n&amp; \\underbrace{\\min}_{\\omega,b} {\\frac{1}{2}}{||\\omega||}^2 \\<br \/>\n&amp; s.t. \\quad y_i(\\omega{x_i}+b)\\geq1, \\quad  i=1,2,\\ldots,m<br \/>\n\\end{align}<br \/>\n$$<\/p>\n<h1>1.8 k\u5747\u503c\u805a\u7c7b\u7b97\u6cd5\u7b80\u4ecb<\/h1>\n<pre><code class=\"language-python\"># k\u5747\u503c\u805a\u7c7b\u7b97\u6cd5\u7b80\u4ecb\u4e3e\u4f8b\nimport matplotlib.pyplot as plt\nfrom matplotlib.font_manager import FontProperties\nfrom sklearn.datasets import make_classification\n%matplotlib inline\nfont = FontProperties(fname=&#039;\/Library\/Fonts\/Heiti.ttc&#039;)\n\nX, y = make_classification(\n    n_samples=500, n_features=20, random_state=1, n_informative=2, shuffle=False)\nplt.scatter(X[0:250, [0]], X[0:250, [1]], c=&#039;r&#039;, s=30)\nplt.scatter(X[250:500, [0]], X[250:500, [1]], c=&#039;g&#039;, s=30)\n\nplt.scatter(2, 1, c=&#039;k&#039;, s=100)\nplt.text(s=&#039;\u521d\u59cb\u70b91$(2,1)$&#039;, x=2, y=1, fontproperties=font, fontsize=15)\nplt.scatter(2, 2, c=&#039;b&#039;, s=100)\nplt.text(s=&#039;\u521d\u59cb\u70b92$(2,2)$&#039;, x=2, y=2, fontproperties=font, fontsize=15)\n\nplt.scatter(-1.2, 0.2, c=&#039;k&#039;, s=100)\nplt.text(s=&#039;\u4e2d\u5fc3\u70b91$(-1.2,0.2)$&#039;, x=-1.2, y=0.2, fontproperties=font, fontsize=15)\nplt.scatter(1, -0.2, c=&#039;b&#039;, s=100)\nplt.text(s=&#039;\u4e2d\u5fc3\u70b91$(1,-0.2)$&#039;, x=1, y=-0.2, fontproperties=font, fontsize=15)\n\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_60_0.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_60_0.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>&emsp;&emsp;\u4ee5\u4e0a\u56fe\u4e3e\u4f8b\uff0ck\u5747\u503c\u7b97\u6cd5\u5219\u662f\u968f\u673a\u5b9a\u4e49\u4e24\u4e2a\u70b9$(2,1)\u3001(2,2)$\uff0c\u7136\u540e\u53ef\u4ee5\u4f7f\u7528k\u5747\u503c\u7b97\u6cd5\u6162\u6162\u6536\u655b\uff0c\u521d\u59cb\u70b9\u6700\u540e\u4f1a\u6536\u655b\u5230\u4e24\u4e2a\u7c07\u7684\u4e2d\u5fc3\uff0c\u8ddd\u79bb\u4e2d\u5fc3\u70b9\u66f4\u8fd1\u7684\u5c06\u88ab\u5212\u5165\u4e2d\u5fc3\u70b9\u4ee3\u8868\u7684\u7c07\u3002\uff08\u6ce8\uff1a\u6b64\u5904\u53ea\u662f\u4e3a\u4e86\u65b9\u4fbf\u4e3e\u4f8b\uff0c\u5e76\u4e0d\u662f\u4f7f\u7528\u4e86\u771f\u6b63\u7684k\u5747\u503c\u7b97\u6cd5\u805a\u7c7b\uff09<\/p>\n<h1>1.9 \u96c6\u6210\u5b66\u4e60\u7b80\u4ecb<\/h1>\n<pre><code class=\"language-python\"># \u96c6\u6210\u5b66\u4e60\u7b80\u4ecb\u56fe\u4f8b\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom matplotlib.font_manager import FontProperties\n%matplotlib inline\nfont = FontProperties(fname=&#039;\/Library\/Fonts\/Heiti.ttc&#039;, size=15)\n\nfig1 = plt.figure()\nax1 = fig1.add_subplot(111, aspect=&#039;equal&#039;)\nax1.add_patch(patches.Rectangle((1, 1), 5, 1.5, color=&#039;b&#039;))\nplt.text(2.6, 3.5, s=&#039;$\\cdots$&#039;, fontsize=30)\nax1.add_patch(patches.Rectangle((1, 5), 5, 1.5, color=&#039;b&#039;))\nax1.add_patch(patches.Rectangle((1, 7), 5, 1.5, color=&#039;b&#039;))\n\nplt.text(3.5, 7.5, s=&#039;\u4e2a\u4f53\u5b66\u4e60\u5668$_1$&#039;, fontsize=20, color=&#039;white&#039;,\n         ha=&#039;center&#039;, fontproperties=font)\nplt.text(3.5, 5.5, s=&#039;\u4e2a\u4f53\u5b66\u4e60\u5668$_2$&#039;, fontsize=20, color=&#039;white&#039;,\n         ha=&#039;center&#039;, fontproperties=font)\nplt.text(3.5, 1.5, s=&#039;\u4e2a\u4f53\u5b66\u4e60\u5668$_T$&#039;, fontsize=20, color=&#039;white&#039;,\n         ha=&#039;center&#039;, fontproperties=font)\n\nplt.annotate(s=&#039;&#039;, xytext=(6, 7.8), xy=(8, 4.7),\n             arrowprops=dict(arrowstyle=&quot;-&gt;&quot;, connectionstyle=&quot;arc3&quot;, color=&#039;orange&#039;))\nplt.annotate(s=&#039;&#039;, xytext=(6, 5.8), xy=(8, 4.2),\n             arrowprops=dict(arrowstyle=&quot;-&gt;&quot;, connectionstyle=&quot;arc3&quot;, color=&#039;orange&#039;))\nplt.annotate(s=&#039;&#039;, xytext=(6, 1.7), xy=(8, 4.0),\n             arrowprops=dict(arrowstyle=&quot;-&gt;&quot;, connectionstyle=&quot;arc3&quot;, color=&#039;orange&#039;))\n\nax1.add_patch(patches.Rectangle((8, 3.4), 4, 2, color=&#039;g&#039;))\nplt.text(10, 4.2, s=&#039;\u7ed3\u5408\u6a21\u5757&#039;, fontsize=20, color=&#039;white&#039;,\n         ha=&#039;center&#039;, fontproperties=font)\n\nplt.annotate(s=&#039;&#039;, xytext=(12, 4.2), xy=(13, 4.2),\n             arrowprops=dict(arrowstyle=&quot;-&gt;&quot;, connectionstyle=&quot;arc3&quot;, color=&#039;orange&#039;))\nax1.add_patch(patches.Rectangle((13, 3.4), 4, 2, color=&#039;purple&#039;))\nplt.text(15, 4.2, s=&#039;\u5f3a\u5b66\u4e60\u5668&#039;, fontsize=20, color=&#039;white&#039;,\n         ha=&#039;center&#039;, fontproperties=font)\n\nplt.annotate(s=&#039;&#039;, xytext=(17, 4.2), xy=(18, 4.2),\n             arrowprops=dict(arrowstyle=&quot;-&gt;&quot;, connectionstyle=&quot;arc3&quot;, color=&#039;orange&#039;))\nplt.text(19, 4, s=&#039;\u8f93\u51fa&#039;, fontsize=20, color=&#039;r&#039;,\n         ha=&#039;center&#039;, fontproperties=font)\n\nplt.xlim(0, 20)\nplt.ylim(0, 10)\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_63_0.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_63_0.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>&emsp;&emsp;\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u96c6\u6210\u5b66\u4e60\u53ef\u4ee5\u7406\u89e3\u6210\uff0c\u82e5\u5e72\u4e2a\u4e2a\u4f53\u5b66\u4e60\u5668\uff0c\u901a\u8fc7\u7ed3\u5408\u7b56\u7565\u6784\u9020\u4e00\u4e2a\u7ed3\u5408\u6a21\u5757\uff0c\u5f62\u6210\u4e00\u4e2a\u5f3a\u5b66\u4e60\u5668\u3002\u5176\u4e2d\u6240\u6709\u7684\u4e2a\u4f53\u5b66\u4e60\u5668\u4e2d\uff0c\u53ef\u4ee5\u662f\u76f8\u540c\u7c7b\u578b\u4e5f\u53ef\u4ee5\u662f\u4e0d\u540c\u7c7b\u578b\u7684\u4e2a\u4f53\u5b66\u4e60\u5668\u3002<\/p>\n<p>&emsp;&emsp;\u56e0\u6b64\u4e3a\u4e86\u83b7\u5f97\u5f3a\u5b66\u4e60\u5668\uff0c\u6211\u4eec\u9996\u5148\u5f97\u83b7\u5f97\u82e5\u5e72\u4e2a\u4e2a\u4f53\u5b66\u4e60\u5668\uff0c\u4e4b\u540e\u9009\u62e9\u4e00\u79cd\u8f83\u597d\u7684\u7ed3\u5408\u7b56\u7565\u3002<\/p>\n<h2>1.9.1 \u4e2a\u4f53\u5b66\u4e60\u5668<\/h2>\n<p>&emsp;&emsp;\u4e0a\u4e00\u8282\u6211\u4eec\u8bb2\u5230\uff0c\u6784\u9020\u5f3a\u5b66\u4e60\u5668\u7684\u6240\u6709\u4e2a\u4f53\u5b66\u4e60\u5668\u4e2d\uff0c\u4e2a\u4f53\u5b66\u4e60\u5668\u53ef\u4ee5\u662f\u76f8\u540c\u7c7b\u578b\u7684\u4e5f\u53ef\u4ee5\u662f\u4e0d\u540c\u7c7b\u578b\u7684\uff0c\u5bf9\u4e8e\u76f8\u540c\u7c7b\u578b\u7684\u4e2a\u4f53\u5b66\u4e60\u5668\uff0c\u8fd9\u6837\u7684\u96c6\u6210\u662f\u540c\u8d28(homogeneous)\u7684\uff0c\u4f8b\u5982\u51b3\u7b56\u6811\u96c6\u6210\u4e2d\u5168\u662f\u51b3\u7b56\u6811\uff0c\u795e\u7ecf\u7f51\u7edc\u96c6\u6210\u4e2d\u5168\u662f\u795e\u7ecf\u7f51\u7edc\uff1b\u5bf9\u4e8e\u4e0d\u540c\u7c7b\u578b\u7684\u4e2a\u4f53\u5b66\u4e60\u5668\uff0c\u8fd9\u6837\u7684\u96c6\u6210\u662f\u5f02\u8d28(heterogenous)\u7684\uff0c\u4f8b\u5982\u67d0\u4e2a\u96c6\u6210\u4e2d\u65e2\u542b\u6709\u51b3\u7b56\u6811\uff0c\u53c8\u542b\u6709\u795e\u7ecf\u7f51\u7edc\u3002<\/p>\n<p>&emsp;&emsp;\u76ee\u524d\u6700\u6d41\u884c\u7684\u662f\u540c\u8d28\u96c6\u6210\uff0c\u5728\u540c\u8d28\u96c6\u6210\u4e2d\uff0c\u4f7f\u7528\u6700\u591a\u7684\u6a21\u578b\u662fCART\u51b3\u7b56\u6811\u548c\u795e\u7ecf\u7f51\u7edc\uff0c\u5e76\u4e14\u4e2a\u4f53\u5b66\u4e60\u5668\u5728\u540c\u8d28\u96c6\u6210\u4e2d\u4e5f\u88ab\u79f0\u4e3a\u5f31\u5b66\u4e60\u5668(weak learner)\u3002\u6309\u7167\u540c\u8d28\u5f31\u5b66\u4e60\u5668\u4e4b\u95f4\u662f\u5426\u5b58\u5728\u4f9d\u8d56\u5173\u7cfb\u53ef\u4ee5\u5c06\u540c\u8d28\u96c6\u6210\u5206\u7c7b\u4e24\u7c7b\uff1a\u7b2c\u4e00\u4e2a\u662f\u5f31\u5b66\u4e60\u5668\u4e4b\u95f4\u5b58\u5728\u5f3a\u4f9d\u8d56\u5173\u7cfb\uff0c\u4e00\u7cfb\u5217\u5f31\u5b66\u4e60\u5668\u57fa\u672c\u90fd\u9700\u8981\u4e32\u884c\u751f\u6210\uff0c\u4ee3\u8868\u7b97\u6cd5\u662fBoosting\u7cfb\u5217\u7b97\u6cd5\uff1b\u7b2c\u4e8c\u4e2a\u662f\u5f31\u5b66\u4e60\u5668\u4e4b\u95f4\u6ca1\u6709\u8f83\u5f3a\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u4e00\u7cfb\u5217\u5f31\u5b66\u4e60\u5668\u53ef\u4ee5\u5e76\u884c\u751f\u6210\uff0c\u4ee3\u8868\u7b97\u6cd5\u662fBagging\u7cfb\u5217\u7b97\u6cd5\uff0cBaggin\u7cfb\u5217\u7b97\u6cd5\u4e2d\u6700\u8457\u540d\u7684\u662f\u968f\u673a\u68ee\u6797(random forest)\u3002<\/p>\n<h2>1.9.2 Boosting\u7cfb\u5217\u7b97\u6cd5<\/h2>\n<p>&emsp;&emsp;Boosting\u662f\u4e00\u79cd\u53ef\u5c06\u5f31\u5b66\u4e60\u5668\u63d0\u5347\u4e3a\u5f3a\u5b66\u4e60\u5668\u7684\u7b97\u6cd5\u3002\u5b83\u7684\u5de5\u4f5c\u673a\u5236\u4e3a\uff1a\u5148\u4ece\u521d\u59cb\u8bad\u7ec3\u96c6\u4e2d\u8bad\u7ec3\u51fa\u4e00\u4e2a\u5f31\u5b66\u4e60\u5668\uff0c\u518d\u6839\u636e\u5f31\u5b66\u4e60\u5668\u7684\u8868\u73b0\u5bf9\u8bad\u7ec3\u6837\u672c\u5206\u5e03\u8fdb\u884c\u8c03\u6574\uff0c\u4f7f\u5f97\u5148\u524d\u5f31\u5b66\u4e60\u5668\u8bad\u7ec3\u9519\u8bef\u7684\u6837\u672c\u6743\u91cd\u53d8\u9ad8\uff0c\u5373\u8ba9\u9519\u8bef\u6837\u672c\u5728\u4e4b\u540e\u7684\u5f31\u5b66\u4e60\u5668\u4e2d\u53d7\u5230\u66f4\u591a\u5173\u6ce8\uff0c\u7136\u540e\u57fa\u4e8e\u8c03\u6574\u540e\u7684\u6837\u672c\u5206\u5e03\u6765\u8bad\u7ec3\u4e0b\u4e00\u4e2a\u5f31\u5b66\u4e60\u5668\u3002<\/p>\n<p>&emsp;&emsp;\u4e0d\u65ad\u91cd\u590d\u4e0a\u8ff0\u8fc7\u7a0b\uff0c\u76f4\u5230\u5f31\u5b66\u4e60\u5668\u6570\u8fbe\u5230\u4e8b\u5148\u6307\u5b9a\u7684\u6570\u76ee$T$\uff0c\u6700\u7ec8\u901a\u8fc7\u96c6\u5408\u7b56\u7565\u6574\u5408\u8fd9$T$\u4e2a\u5f31\u5b66\u4e60\u5668\uff0c\u5f97\u5230\u6700\u7ec8\u7684\u5f3a\u5b66\u4e60\u5668\u3002<\/p>\n<p>&emsp;&emsp;Boosting\u7cfb\u5217\u7b97\u6cd5\u4e2d\u6700\u8457\u540d\u7684\u7b97\u6cd5\u6709AdaBoost\u7b97\u6cd5\u548c\u63d0\u5347\u6811(boosting tree)\u7cfb\u5217\u7b97\u6cd5\uff0c\u63d0\u5347\u6811\u7cfb\u5217\u7b97\u6cd5\u4e2d\u5e94\u7528\u6700\u5e7f\u6cdb\u7684\u662f\u68af\u5ea6\u63d0\u5347\u6811(gradient boosting tree)\u3002<\/p>\n<p>&emsp;&emsp;Boosting\u7531\u4e8e\u6bcf\u4e00\u4e2a\u5f31\u5b66\u4e60\u5668\u90fd\u57fa\u4e8e\u4e0a\u4e00\u4e2a\u5f31\u5b66\u4e60\u5668\uff0c\u56e0\u6b64\u5b83\u7684\u504f\u5dee\u8f83\u5c0f\uff0c\u5373\u6a21\u578b\u62df\u5408\u80fd\u529b\u8f83\u5f3a\uff0c\u4f46\u662f\u6a21\u578b\u6cdb\u5316\u80fd\u529b\u4f1a\u7a0d\u5dee\uff0c\u5373\u65b9\u5dee\u504f\u5927\uff0c\u800cBoosgting\u5219\u662f\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u80fd\u51cf\u5c0f\u65b9\u5dee\u7684\u5b66\u4e60\u5668\uff0c\u4e00\u822c\u9009\u62e9\u8f83\u7b80\u5355\u6a21\u578b\uff0c\u5982\u9009\u62e9\u6df1\u5ea6\u5f88\u6d45\u7684\u51b3\u7b56\u6811\u3002<\/p>\n<h2>1.9.3 Bagging\u7cfb\u5217\u7b97\u6cd5<\/h2>\n<p>&emsp;&emsp;Boosting\u7684\u5f31\u5b66\u4e60\u5668\u4e4b\u95f4\u662f\u6709\u4f9d\u8d56\u5173\u7cfb\u7684\uff0c\u800cBagging\u7684\u5f31\u5b66\u4e60\u5668\u4e4b\u95f4\u662f\u6ca1\u6709\u4f9d\u8d56\u5173\u7cfb\u7684\uff0c\u56e0\u6b64\u5b83\u7684\u5f31\u5b66\u4e60\u5668\u662f\u5e76\u884c\u751f\u6210\u3002<\/p>\n<p>&emsp;&emsp;Bagging\u7684\u5f31\u5b66\u4e60\u5668\u7684\u8bad\u7ec3\u96c6\u662f\u901a\u8fc7\u968f\u673a\u91c7\u6837\u5f97\u5230\u7684\u3002\u901a\u8fc7$T$\u6b21\u7684\u968f\u673a\u91c7\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u81ea\u4e3b\u91c7\u6837\u6cd5(bootstrap sampling)\u5f97\u5230$T$\u4e2a\u91c7\u6837\u96c6\uff0c\u7136\u540e\u5bf9\u4e8e\u8fd9$T$\u4e2a\u91c7\u6837\u96c6\u72ec\u7acb\u7684\u8bad\u7ec3\u51fa$T$\u4e2a\u5f31\u5b66\u4e60\u5668\uff0c\u4e4b\u540e\u6211\u4eec\u901a\u8fc7\u67d0\u79cd\u7ed3\u5408\u7b56\u7565\u5c06\u8fd9$T$\u4e2a\u5f31\u5b66\u4e60\u5668\u6784\u9020\u6210\u4e00\u4e2a\u5f3a\u5b66\u4e60\u5668\u3002<\/p>\n<p>&emsp;&emsp;Bagging\u7cfb\u5217\u7b97\u6cd5\u4e2d\u6700\u8457\u540d\u7684\u7b97\u6cd5\u6709\u968f\u673a\u68ee\u6797\uff0c\u4f46\u662f\u968f\u673a\u68ee\u6797\u53ef\u4ee5\u8bf4\u662f\u4e00\u4e2a\u8fdb\u9636\u7248\u7684Bagging\u7b97\u6cd5\uff0c\u867d\u7136\u968f\u673a\u68ee\u6797\u7684\u5f31\u5b66\u4e60\u5668\u90fd\u662f\u51b3\u7b56\u6811\uff0c\u4f46\u662f\u968f\u673a\u68ee\u6797\u5728Baggin\u7684\u6837\u672c\u968f\u673a\u91c7\u6837\u7684\u57fa\u7840\u4e0a\uff0c\u53c8\u8fdb\u884c\u4e86\u7279\u5f81\u7684\u968f\u673a\u9009\u62e9\u3002<\/p>\n<p>&emsp;&emsp;Bagging\u7531\u4e8e\u901a\u8fc7\u968f\u673a\u91c7\u6837\u83b7\u5f97\u6570\u636e\uff0c\u56e0\u6b64\u5b83\u7684\u65b9\u5dee\u8f83\u5c0f\uff0c\u5373\u6a21\u578b\u6cdb\u5316\u80fd\u529b\u8f83\u5f3a\uff0c\u4f46\u662f\u6a21\u578b\u62df\u5408\u80fd\u529b\u8f83\u5f31\uff0c\u5373\u504f\u5dee\u504f\u5927\uff0c\u800cBagging\u5219\u662f\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u80fd\u51cf\u5c0f\u504f\u5dee\u7684\u5b66\u4e60\u5668\uff0c\u4e00\u822c\u9009\u62e9\u8f83\u590d\u6742\u6a21\u578b\uff0c\u5982\u9009\u62e9\u6df1\u5ea6\u5f88\u6df1\u7684\u51b3\u7b56\u6811\u6216\u4e0d\u526a\u679d\u7684\u51b3\u7b56\u6811\u3002<\/p>\n<h1>1.10 \u964d\u7ef4\u7b97\u6cd5\u7b80\u4ecb<\/h1>\n<h2>1.10.1 \u7ef4\u6570\u707e\u96be\u548c\u964d\u7ef4<\/h2>\n<p>&emsp;&emsp;\u5bf9\u4e8e\u9ad8\u7ef4\u6570\u636e\uff0c\u4f1a\u51fa\u73b0\u6570\u636e\u6837\u672c\u7a00\u758f\u3001\u8ddd\u79bb\u8ba1\u7b97\u56f0\u96be\u7b49\u95ee\u9898\u3002\u5c24\u5176\u662f\u5728KNN\u7b97\u6cd5\u4e2d\u8fd9\u79cd\u95ee\u9898\u4f1a\u88ab\u653e\u5927\uff0c\u800c\u5176\u4ed6\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u4e5f\u4f1a\u56e0\u4e3a\u9ad8\u7ef4\u6570\u636e\u5bf9\u8bad\u7ec3\u6a21\u578b\u9020\u6210\u6781\u5927\u7684\u969c\u788d\uff0c\u8fd9\u79cd\u95ee\u9898\u4e00\u822c\u88ab\u79f0\u4e3a\u7ef4\u6570\u707e\u96be(curse of dimensionality)\u3002<\/p>\n<p>&emsp;&emsp;\u89e3\u51b3\u7ef4\u6570\u707e\u96be\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u964d\u7ef4(dimension reduction)\uff0c\u5373\u901a\u8fc7\u67d0\u79cd\u6570\u5b66\u53d8\u6362\u5c06\u539f\u59cb\u9ad8\u7ef4\u7279\u5f81\u7a7a\u95f4\u8f6c\u53d8\u4e3a\u4e00\u4e2a\u4f4e\u7ef4\u5b50\u7a7a\u95f4\uff0c\u5728\u8fd9\u4e2a\u5b50\u7a7a\u95f4\u4e2d\u6837\u672c\u5bc6\u5ea6\u5927\u5e45\u63d0\u9ad8\uff0c\u8ddd\u79bb\u8ba1\u7b97\u4e5f\u53d8\u5f97\u66f4\u5bb9\u6613\u3002<\/p>\n<pre><code class=\"language-python\"># \u7ef4\u6570\u707e\u96be\u548c\u964d\u7ef4\u56fe\u4f8b\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.font_manager import FontProperties\nfrom sklearn.decomposition import PCA\n%matplotlib inline\nfont = FontProperties(fname=&#039;\/Library\/Fonts\/Heiti.ttc&#039;)\n\nnp.random.seed(0)\nX = np.empty((100, 2))\nX[:, 0] = np.random.uniform(0, 100, size=100)\nX[:, 1] = 0.75 * X[:, 0] + 3. + np.random.normal(0, 10, size=100)\npca = PCA(n_components=1)\nX_reduction = pca.fit_transform(X)\nX_restore = pca.inverse_transform(X_reduction)\n\nplt.scatter(X[:, 0], X[:, 1], color=&#039;g&#039;, label=&#039;\u539f\u59cb\u6570\u636e&#039;)\nplt.scatter(X_restore[:, 0], X_restore[:, 1],\n            color=&#039;r&#039;, label=&#039;\u964d\u7ef4\u540e\u7684\u6570\u636e&#039;)\nplt.annotate(s=&#039;&#039;, xytext=(40, 60), xy=(65, 30),\n             arrowprops=dict(arrowstyle=&#039;-&#039;, color=&#039;b&#039;, linewidth=5))\nplt.legend(prop=font)\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_74_0.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/02\/08-02-\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406_74_0.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>&emsp;&emsp;\u5982\u4e0a\u56fe\u6240\u793a\uff0c\u7eff\u70b9\u5373\u539f\u59cb\u9ad8\u7ef4\u7a7a\u95f4\u4e2d\u7684\u6837\u672c\u70b9\uff0c\u7ea2\u70b9\u5373\u6211\u4eec\u964d\u7ef4\u540e\u7684\u6837\u672c\u70b9\u3002\u7531\u4e8e\u56fe\u4e2d\u7684\u9ad8\u7ef4\u662f\u4e8c\u7ef4\uff0c\u4f4e\u7ef4\u662f\u4e00\u7ef4\uff0c\u6240\u4ee5\u6837\u672c\u5728\u4f4e\u7ef4\u7a7a\u95f4\u662f\u4e00\u6761\u76f4\u7ebf\u3002<\/p>\n<p>&emsp;&emsp;\u63a5\u4e0b\u6765\u6211\u4eec\u7684\u76ee\u6807\u5c31\u662f\u804a\u4e00\u804a\u5982\u4f55\u505a\u5230\u628a\u9ad8\u7ef4\u7a7a\u95f4\u6837\u672c\u70b9\u6620\u5c04\u5230\u4f4e\u7ef4\u7a7a\u95f4\uff0c\u5373\u5404\u79cd\u964d\u7ef4\u7b97\u6cd5\u3002<\/p>\n<h2>1.10.2 \u4e3b\u6210\u5206\u5206\u6790<\/h2>\n<p>&emsp;&emsp;\u4e3b\u6210\u5206\u5206\u6790(principal component analysis\uff0cPCA)\u662f\u6700\u5e38\u7528\u7684\u4e00\u79cd\u964d\u7ef4\u65b9\u6cd5\uff0c\u6211\u4eec\u5df2\u7ecf\u5229\u7528\u201c\u7ef4\u6570\u707e\u96be\u548c\u964d\u7ef4\u56fe\u4f8b\u201d\u89e3\u91ca\u4e86\u964d\u7ef4\u7684\u8fc7\u7a0b\uff0cPCA\u7684\u964d\u7ef4\u8fc7\u7a0b\u5219\u662f\u5c3d\u53ef\u80fd\u7684\u4f7f\u7528\u6570\u636e\u6700\u4e3b\u8981\u7684\u7279\u5f81\u6765\u4ee3\u8868\u6570\u636e\u539f\u6709\u7684\u6240\u6709\u7279\u5f81\u3002\u4f46\u662f\u6709\u6ca1\u6709\u540c\u5b66\u60f3\u8fc7\u4e3a\u4ec0\u4e48\u4f7f\u7528PCA\u964d\u7ef4\u662f\u4e0a\u56fe\u7684\u7ea2\u70b9\u7ec4\u6210\u7684\u7ebf\u800c\u4e0d\u662f\u84dd\u7ebf\u5462\uff1f\u8fd9\u91cc\u5c31\u9700\u8981\u8bf4\u5230\u6211\u4eecPCA\u7684\u4e24\u4e2a\u6761\u4ef6\u4e86\u3002<\/p>\n<p>&emsp;&emsp;\u5bf9\u4e8e\u201c\u7ef4\u6570\u707e\u96be\u548c\u964d\u7ef4\u56fe\u4f8b\u201d\u4e2d\u7684\u7ea2\u7ebf\u548c\u84dd\u7ebf\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u770b\u6210\u4e00\u4e2a\u8d85\u5e73\u9762$S$\uff0c\u7406\u8bba\u4e0a\u7ea2\u7ebf\u548c\u84dd\u7ebf\u6784\u6210\u7684\u8d85\u5e73\u9762\u90fd\u53ef\u4ee5\u505a\u5230\u5bf9\u6837\u672c\u7279\u5f81\u7684\u964d\u7ef4\uff0c\u4f46\u662f\u4e00\u822c\u6211\u4eec\u5e0c\u671b\u8fd9\u79cd\u80fd\u591f\u505a\u5230\u964d\u7ef4\u7684\u8d85\u5e73\u9762\u6ee1\u8db3\u4ee5\u4e0b\u4e24\u4e2a\u6761\u4ef6<\/p>\n<ol>\n<li>\u6700\u8fd1\u91cd\u6784\u6027\uff1a\u6837\u672c\u70b9\u5230\u8fd9\u4e2a\u8d85\u5e73\u9762\u7684\u8ddd\u79bb\u90fd\u8db3\u591f\u8fd1<\/li>\n<li>\u6700\u5927\u53ef\u5206\u6027\uff1a\u6837\u672c\u70b9\u5230\u8fd9\u4e2a\u8d85\u5e73\u9762\u4e0a\u7684\u6295\u5f71\u5c3d\u53ef\u80fd\u5206\u5f00<\/li>\n<\/ol>\n<p>&emsp;&emsp;\u57fa\u4e8e\u6700\u8fd1\u91cd\u6784\u6027\u548c\u6700\u5927\u53ef\u5206\u6027\uff0c\u5c31\u53ef\u4ee5\u5f97\u5230\u4e3b\u6210\u5206\u5206\u6790\u7684\u4e24\u79cd\u7b49\u4ef7\u63a8\u5bfc\uff0c\u4e5f\u53ef\u4ee5\u5f97\u51fa\u4e3a\u4ec0\u4e48\u7ea2\u7ebf\u662f\u66f4\u5408\u7406\u7684\u8d85\u5e73\u9762\u3002<\/p>\n<h1>1.11 \u672c\u7ae0\u5c0f\u7ed3<\/h1>\n<p>&emsp;&emsp;\u672c\u7ae0\u4e3b\u8981\u662f\u5e26\u5927\u5bb6\u5bf9\u4f20\u7edf\u673a\u5668\u5b66\u4e60\u7684\u7b97\u6cd5\u6709\u4e00\u4e2a\u5927\u6982\u7684\u4e86\u89e3\uff0c\u7531\u4e8e\u795e\u7ecf\u7f51\u7edc\u53ef\u4ee5\u7406\u89e3\u6210\u591a\u4e2a\u611f\u77e5\u673a\u7ec4\u6210\u7684\u7b97\u6cd5\uff0c\u6b64\u5904\u4e0d\u591a\u4ecb\u7ecd\u3002\u540e\u671f\u8fdb\u9636\u8bfe\u7a0b\u5c06\u4f1a\u6d89\u53ca\u795e\u7ecf\u7f51\u7edc\u4ee5\u53ca\u6df1\u5ea6\u5b66\u4e60\u3002<\/p>\n<p>&emsp;&emsp;\u7531\u4e8e\u672c\u8bfe\u7a0b\u7247\u5e94\u7528\uff0c\u6240\u4ee5\u7b97\u6cd5\u90fd\u662f\u70b9\u5230\u4e3a\u6b62\uff0c\u5982\u679c\u9700\u8981\u5bf9\u7b97\u6cd5\u539f\u7406\u60f3\u8981\u6709\u4e00\u4e2a\u6e05\u6670\u8ba4\u8bc6\u7684\u540c\u5b66\uff0c\u53ef\u4ee5\u770b\u6211\u4eec\u672a\u6765\u7684\u8fdb\u9636\u8bfe\u7a0b\uff0c\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u6bcf\u4e00\u4e2a\u51fd\u6570\u7684\u635f\u5931\u51fd\u6570\u3001\u76ee\u6807\u51fd\u6570\u3001\u76ee\u6807\u51fd\u6570\u4f18\u5316\u95ee\u9898\u4e3a\u4ec0\u4e48\u662f\u8fd9\u6837\u7684\uff0c\u656c\u8bf7\u671f\u5f85\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u539f\u7406 1.1 \u611f\u77e5\u673a\u7b97\u6cd5 # \u611f\u77e5\u673a\u7b97\u6cd5\u56fe\u4f8b import numpy as np import  [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":3275,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[276,301],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/3258"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3258"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/3258\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/media\/3275"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}