{"id":20089,"date":"2026-01-16T16:41:24","date_gmt":"2026-01-16T08:41:24","guid":{"rendered":"https:\/\/92it.top\/?p=20089"},"modified":"2026-01-16T16:41:24","modified_gmt":"2026-01-16T08:41:24","slug":"%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0_%e8%ae%ad%e7%bb%83%e9%9b%86%e6%b5%8b%e8%af%95%e9%9b%86%e5%88%92%e5%88%86","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=20089","title":{"rendered":"\u673a\u5668\u5b66\u4e60_\u8bad\u7ec3\u96c6\u6d4b\u8bd5\u96c6\u5212\u5206"},"content":{"rendered":"\n<p><strong>\u524d\u8a00\u200b\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u673a\u5668\u5b66\u4e60\u7684\u4e16\u754c\u91cc\uff0c\u6570\u636e\u662f\u9a71\u52a8\u4e00\u5207\u6a21\u578b\u7684\u71c3\u6599\uff0c\u7136\u800c\uff0c\u5982\u4f55\u6b63\u786e\u5730\u4f7f\u7528\u8fd9\u4e9b\u71c3\u6599\uff0c\u51b3\u5b9a\u4e86\u4f60\u7684\u6a21\u578b\u662f\u80fd\u7cbe\u51c6\u9884\u6d4b\u672a\u6765\u7684\u667a\u80fd\u5f15\u64ce\uff0c\u8fd8\u662f\u4e00\u4e2a\u53ea\u4f1a\u6b7b\u8bb0\u786c\u80cc\u7684\u590d\u8bfb\u673a\u3002<\/p>\n\n\n\n<p>\u4eca\u5929\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u673a\u5668\u5b66\u4e60\u4e2d\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u4e14\u57fa\u7840\u7684\u6982\u5ff5\uff1a<strong>\u8bad\u7ec3\u96c6\u4e0e\u6d4b\u8bd5\u96c6\u7684\u5212\u5206<\/strong>\u3002\u8fd9\u662f\u4f60\u6784\u5efa\u4efb\u4f55\u53ef\u9760\u6a21\u578b\u7684\u7b2c\u4e00\u6b65\uff0c\u4e5f\u662f\u8bc4\u4f30\u6a21\u578b\u771f\u5b9e\u80fd\u529b\u7684\u5173\u952e\u3002<\/p>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u7684\u5212\u5206\uff0c\u5c31\u50cf\u5b66\u751f\u65f6\u4ee3\u7684\u5b66\u4e60\u4e0e\u8003\u8bd5\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>\u8bad\u7ec3\u96c6<\/strong>\u00a0\u662f\u5b66\u751f\u7684\u6559\u6750\u548c\u7ec3\u4e60\u9898\uff0c\u6a21\u578b\u7528\u5b83\u6765\u5b66\u4e60\u6570\u636e\u4e2d\u7684\u89c4\u5f8b\u548c\u6a21\u5f0f\u3002<\/li>\n\n\n\n<li><strong>\u6d4b\u8bd5\u96c6<\/strong>\u00a0\u662f\u6700\u7ec8\u7684\u671f\u672b\u8003\u8bd5\uff0c\u6a21\u578b\u7528\u5b83\u6765\u68c0\u9a8c\u81ea\u5df1\u662f\u5426\u771f\u6b63\u638c\u63e1\u4e86\u77e5\u8bc6\uff0c\u800c\u4e0d\u662f\u4ec5\u4ec5\u8bb0\u4f4f\u4e86\u7ec3\u4e60\u9898\uff08\u8bad\u7ec3\u96c6\uff09\u7684\u7b54\u6848\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u4e3a\u4ec0\u4e48\u5fc5\u987b\u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\uff1f\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u4e00\u4e2a\u5b66\u751f\u53ea\u590d\u4e60\u4e86\u8001\u5e08\u7ed9\u7684\u6a21\u62df\u9898\uff0c\u5e76\u4e14\u8003\u8bd5\u9898\u76ee\u5c31\u662f\u4e00\u6a21\u4e00\u6837\u7684\u6a21\u62df\u9898\uff0c\u4ed6\u5f97\u4e86\u6ee1\u5206\u3002\u8fd9\u80fd\u8bc1\u660e\u4ed6\u771f\u6b63\u7406\u89e3\u4e86\u8fd9\u4e2a\u5b66\u79d1\u5417\uff1f\u663e\u7136\u4e0d\u80fd\u3002\u4ed6\u53ef\u80fd\u53ea\u662f\u8bb0\u4f4f\u4e86\u7b54\u6848\u3002<\/p>\n\n\n\n<p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u5982\u679c\u6211\u4eec\u5728<strong>\u5168\u90e8\u6570\u636e<\/strong>\u4e0a\u8bad\u7ec3\u6a21\u578b\uff0c\u7136\u540e\u53c8\u7528\u8fd9<strong>\u540c\u4e00\u4efd\u6570\u636e<\/strong>\u53bb\u8bc4\u4f30\u5b83\u7684\u6027\u80fd\uff0c\u5c31\u4f1a\u72af\u540c\u6837\u7684\u9519\u8bef\u3002\u6a21\u578b\u4f1a\u8868\u73b0\u5f97\u5f02\u5e38\u51fa\u8272\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf&#8221;\u89c1\u8fc7&#8221;\u5e76&#8221;\u8bb0\u4f4f&#8221;\u4e86\u6240\u6709\u6570\u636e\u7684\u7ec6\u8282\uff0c\u5305\u62ec\u5176\u4e2d\u7684\u566a\u58f0\u548c\u5076\u7136\u6027\u3002\u8fd9\u79cd\u73b0\u8c61\u88ab\u79f0\u4e3a<strong>\u8fc7\u62df\u5408<\/strong>\u3002<\/p>\n\n\n\n<p>\u8fc7\u62df\u5408\u7684\u6a21\u578b\u5c31\u50cf\u4e00\u4e2a\u53ea\u4f1a\u80cc\u8bf5\u4f8b\u9898\u7684\u5b66\u751f\uff0c\u4e00\u65e6\u9047\u5230\u65b0\u7684\u3001\u6ca1\u89c1\u8fc7\u7684\u9898\u76ee\uff08\u65b0\u6570\u636e\uff09\uff0c\u5c31\u4f1a\u8868\u73b0\u5f97\u5f88\u5dee\u3002\u5b83\u7684&#8221;\u6cdb\u5316\u80fd\u529b&#8221;\u5f88\u5f31\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u5fc5\u987b\u5c06\u6570\u636e\u5206\u6210\u4e24\u90e8\u5206\uff1a<\/p>\n\n\n\n<ol>\n<li><strong>\u8bad\u7ec3\u96c6<\/strong>\uff1a\u7528\u4e8e<strong>\u6559<\/strong>\u6a21\u578b\uff0c\u8ba9\u5b83\u5b66\u4e60\u3002<\/li>\n\n\n\n<li><strong>\u6d4b\u8bd5\u96c6<\/strong>\uff1a\u7528\u4e8e<strong>\u8003<\/strong>\u6a21\u578b\uff0c\u8bc4\u4f30\u5b83\u5904\u7406<strong>\u4ece\u672a\u89c1\u8fc7\u7684\u65b0\u6570\u636e<\/strong>\u7684\u80fd\u529b\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u6d4b\u8bd5\u96c6\u5fc5\u987b\u4e0e\u8bad\u7ec3\u96c6\u5b8c\u5168\u9694\u79bb\uff0c\u5728\u6574\u4e2a\u6a21\u578b\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u90fd<strong>\u4e0d\u80fd<\/strong>\u88ab\u6a21\u578b\u770b\u5230\u3002\u53ea\u6709\u8fd9\u6837\uff0c\u6d4b\u8bd5\u96c6\u4e0a\u7684\u8bc4\u4f30\u7ed3\u679c\u624d\u80fd\u5ba2\u89c2\u5730\u53cd\u6620\u6a21\u578b\u7684\u771f\u5b9e\u6cdb\u5316\u80fd\u529b\u3002<\/p>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u5982\u4f55\u5212\u5206\uff1a\u5e38\u7528\u65b9\u6cd5\u4e0e\u7b56\u7565\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5212\u5206\u6570\u636e\u542c\u8d77\u6765\u7b80\u5355\uff0c\u4f46\u5176\u4e2d\u4e5f\u6709\u4e0d\u5c11\u5b66\u95ee\u3002\u4e0d\u540c\u7684\u5212\u5206\u7b56\u7565\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002<\/p>\n\n\n\n<p><strong>\ud83d\udd391. \u7b80\u5355\u968f\u673a\u5212\u5206<\/strong><\/p>\n\n\n\n<p>\u8fd9\u662f\u6700\u57fa\u7840\u3001\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u5c06\u6574\u4e2a\u6570\u636e\u96c6\u968f\u673a\u6253\u4e71\uff0c\u7136\u540e\u6309\u4e00\u5b9a\u6bd4\u4f8b\u5207\u5206\u6210\u4e24\u90e8\u5206\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u793a\u4f8b\uff1a\u4f7f\u7528 Python \u7684 scikit-learn \u5e93\u8fdb\u884c\u968f\u673a\u5212\u5206\nfrom sklearn.model_selection import train_test_split\n\n# \u5047\u8bbe X \u662f\u7279\u5f81\u6570\u636e\uff0cy \u662f\u6807\u7b7e\u6570\u636e\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\nprint(f\"\u8bad\u7ec3\u96c6\u6837\u672c\u6570\uff1a{len(X_train)}\")\nprint(f\"\u6d4b\u8bd5\u96c6\u6837\u672c\u6570\uff1a{len(X_test)}\")<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u8bad\u7ec3\u96c6\u6837\u672c\u6570\uff1a455\n\u6d4b\u8bd5\u96c6\u6837\u672c\u6570\uff1a114<\/pre>\n\n\n\n<p>\u4ee3\u7801\u89e3\u6790\uff1a<\/p>\n\n\n\n<ul>\n<li><code>train_test_split<\/code>\uff1a\u8fd9\u662f scikit-learn \u4e2d\u7528\u4e8e\u5212\u5206\u6570\u636e\u7684\u6838\u5fc3\u51fd\u6570\u3002<\/li>\n\n\n\n<li><code>X, y<\/code>\uff1a\u8f93\u5165\u7684\u7279\u5f81\u6570\u636e\u548c\u5bf9\u5e94\u7684\u6807\u7b7e\u3002<\/li>\n\n\n\n<li><code>test_size=0.2<\/code>\uff1a\u6307\u5b9a\u6d4b\u8bd5\u96c6\u7684\u5927\u5c0f\u6bd4\u4f8b\u4e3a 20%\uff08\u5373\u8bad\u7ec3\u96c6\u5360 80%\uff09\u3002\u4f60\u4e5f\u53ef\u4ee5\u7528\u00a0<code>train_size=0.8<\/code>\u00a0\u6765\u6307\u5b9a\u3002<\/li>\n\n\n\n<li><code>random_state=42<\/code>\uff1a\u8bbe\u7f6e\u4e00\u4e2a\u968f\u673a\u79cd\u5b50\u3002\u8fd9\u80fd\u786e\u4fdd\u6bcf\u6b21\u8fd0\u884c\u4ee3\u7801\u65f6\uff0c\u5212\u5206\u7684\u7ed3\u679c\u90fd\u662f\u5b8c\u5168\u76f8\u540c\u7684\uff0c\u8fd9\u5bf9\u4e8e\u5b9e\u9a8c\u7684\u53ef\u590d\u73b0\u6027\u81f3\u5173\u91cd\u8981\u3002\u4f60\u53ef\u4ee5\u5c06\u5176\u8bbe\u7f6e\u4e3a\u4efb\u610f\u6574\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd392. \u5206\u5c42\u62bd\u6837\u5212\u5206<\/strong><\/p>\n\n\n\n<p>\u5728\u5206\u7c7b\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u6570\u636e\u96c6\u7684\u7c7b\u522b\u5206\u5e03\u4e0d\u5747\u8861\uff08\u4f8b\u5982\uff0c90%\u662fA\u7c7b\uff0c10%\u662fB\u7c7b\uff09\uff0c\u7b80\u5355\u7684\u968f\u673a\u5212\u5206\u53ef\u80fd\u5bfc\u81f4\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u4e2d\u5404\u7c7b\u522b\u7684\u6bd4\u4f8b\u5dee\u5f02\u5f88\u5927\uff0c\u5f71\u54cd\u8bc4\u4f30\u7684\u516c\u5e73\u6027\u3002<\/p>\n\n\n\n<p><strong>\u5206\u5c42\u62bd\u6837<\/strong>\u53ef\u4ee5\u786e\u4fdd\u5212\u5206\u540e\u7684\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u4e2d\uff0c\u5404\u4e2a\u7c7b\u522b\u7684\u6bd4\u4f8b\u4e0e\u539f\u59cb\u6570\u636e\u96c6\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u793a\u4f8b\uff1a\u5728\u5206\u7c7b\u95ee\u9898\u4e2d\u4f7f\u7528\u5206\u5c42\u62bd\u6837\nfrom sklearn.model_selection import train_test_split\n\n# \u5047\u8bbe y \u662f\u5206\u7c7b\u6807\u7b7e\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)\n\n# \u68c0\u67e5\u5212\u5206\u540e\u7684\u7c7b\u522b\u6bd4\u4f8b\nfrom collections import Counter\nprint(\"\u539f\u59cb\u6570\u636e\u7c7b\u522b\u5206\u5e03\uff1a\", Counter(y))\nprint(\"\u8bad\u7ec3\u96c6\u7c7b\u522b\u5206\u5e03\uff1a\", Counter(y_train))\nprint(\"\u6d4b\u8bd5\u96c6\u7c7b\u522b\u5206\u5e03\uff1a\", Counter(y_test))<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u539f\u59cb\u6570\u636e\u7c7b\u522b\u5206\u5e03\uff1a Counter({np.int64(1): 357, np.int64(0): 212})\n\u8bad\u7ec3\u96c6\u7c7b\u522b\u5206\u5e03\uff1a Counter({np.int64(1): 285, np.int64(0): 170})\n\u6d4b\u8bd5\u96c6\u7c7b\u522b\u5206\u5e03\uff1a Counter({np.int64(1): 72, np.int64(0): 42})<\/pre>\n\n\n\n<p>\u4ee3\u7801\u89e3\u6790\uff1a<\/p>\n\n\n\n<ul>\n<li><code>stratify=y<\/code>\uff1a\u8fd9\u662f\u5173\u952e\u53c2\u6570\u3002\u5b83\u544a\u8bc9\u51fd\u6570\u6309\u7167\u6807\u7b7e\u00a0<code>y<\/code>\u00a0\u7684\u7c7b\u522b\u5206\u5e03\u6765\u8fdb\u884c\u5206\u5c42\u62bd\u6837\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd393. \u65f6\u95f4\u5e8f\u5217\u6570\u636e\u5212\u5206<\/strong><\/p>\n\n\n\n<p>\u5bf9\u4e8e\u65f6\u95f4\u5e8f\u5217\u6570\u636e\uff08\u5982\u80a1\u7968\u4ef7\u683c\u3001\u6bcf\u65e5\u6c14\u6e29\uff09\uff0c\u6570\u636e\u70b9\u4e4b\u95f4\u5b58\u5728\u65f6\u95f4\u4e0a\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u6211\u4eec\u4e0d\u80fd\u968f\u673a\u6253\u4e71\uff0c\u56e0\u4e3a\u672a\u6765\u7684\u6570\u636e\u4e0d\u80fd\u7528\u6765\u9884\u6d4b\u8fc7\u53bb\u3002<\/p>\n\n\n\n<p>\u901a\u5e38\u7684\u505a\u6cd5\u662f\u6309\u65f6\u95f4\u987a\u5e8f\u5212\u5206\uff1a<strong>\u7528\u524d 80% \u65f6\u95f4\u7684\u6570\u636e\u4f5c\u4e3a\u8bad\u7ec3\u96c6\uff0c\u540e 20% \u7684\u6570\u636e\u4f5c\u4e3a\u6d4b\u8bd5\u96c6<\/strong>\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u793a\u4f8b\uff1a\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\u987a\u5e8f\u5212\u5206\nsplit_index = int(len(X) * 0.8) # \u8ba1\u7b9780%\u4f4d\u7f6e\u7684\u7d22\u5f15\n\nX_train, X_test = X[:split_index], X[split_index:]\ny_train, y_test = y[:split_index], y[split_index:]\n\nprint(f\"\u8bad\u7ec3\u96c6\u65f6\u95f4\u8303\u56f4\uff1a\u524d {split_index} \u4e2a\u6837\u672c\")\nprint(f\"\u6d4b\u8bd5\u96c6\u65f6\u95f4\u8303\u56f4\uff1a\u540e {len(X) - split_index} \u4e2a\u6837\u672c\")<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u8bad\u7ec3\u96c6\u65f6\u95f4\u8303\u56f4\uff1a\u524d 455 \u4e2a\u6837\u672c\n\u6d4b\u8bd5\u96c6\u65f6\u95f4\u8303\u56f4\uff1a\u540e 114 \u4e2a\u6837\u672c<\/pre>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u5212\u5206\u6bd4\u4f8b\u5982\u4f55\u9009\u62e9\uff1f\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\uff0c\u4f46\u6ca1\u6709\u56fa\u5b9a\u7b54\u6848\u3002\u5e38\u89c1\u7684\u6bd4\u4f8b\u6709\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>\u6bd4\u4f8b (\u8bad\u7ec3\u96c6:\u6d4b\u8bd5\u96c6)<\/th><th>\u9002\u7528\u573a\u666f<\/th><th>\u4f18\u70b9<\/th><th>\u7f3a\u70b9<\/th><\/tr><\/thead><tbody><tr><td><strong>70:30<\/strong><\/td><td>\u4e2d\u5c0f\u578b\u6570\u636e\u96c6\uff08\u6570\u5343\u5230\u6570\u4e07\u6837\u672c\uff09\u7684\u7ecf\u5178\u9009\u62e9<\/td><td>\u5e73\u8861\u4e86\u8bad\u7ec3\u6570\u636e\u91cf\u548c\u8bc4\u4f30\u53ef\u9760\u6027<\/td><td>\u5bf9\u4e8e\u6781\u5c0f\u6570\u636e\u96c6\uff0c30%\u7684\u6d4b\u8bd5\u96c6\u53ef\u80fd\u6837\u672c\u592a\u5c11\uff0c\u8bc4\u4f30\u4e0d\u7a33\u5b9a<\/td><\/tr><tr><td><strong>80:20<\/strong><\/td><td>\u76ee\u524d\u66f4\u6d41\u884c\u7684\u9ed8\u8ba4\u9009\u62e9\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u6df1\u5ea6\u5b66\u4e60<\/td><td>\u4e3a\u6a21\u578b\u63d0\u4f9b\u4e86\u66f4\u591a\u6570\u636e\u7528\u4e8e\u5b66\u4e60<\/td><td>\u6d4b\u8bd5\u96c6\u76f8\u5bf9\u8f83\u5c0f\uff0c\u8bc4\u4f30\u7684\u65b9\u5dee\u53ef\u80fd\u7565\u5927<\/td><\/tr><tr><td><strong>90:10 \u6216 95:5<\/strong><\/td><td>\u6570\u636e\u91cf\u975e\u5e38\u6709\u9650\u65f6<\/td><td>\u6700\u5927\u5316\u5229\u7528\u6709\u9650\u6570\u636e\u8fdb\u884c\u8bad\u7ec3<\/td><td>\u6d4b\u8bd5\u96c6\u592a\u5c0f\uff0c\u8bc4\u4f30\u7ed3\u679c\u53ef\u80fd\u4e0d\u53ef\u9760\uff0c\u7f6e\u4fe1\u5ea6\u4f4e<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p>\u6838\u5fc3\u539f\u5219\uff1a<\/p>\n\n\n\n<ol>\n<li><strong>\u786e\u4fdd\u8bad\u7ec3\u96c6\u8db3\u591f\u5927<\/strong>\uff1a\u6a21\u578b\u9700\u8981\u8db3\u591f\u7684\u6570\u636e\u6765\u5b66\u4e60\u6709\u6548\u7684\u6a21\u5f0f\u3002<\/li>\n\n\n\n<li><strong>\u786e\u4fdd\u6d4b\u8bd5\u96c6\u8db3\u591f\u5927<\/strong>\uff1a\u6d4b\u8bd5\u96c6\u9700\u8981\u63d0\u4f9b\u7edf\u8ba1\u4e0a\u53ef\u9760\u7684\u6027\u80fd\u8bc4\u4f30\u3002\u901a\u5e38\uff0c\u6d4b\u8bd5\u96c6\u81f3\u5c11\u5e94\u6709\u51e0\u767e\u4e2a\u6837\u672c\uff0c\u8bc4\u4f30\u7ed3\u679c\u624d\u6bd4\u8f83\u7a33\u5b9a\u3002<\/li>\n\n\n\n<li><strong>\u6570\u636e\u91cf\u8d8a\u5927<\/strong>\uff0c\u5206\u914d\u7ed9\u6d4b\u8bd5\u96c6\u7684\u6bd4\u4f8b\u53ef\u4ee5\u76f8\u5bf9<strong>\u8d8a\u5c0f<\/strong>\uff0c\u56e0\u4e3a\u5373\u4f7f\u5f88\u5c0f\u7684\u6bd4\u4f8b\u4e5f\u53ef\u80fd\u4ee3\u8868\u5927\u91cf\u7684\u6837\u672c\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u8fdb\u9636\u6982\u5ff5\uff1a\u9a8c\u8bc1\u96c6\u4e0e\u4ea4\u53c9\u9a8c\u8bc1\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u6211\u4eec\u4e0d\u4ec5\u9700\u8981\u8bc4\u4f30\u6700\u7ec8\u6a21\u578b\uff0c\u8fd8\u9700\u8981\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u8c03\u6574\u6a21\u578b\u7684<strong>\u8d85\u53c2\u6570<\/strong>\uff08\u5982\u5b66\u4e60\u7387\u3001\u6811\u7684\u6df1\u5ea6\u7b49\uff09\u3002\u5982\u679c\u76f4\u63a5\u7528\u6d4b\u8bd5\u96c6\u6765\u8c03\u6574\u53c2\u6570\uff0c\u90a3\u4e48\u6d4b\u8bd5\u96c6\u5c31\u53c8\u88ab&#8221;\u6c61\u67d3&#8221;\u4e86\uff0c\u5931\u53bb\u4e86\u4f5c\u4e3a&#8221;\u6700\u7ec8\u8003\u5b98&#8221;\u7684\u516c\u6b63\u6027\u3002<\/p>\n\n\n\n<p>\u4e3a\u6b64\uff0c\u6211\u4eec\u5f15\u5165\u4e86<strong>\u9a8c\u8bc1\u96c6<\/strong>\u3002<\/p>\n\n\n\n<p>\u4e09\u6570\u636e\u96c6\u5212\u5206\uff1a\u8bad\u7ec3\u96c6\u3001\u9a8c\u8bc1\u96c6\u3001\u6d4b\u8bd5\u96c6<\/p>\n\n\n\n<ol>\n<li><strong>\u8bad\u7ec3\u96c6<\/strong>\uff1a\u7528\u4e8e\u6a21\u578b\u53c2\u6570\u7684\u5b66\u4e60\u3002<\/li>\n\n\n\n<li><strong>\u9a8c\u8bc1\u96c6<\/strong>\uff1a\u7528\u4e8e\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u8c03\u6574\u8d85\u53c2\u6570\u3001\u9009\u62e9\u6a21\u578b\u6216\u8fdb\u884c\u65e9\u505c\u3002\u5b83\u76f8\u5f53\u4e8e&#8221;\u6a21\u62df\u8003&#8221;\u3002<\/li>\n\n\n\n<li><strong>\u6d4b\u8bd5\u96c6<\/strong>\uff1a\u5728\u6a21\u578b\u548c\u8d85\u53c2\u6570\u90fd\u786e\u5b9a\u540e\uff0c\u7528\u4e8e\u6700\u7ec8\u3001\u4e00\u6b21\u6027\u7684\u6027\u80fd\u8bc4\u4f30\u3002\u5b83\u662f&#8221;\u6700\u7ec8\u5927\u8003&#8221;\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u793a\u4f8b\uff1a\u5148\u5212\u5206\u51fa\u8bad\u7ec3+\u9a8c\u8bc1\u96c6 \u4e0e \u6d4b\u8bd5\u96c6\uff0c\u518d\u4ece\u8bad\u7ec3+\u9a8c\u8bc1\u96c6\u4e2d\u5212\u5206\u51fa\u9a8c\u8bc1\u96c6\nX_temp, X_test, y_temp, y_test = train_test_split(X, y, test_size=0.15, random_state=42) # \u5148\u5206\u51fa15%\u4f5c\u4e3a\u6700\u7ec8\u6d4b\u8bd5\u96c6\nX_train, X_val, y_train, y_val = train_test_split(X_temp, y_temp, test_size=0.176, random_state=42) # \u4ece\u5269\u4e0b\u768485%\u4e2d\u5206\u51fa\u7ea615%\u4f5c\u4e3a\u9a8c\u8bc1\u96c6\n\n# \u8ba1\u7b97\u6bd4\u4f8b\uff1a 0.85 * 0.176 \u2248 0.15\uff0c \u6700\u7ec8\u6bd4\u4f8b\u7ea6\u4e3a 70:15:15\nprint(f\"\u8bad\u7ec3\u96c6\uff1a{len(X_train)}\uff0c \u9a8c\u8bc1\u96c6\uff1a{len(X_val)}\uff0c \u6d4b\u8bd5\u96c6\uff1a{len(X_test)}\")<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u8bad\u7ec3\u96c6\uff1a397\uff0c \u9a8c\u8bc1\u96c6\uff1a86\uff0c \u6d4b\u8bd5\u96c6\uff1a86\n<\/pre>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\ud83d\udd39K\u6298\u4ea4\u53c9\u9a8c\u8bc1<\/strong><\/p>\n\n\n\n<p>\u5f53\u6570\u636e\u91cf\u4e0d\u5927\u65f6\uff0c\u5355\u72ec\u5212\u5206\u9a8c\u8bc1\u96c6\u4f1a\u8fdb\u4e00\u6b65\u51cf\u5c11\u8bad\u7ec3\u6570\u636e\u3002<strong>K\u6298\u4ea4\u53c9\u9a8c\u8bc1<\/strong>\u662f\u66f4\u5f3a\u5927\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n\n\n\n<p>\u5176\u6d41\u7a0b\u5982\u4e0b\uff0c\u53ef\u4ee5\u6709\u6548\u5229\u7528\u6709\u9650\u7684\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">flowchart TD\n    A[\u539f\u59cb\u6570\u636e\u96c6] --> B[\u968f\u673a\u6253\u4e71\u5e76\u5747\u5300\u5206\u4e3aK\u4efd]\n    B --> C{\u8fdb\u884cK\u8f6e\u5faa\u73af}\n    C --> D[\u7b2ci\u8f6e: \u5c06\u7b2ci\u4efd\u4f5c\u4e3a\u9a8c\u8bc1\u96c6]\n    D --> E[\u5176\u4f59K-1\u4efd\u5408\u5e76\u4f5c\u4e3a\u8bad\u7ec3\u96c6]\n    E --> F[\u5728\u8be5\u8f6e\u8bad\u7ec3\u96c6\u4e0a\u8bad\u7ec3\u6a21\u578b]\n    F --> G[\u5728\u8be5\u8f6e\u9a8c\u8bc1\u96c6\u4e0a\u8bc4\u4f30\u5f97\u5206Si]\n    G --> C\n    C -- K\u8f6e\u5b8c\u6210\u540e --> H[\u8ba1\u7b97K\u4e2a\u5f97\u5206\u7684\u5e73\u5747\u503c\u4f5c\u4e3a\u6700\u7ec8\u8bc4\u4f30]<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># \u793a\u4f8b\uff1a\u4f7f\u75285\u6298\u4ea4\u53c9\u9a8c\u8bc1\u8bc4\u4f30\u6a21\u578b\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.linear_model import LogisticRegression\n\nmodel = LogisticRegression()\nscores = cross_val_score(model, X, y, cv=5) # cv=5 \u8868\u793a5\u6298\u4ea4\u53c9\u9a8c\u8bc1\n\nprint(f\"\u5404\u6298\u5f97\u5206\uff1a{scores}\")\nprint(f\"\u5e73\u5747\u5f97\u5206\uff1a{scores.mean():.4f} (+\/- {scores.std()*2:.4f})\") # \u8f93\u51fa\u5e73\u5747\u5206\u548c\u6807\u51c6\u5dee<\/pre>\n\n\n\n<p><strong>\u4ea4\u53c9\u9a8c\u8bc1\u7684\u4f18\u70b9<\/strong>\uff1a<\/p>\n\n\n\n<ul>\n<li>\u5145\u5206\u5229\u7528\u6240\u6709\u6570\u636e\u8fdb\u884c\u8bad\u7ec3\u548c\u9a8c\u8bc1\u3002<\/li>\n\n\n\n<li>\u8bc4\u4f30\u7ed3\u679c\u66f4\u52a0\u7a33\u5b9a\u53ef\u9760\uff08\u56e0\u4e3a\u662f\u591a\u6b21\u8bc4\u4f30\u7684\u5e73\u5747\uff09\u3002<\/li>\n\n\n\n<li>\u662f\u4e2d\u5c0f\u6570\u636e\u96c6\u4e0a\u8fdb\u884c\u6a21\u578b\u9009\u62e9\u548c\u8c03\u53c2\u7684\u9ec4\u91d1\u6807\u51c6\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u5b9e\u8df5\u7ec3\u4e60\uff1a\u52a8\u624b\u4f53\u9a8c\u6570\u636e\u5212\u5206\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u7528\u4e00\u4e2a\u7b80\u5355\u7684\u6570\u636e\u96c6\u6765\u5b9e\u8df5\u4e00\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># 1. \u5bfc\u5165\u5fc5\u8981\u7684\u5e93\nimport numpy as np\nfrom sklearn.datasets import load_iris\nfrom sklearn.model_selection import train_test_split\n\n# 2. \u52a0\u8f7d\u9e22\u5c3e\u82b1\u6570\u636e\u96c6\niris = load_iris()\nX, y = iris.data, iris.target\nprint(f\"\u6570\u636e\u96c6\u5f62\u72b6\uff1a\u7279\u5f81 {X.shape}, \u6807\u7b7e {y.shape}\")\n\n# 3. \u7b80\u5355\u968f\u673a\u5212\u5206 (80%\u8bad\u7ec3\uff0c 20%\u6d4b\u8bd5)\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\nprint(f\"\u968f\u673a\u5212\u5206 -> \u8bad\u7ec3\u96c6\uff1a{X_train.shape}\uff0c \u6d4b\u8bd5\u96c6\uff1a{X_test.shape}\")\n\n# 4. \u5206\u5c42\u968f\u673a\u5212\u5206\nX_train_s, X_test_s, y_train_s, y_test_s = train_test_split(X, y, test_size=0.2, stratify=y, random_state=42)\nprint(f\"\u5206\u5c42\u5212\u5206 -> \u8bad\u7ec3\u96c6\uff1a{X_train_s.shape}\uff0c \u6d4b\u8bd5\u96c6\uff1a{X_test_s.shape}\")\n\n# 5. \u68c0\u67e5\u5206\u5c42\u6548\u679c\nprint(\"\\n\u539f\u59cb\u6570\u636e\u7c7b\u522b\u5206\u5e03\uff1a\", np.bincount(y))\nprint(\"\u968f\u673a\u5212\u5206\u540e\u6d4b\u8bd5\u96c6\u5206\u5e03\uff1a\", np.bincount(y_test)) # \u53ef\u80fd\u4e0d\u5747\u8861\nprint(\"\u5206\u5c42\u5212\u5206\u540e\u6d4b\u8bd5\u96c6\u5206\u5e03\uff1a\", np.bincount(y_test_s)) # \u5e94\u4e0e\u539f\u59cb\u5206\u5e03\u6210\u6bd4\u4f8b<\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\u6570\u636e\u96c6\u5f62\u72b6\uff1a\u7279\u5f81 (150, 4), \u6807\u7b7e (150,)\n\u968f\u673a\u5212\u5206 -> \u8bad\u7ec3\u96c6\uff1a(120, 4)\uff0c \u6d4b\u8bd5\u96c6\uff1a(30, 4)\n\u5206\u5c42\u5212\u5206 -> \u8bad\u7ec3\u96c6\uff1a(120, 4)\uff0c \u6d4b\u8bd5\u96c6\uff1a(30, 4)\n\n\u539f\u59cb\u6570\u636e\u7c7b\u522b\u5206\u5e03\uff1a [50 50 50]\n\u968f\u673a\u5212\u5206\u540e\u6d4b\u8bd5\u96c6\u5206\u5e03\uff1a [10  9 11]\n\u5206\u5c42\u5212\u5206\u540e\u6d4b\u8bd5\u96c6\u5206\u5e03\uff1a [10 10 10]<\/pre>\n\n\n\n<p>\u4f60\u7684\u4efb\u52a1\uff1a<\/p>\n\n\n\n<ol>\n<li>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u89c2\u5bdf\u8f93\u51fa\u7ed3\u679c\u3002<\/li>\n\n\n\n<li>\u5c1d\u8bd5\u4fee\u6539\u00a0<code>test_size<\/code>\u00a0\u4e3a 0.3\uff0c\u89c2\u5bdf\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u5927\u5c0f\u7684\u53d8\u5316\u3002<\/li>\n\n\n\n<li>\u5c1d\u8bd5\u4fee\u6539\u00a0<code>random_state<\/code>\u00a0\u4e3a\u53e6\u4e00\u4e2a\u6570\u5b57\uff08\u5982 7\uff09\uff0c\u518d\u6b21\u8fd0\u884c\uff0c\u89c2\u5bdf\u5212\u5206\u7ed3\u679c\u662f\u5426\u53d8\u5316\u3002<\/li>\n\n\n\n<li>\uff08\u6311\u6218\uff09\u4e0d\u8bbe\u7f6e\u00a0<code>random_state<\/code>\u00a0\u53c2\u6570\uff0c\u591a\u6b21\u8fd0\u884c\u4ee3\u7801\uff0c\u89c2\u5bdf\u6bcf\u6b21\u7684\u5212\u5206\u7ed3\u679c\u662f\u5426\u76f8\u540c\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u3000\u3000<\/p>\n\n\n\n<p><strong>\u603b\u7ed3\u4e0e\u6838\u5fc3\u8981\u70b9\ud83d\udd16<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ul>\n<li>\u6838\u5fc3\u76ee\u7684\uff1a\u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u662f\u4e3a\u4e86\u8bc4\u4f30\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\uff0c\u9632\u6b62\u8fc7\u62df\u5408\uff0c\u786e\u4fdd\u6a21\u578b\u80fd\u5904\u7406\u65b0\u6570\u636e\u3002<\/li>\n\n\n\n<li>\u9ec4\u91d1\u6cd5\u5219\uff1a\u6d4b\u8bd5\u96c6\u5fc5\u987b\u5728\u6574\u4e2a\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u5b8c\u5168\u4fdd\u5bc6\uff0c\u4ec5\u7528\u4e8e\u6700\u7ec8\u8bc4\u4f30\u3002<\/li>\n\n\n\n<li>\u5212\u5206\u65b9\u6cd5\uff1a\n<ul>\n<li>\u968f\u673a\u5212\u5206\uff1a\u6700\u901a\u7528\u3002<\/li>\n\n\n\n<li>\u5206\u5c42\u5212\u5206\uff1a\u9002\u7528\u4e8e\u5206\u7c7b\u95ee\u9898\u4e2d\u7684\u4e0d\u5747\u8861\u6570\u636e\u3002<\/li>\n\n\n\n<li>\u987a\u5e8f\u5212\u5206\uff1a\u9002\u7528\u4e8e\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>\u5212\u5206\u6bd4\u4f8b\uff1a\u6ca1\u6709\u7edd\u5bf9\u6807\u51c6\uff0c\u9700\u5728&#8221;\u8db3\u591f\u8bad\u7ec3&#8221;\u548c&#8221;\u53ef\u9760\u8bc4\u4f30&#8221;\u95f4\u6743\u8861\u300280:20 \u6216 70:30 \u662f\u5e38\u89c1\u8d77\u70b9\u3002<\/li>\n\n\n\n<li>\u8fdb\u9636\u5de5\u5177\uff1a\n<ul>\n<li>\u9a8c\u8bc1\u96c6\uff1a\u7528\u4e8e\u6a21\u578b\u8c03\u53c2\uff0c\u4fdd\u62a4\u6d4b\u8bd5\u96c6\u7684\u7eaf\u6d01\u6027\u3002<\/li>\n\n\n\n<li>K\u6298\u4ea4\u53c9\u9a8c\u8bc1\uff1a\u4e2d\u5c0f\u6570\u636e\u96c6\u7684\u8bc4\u4f30\u548c\u8c03\u53c2\u5229\u5668\uff0c\u7ed3\u679c\u66f4\u7a33\u5065\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00\u200b\ud83d\udd16 \u5728\u673a\u5668\u5b66\u4e60\u7684\u4e16\u754c\u91cc\uff0c\u6570\u636e\u662f\u9a71\u52a8\u4e00\u5207\u6a21\u578b\u7684\u71c3\u6599\uff0c\u7136\u800c\uff0c\u5982\u4f55\u6b63\u786e\u5730\u4f7f\u7528\u8fd9\u4e9b\u71c3\u6599\uff0c\u51b3\u5b9a\u4e86\u4f60\u7684\u6a21\u578b\u662f\u80fd\u7cbe\u51c6\u9884 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/20089"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20089"}],"version-history":[{"count":3,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/20089\/revisions"}],"predecessor-version":[{"id":20092,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/20089\/revisions\/20092"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}