{"id":1159171,"date":"2025-01-13T18:49:41","date_gmt":"2025-01-13T10:49:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159171.html"},"modified":"2025-01-13T18:49:44","modified_gmt":"2025-01-13T10:49:44","slug":"python%e5%a6%82%e4%bd%95%e5%81%9a%e5%9b%9e%e5%bd%92%e5%bb%ba%e6%a8%a1","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1159171.html","title":{"rendered":"python\u5982\u4f55\u505a\u56de\u5f52\u5efa\u6a21"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25200940\/269e481b-9383-43bc-af51-3fcab429eabf.webp\" alt=\"python\u5982\u4f55\u505a\u56de\u5f52\u5efa\u6a21\" \/><\/p>\n<p><p> <strong>\u5728 Python \u4e2d\u8fdb\u884c\u56de\u5f52\u5efa\u6a21\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u6570\u636e\u51c6\u5907\u3001\u7279\u5f81\u9009\u62e9\u3001\u6a21\u578b\u9009\u62e9\u3001\u6a21\u578b\u8bad\u7ec3\u3001\u6a21\u578b\u8bc4\u4f30\u3001\u6a21\u578b\u8c03\u4f18\u3001\u5e76\u4e14\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u6a21\u578b\u90e8\u7f72\u3002<\/strong>\u5176\u4e2d\uff0c<strong>\u6a21\u578b\u9009\u62e9<\/strong>\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u6b65\u9aa4\uff0c\u9009\u62e9\u5408\u9002\u7684\u56de\u5f52\u6a21\u578b\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u9884\u6d4b\u6027\u80fd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u5c55\u5f00\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><p>\u6a21\u578b\u9009\u62e9\u5305\u62ec\u9009\u62e9\u7ebf\u6027\u56de\u5f52\u3001\u5cad\u56de\u5f52\u3001Lasso\u56de\u5f52\u3001\u5f39\u6027\u7f51\u56de\u5f52\u3001\u51b3\u7b56\u6811\u56de\u5f52\u3001\u968f\u673a\u68ee\u6797\u56de\u5f52\u3001\u652f\u6301\u5411\u91cf\u56de\u5f52\u3001K\u8fd1\u90bb\u56de\u5f52\u3001\u68af\u5ea6\u63d0\u5347\u56de\u5f52\u7b49\u591a\u79cd\u6a21\u578b\u3002\u9009\u62e9\u6a21\u578b\u65f6\u9700\u8981\u8003\u8651\u6570\u636e\u7684\u7279\u6027\u3001\u6a21\u578b\u7684\u590d\u6742\u5ea6\u3001\u8ba1\u7b97\u6210\u672c\u7b49\u56e0\u7d20\u3002\u6bd4\u5982\uff0c\u7ebf\u6027\u56de\u5f52\u9002\u7528\u4e8e\u7ebf\u6027\u5173\u7cfb\u7684\u6570\u636e\uff0c\u800c\u968f\u673a\u68ee\u6797\u56de\u5f52\u5219\u9002\u7528\u4e8e\u975e\u7ebf\u6027\u5173\u7cfb\u7684\u6570\u636e\uff0c\u5e76\u4e14\u80fd\u591f\u5904\u7406\u8f83\u9ad8\u7ef4\u5ea6\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u6570\u636e\u51c6\u5907<\/h2>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u56de\u5f52\u5efa\u6a21\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u51c6\u5907\u6570\u636e\u3002\u6570\u636e\u51c6\u5907\u5305\u62ec\u6570\u636e\u6536\u96c6\u3001\u6570\u636e\u6e05\u6d17\u548c\u6570\u636e\u9884\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u6536\u96c6<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u6536\u96c6\u662f\u56de\u5f52\u5efa\u6a21\u7684\u7b2c\u4e00\u6b65\uff0c\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u591a\u4e2a\u6765\u6e90\uff0c\u5982\u6570\u636e\u5e93\u3001API\u3001\u6587\u4ef6\uff08\u5982CSV\u3001Excel\uff09\u3001\u7f51\u7edc\u722c\u866b\u7b49\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u5de5\u5177\u6765\u8fdb\u884c\u6570\u636e\u6536\u96c6\uff0c\u4f8b\u5982<code>pandas<\/code>\u3001<code>requests<\/code>\u3001<code>BeautifulSoup<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u6e05\u6d17<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u6e05\u6d17\u662f\u5c06\u539f\u59cb\u6570\u636e\u8f6c\u6362\u4e3a\u9002\u5408\u5efa\u6a21\u7684\u6570\u636e\u7684\u8fc7\u7a0b\u3002\u6570\u636e\u6e05\u6d17\u5305\u62ec\u5904\u7406\u7f3a\u5931\u503c\u3001\u53bb\u9664\u91cd\u590d\u6570\u636e\u3001\u5904\u7406\u5f02\u5e38\u503c\u3001\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u7b49\u3002Python\u7684<code>pandas<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u6765\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3002<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u9884\u5904\u7406\u662f\u5c06\u6e05\u6d17\u540e\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u9002\u5408\u6a21\u578b\u8bad\u7ec3\u7684\u6570\u636e\u7684\u8fc7\u7a0b\u3002\u6570\u636e\u9884\u5904\u7406\u5305\u62ec\u7279\u5f81\u7f29\u653e\u3001\u7279\u5f81\u7f16\u7801\u3001\u7279\u5f81\u9009\u62e9\u3001\u6570\u636e\u5206\u5272\u7b49\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u6570\u636e\u9884\u5904\u7406\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u7279\u5f81\u9009\u62e9<\/h2>\n<\/p>\n<p><p>\u7279\u5f81\u9009\u62e9\u662f\u4ece\u539f\u59cb\u7279\u5f81\u4e2d\u9009\u62e9\u51fa\u5bf9\u6a21\u578b\u6027\u80fd\u6709\u663e\u8457\u5f71\u54cd\u7684\u7279\u5f81\u7684\u8fc7\u7a0b\u3002\u7279\u5f81\u9009\u62e9\u53ef\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\uff0c\u51cf\u5c11\u6a21\u578b\u7684\u590d\u6742\u5ea6\uff0c\u964d\u4f4e\u8ba1\u7b97\u6210\u672c\u3002<\/p>\n<\/p>\n<p><h3>\u8fc7\u6ee4\u6cd5<\/h3>\n<\/p>\n<p><p>\u8fc7\u6ee4\u6cd5\u6839\u636e\u7edf\u8ba1\u6307\u6807\u5bf9\u7279\u5f81\u8fdb\u884c\u8bc4\u5206\uff0c\u5e76\u9009\u62e9\u8bc4\u5206\u6700\u9ad8\u7684\u7279\u5f81\u3002\u5e38\u7528\u7684\u7edf\u8ba1\u6307\u6807\u6709\u65b9\u5dee\u3001\u76f8\u5173\u7cfb\u6570\u3001\u5361\u65b9\u68c0\u9a8c\u7b49\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u8fc7\u6ee4\u6cd5\u7684\u5b9e\u73b0\uff0c\u5982<code>SelectKBest<\/code>\u3001<code>VarianceThreshold<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u5305\u88c5\u6cd5<\/h3>\n<\/p>\n<p><p>\u5305\u88c5\u6cd5\u901a\u8fc7\u6a21\u578b\u8bad\u7ec3\u548c\u8bc4\u4f30\u6765\u9009\u62e9\u7279\u5f81\u3002\u5e38\u7528\u7684\u5305\u88c5\u6cd5\u6709\u9012\u5f52\u7279\u5f81\u6d88\u9664\uff08RFE\uff09\u3001\u524d\u5411\u9009\u62e9\u3001\u540e\u5411\u9009\u62e9\u7b49\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u9012\u5f52\u7279\u5f81\u6d88\u9664\u7684\u5b9e\u73b0\uff0c\u5982<code>RFE<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5d4c\u5165\u6cd5<\/h3>\n<\/p>\n<p><p>\u5d4c\u5165\u6cd5\u901a\u8fc7\u6a21\u578b\u8bad\u7ec3\u6765\u9009\u62e9\u7279\u5f81\uff0c\u5e38\u7528\u7684\u5d4c\u5165\u6cd5\u6709Lasso\u56de\u5f52\u3001\u51b3\u7b56\u6811\u3001\u968f\u673a\u68ee\u6797\u7b49\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u5d4c\u5165\u6cd5\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u6a21\u578b\u9009\u62e9<\/h2>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u56de\u5f52\u6a21\u578b\u662f\u56de\u5f52\u5efa\u6a21\u7684\u91cd\u8981\u6b65\u9aa4\uff0c\u4e0d\u540c\u7684\u6a21\u578b\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u6570\u636e\u548c\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u7ebf\u6027\u56de\u5f52<\/h3>\n<\/p>\n<p><p>\u7ebf\u6027\u56de\u5f52\u662f\u6700\u57fa\u672c\u7684\u56de\u5f52\u6a21\u578b\uff0c\u9002\u7528\u4e8e\u7279\u5f81\u548c\u76ee\u6807\u53d8\u91cf\u4e4b\u95f4\u5b58\u5728\u7ebf\u6027\u5173\u7cfb\u7684\u6570\u636e\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u7ebf\u6027\u56de\u5f52\u7684\u5b9e\u73b0\uff0c\u5982<code>LinearRegression<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5cad\u56de\u5f52<\/h3>\n<\/p>\n<p><p>\u5cad\u56de\u5f52\u5728\u7ebf\u6027\u56de\u5f52\u7684\u57fa\u7840\u4e0a\u589e\u52a0\u4e86L2\u6b63\u5219\u5316\u9879\uff0c\u9002\u7528\u4e8e\u7279\u5f81\u4e4b\u95f4\u5b58\u5728\u591a\u91cd\u5171\u7ebf\u6027\u7684\u6570\u636e\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u5cad\u56de\u5f52\u7684\u5b9e\u73b0\uff0c\u5982<code>Ridge<\/code>\u3002<\/p>\n<\/p>\n<p><h3>Lasso\u56de\u5f52<\/h3>\n<\/p>\n<p><p>Lasso\u56de\u5f52\u5728\u7ebf\u6027\u56de\u5f52\u7684\u57fa\u7840\u4e0a\u589e\u52a0\u4e86L1\u6b63\u5219\u5316\u9879\uff0c\u9002\u7528\u4e8e\u7279\u5f81\u4e4b\u95f4\u5b58\u5728\u591a\u91cd\u5171\u7ebf\u6027\uff0c\u5e76\u4e14\u5e0c\u671b\u8fdb\u884c\u7279\u5f81\u9009\u62e9\u7684\u6570\u636e\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86Lasso\u56de\u5f52\u7684\u5b9e\u73b0\uff0c\u5982<code>Lasso<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5f39\u6027\u7f51\u56de\u5f52<\/h3>\n<\/p>\n<p><p>\u5f39\u6027\u7f51\u56de\u5f52\u7ed3\u5408\u4e86\u5cad\u56de\u5f52\u548cLasso\u56de\u5f52\u7684\u4f18\u70b9\uff0c\u9002\u7528\u4e8e\u7279\u5f81\u4e4b\u95f4\u5b58\u5728\u591a\u91cd\u5171\u7ebf\u6027\uff0c\u5e76\u4e14\u5e0c\u671b\u8fdb\u884c\u7279\u5f81\u9009\u62e9\u7684\u6570\u636e\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u5f39\u6027\u7f51\u56de\u5f52\u7684\u5b9e\u73b0\uff0c\u5982<code>ElasticNet<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u51b3\u7b56\u6811\u56de\u5f52<\/h3>\n<\/p>\n<p><p>\u51b3\u7b56\u6811\u56de\u5f52\u901a\u8fc7\u6784\u5efa\u51b3\u7b56\u6811\u6765\u8fdb\u884c\u56de\u5f52\uff0c\u9002\u7528\u4e8e\u7279\u5f81\u548c\u76ee\u6807\u53d8\u91cf\u4e4b\u95f4\u5b58\u5728\u975e\u7ebf\u6027\u5173\u7cfb\u7684\u6570\u636e\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u51b3\u7b56\u6811\u56de\u5f52\u7684\u5b9e\u73b0\uff0c\u5982<code>DecisionTreeRegressor<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u968f\u673a\u68ee\u6797\u56de\u5f52<\/h3>\n<\/p>\n<p><p>\u968f\u673a\u68ee\u6797\u56de\u5f52\u901a\u8fc7\u6784\u5efa\u591a\u68f5\u51b3\u7b56\u6811\u5e76\u5bf9\u7ed3\u679c\u8fdb\u884c\u5e73\u5747\u6765\u8fdb\u884c\u56de\u5f52\uff0c\u9002\u7528\u4e8e\u7279\u5f81\u548c\u76ee\u6807\u53d8\u91cf\u4e4b\u95f4\u5b58\u5728\u975e\u7ebf\u6027\u5173\u7cfb\u7684\u6570\u636e\uff0c\u5e76\u4e14\u80fd\u591f\u5904\u7406\u8f83\u9ad8\u7ef4\u5ea6\u7684\u6570\u636e\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u968f\u673a\u68ee\u6797\u56de\u5f52\u7684\u5b9e\u73b0\uff0c\u5982<code>RandomForestRegressor<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u652f\u6301\u5411\u91cf\u56de\u5f52<\/h3>\n<\/p>\n<p><p>\u652f\u6301\u5411\u91cf\u56de\u5f52\u901a\u8fc7\u6784\u5efa\u652f\u6301\u5411\u91cf\u673a\u6765\u8fdb\u884c\u56de\u5f52\uff0c\u9002\u7528\u4e8e\u7279\u5f81\u548c\u76ee\u6807\u53d8\u91cf\u4e4b\u95f4\u5b58\u5728\u975e\u7ebf\u6027\u5173\u7cfb\u7684\u6570\u636e\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u652f\u6301\u5411\u91cf\u56de\u5f52\u7684\u5b9e\u73b0\uff0c\u5982<code>SVR<\/code>\u3002<\/p>\n<\/p>\n<p><h3>K\u8fd1\u90bb\u56de\u5f52<\/h3>\n<\/p>\n<p><p>K\u8fd1\u90bb\u56de\u5f52\u901a\u8fc7\u8ba1\u7b97\u76ee\u6807\u6837\u672c\u4e0e\u8bad\u7ec3\u6837\u672c\u4e4b\u95f4\u7684\u8ddd\u79bb\uff0c\u5e76\u5bf9\u6700\u8fd1\u7684K\u4e2a\u6837\u672c\u7684\u76ee\u6807\u503c\u8fdb\u884c\u5e73\u5747\u6765\u8fdb\u884c\u56de\u5f52\uff0c\u9002\u7528\u4e8e\u7279\u5f81\u548c\u76ee\u6807\u53d8\u91cf\u4e4b\u95f4\u5b58\u5728\u975e\u7ebf\u6027\u5173\u7cfb\u7684\u6570\u636e\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86K\u8fd1\u90bb\u56de\u5f52\u7684\u5b9e\u73b0\uff0c\u5982<code>KNeighborsRegressor<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u68af\u5ea6\u63d0\u5347\u56de\u5f52<\/h3>\n<\/p>\n<p><p>\u68af\u5ea6\u63d0\u5347\u56de\u5f52\u901a\u8fc7\u6784\u5efa\u591a\u4e2a\u5f31\u56de\u5f52\u6a21\u578b\uff0c\u5e76\u5bf9\u8fd9\u4e9b\u6a21\u578b\u8fdb\u884c\u52a0\u6743\u5e73\u5747\u6765\u8fdb\u884c\u56de\u5f52\uff0c\u9002\u7528\u4e8e\u7279\u5f81\u548c\u76ee\u6807\u53d8\u91cf\u4e4b\u95f4\u5b58\u5728\u975e\u7ebf\u6027\u5173\u7cfb\u7684\u6570\u636e\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u68af\u5ea6\u63d0\u5347\u56de\u5f52\u7684\u5b9e\u73b0\uff0c\u5982<code>GradientBoostingRegressor<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u6a21\u578b\u8bad\u7ec3<\/h2>\n<\/p>\n<p><p>\u5728\u9009\u62e9\u5408\u9002\u7684\u56de\u5f52\u6a21\u578b\u4e4b\u540e\uff0c\u9700\u8981\u5bf9\u6a21\u578b\u8fdb\u884c\u8bad\u7ec3\u3002\u6a21\u578b\u8bad\u7ec3\u662f\u5c06\u6570\u636e\u8f93\u5165\u6a21\u578b\uff0c\u5e76\u8c03\u6574\u6a21\u578b\u53c2\u6570\u4f7f\u5176\u80fd\u591f\u6700\u5c0f\u5316\u9884\u6d4b\u8bef\u5dee\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u5212\u5206<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3\u4e4b\u524d\uff0c\u9700\u8981\u5c06\u6570\u636e\u96c6\u5212\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002\u8bad\u7ec3\u96c6\u7528\u4e8e\u8bad\u7ec3\u6a21\u578b\uff0c\u6d4b\u8bd5\u96c6\u7528\u4e8e\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u6570\u636e\u96c6\u5212\u5206\u7684\u51fd\u6570\uff0c\u5982<code>tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_test_split<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u6a21\u578b\u8bad\u7ec3<\/h3>\n<\/p>\n<p><p>\u6a21\u578b\u8bad\u7ec3\u662f\u5c06\u8bad\u7ec3\u96c6\u8f93\u5165\u6a21\u578b\uff0c\u5e76\u8c03\u6574\u6a21\u578b\u53c2\u6570\u4f7f\u5176\u80fd\u591f\u6700\u5c0f\u5316\u9884\u6d4b\u8bef\u5dee\u7684\u8fc7\u7a0b\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u6a21\u578b\u8bad\u7ec3\u7684\u63a5\u53e3\uff0c\u5982<code>fit<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u6a21\u578b\u8bc4\u4f30<\/h2>\n<\/p>\n<p><p>\u5728\u6a21\u578b\u8bad\u7ec3\u4e4b\u540e\uff0c\u9700\u8981\u5bf9\u6a21\u578b\u8fdb\u884c\u8bc4\u4f30\u3002\u6a21\u578b\u8bc4\u4f30\u662f\u901a\u8fc7\u8ba1\u7b97\u9884\u6d4b\u8bef\u5dee\u6765\u8861\u91cf\u6a21\u578b\u6027\u80fd\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u8bc4\u4f30\u6307\u6807<\/h3>\n<\/p>\n<p><p>\u5e38\u7528\u7684\u56de\u5f52\u6a21\u578b\u8bc4\u4f30\u6307\u6807\u6709\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u3001\u5747\u65b9\u6839\u8bef\u5dee\uff08RMSE\uff09\u3001\u5e73\u5747\u7edd\u5bf9\u8bef\u5dee\uff08MAE\uff09\u3001\u51b3\u5b9a\u7cfb\u6570\uff08R\u00b2\uff09\u7b49\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u8bc4\u4f30\u6307\u6807\u7684\u5b9e\u73b0\uff0c\u5982<code>mean_squared_error<\/code>\u3001<code>mean_absolute_error<\/code>\u3001<code>r2_score<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4ea4\u53c9\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u4ea4\u53c9\u9a8c\u8bc1\u662f\u4e00\u79cd\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5c06\u6570\u636e\u96c6\u5212\u5206\u4e3a\u591a\u4e2a\u5b50\u96c6\uff0c\u5e76\u5728\u591a\u4e2a\u5b50\u96c6\u4e0a\u8fdb\u884c\u8bad\u7ec3\u548c\u6d4b\u8bd5\uff0c\u6765\u8861\u91cf\u6a21\u578b\u7684\u7a33\u5b9a\u6027\u548c\u6cdb\u5316\u80fd\u529b\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u4ea4\u53c9\u9a8c\u8bc1\u7684\u65b9\u6cd5\uff0c\u5982<code>cross_val_score<\/code>\u3001<code>KFold<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u6a21\u578b\u8c03\u4f18<\/h2>\n<\/p>\n<p><p>\u5728\u6a21\u578b\u8bc4\u4f30\u4e4b\u540e\uff0c\u9700\u8981\u5bf9\u6a21\u578b\u8fdb\u884c\u8c03\u4f18\u3002\u6a21\u578b\u8c03\u4f18\u662f\u901a\u8fc7\u8c03\u6574\u6a21\u578b\u8d85\u53c2\u6570\u6765\u63d0\u9ad8\u6a21\u578b\u6027\u80fd\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u7f51\u683c\u641c\u7d22<\/h3>\n<\/p>\n<p><p>\u7f51\u683c\u641c\u7d22\u662f\u4e00\u79cd\u6a21\u578b\u8c03\u4f18\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u7a77\u4e3e\u6240\u6709\u53ef\u80fd\u7684\u8d85\u53c2\u6570\u7ec4\u5408\uff0c\u6765\u5bfb\u627e\u6700\u4f18\u7684\u8d85\u53c2\u6570\u7ec4\u5408\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u7f51\u683c\u641c\u7d22\u7684\u5b9e\u73b0\uff0c\u5982<code>GridSearchCV<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u968f\u673a\u641c\u7d22<\/h3>\n<\/p>\n<p><p>\u968f\u673a\u641c\u7d22\u662f\u4e00\u79cd\u6a21\u578b\u8c03\u4f18\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u968f\u673a\u9009\u62e9\u8d85\u53c2\u6570\u7ec4\u5408\uff0c\u6765\u5bfb\u627e\u6700\u4f18\u7684\u8d85\u53c2\u6570\u7ec4\u5408\u3002\u76f8\u6bd4\u4e8e\u7f51\u683c\u641c\u7d22\uff0c\u968f\u673a\u641c\u7d22\u5728\u9ad8\u7ef4\u5ea6\u8d85\u53c2\u6570\u7a7a\u95f4\u4e2d\u66f4\u9ad8\u6548\u3002Python\u7684<code>scikit-learn<\/code>\u5e93\u63d0\u4f9b\u4e86\u968f\u673a\u641c\u7d22\u7684\u5b9e\u73b0\uff0c\u5982<code>RandomizedSearchCV<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u8d1d\u53f6\u65af\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u8d1d\u53f6\u65af\u4f18\u5316\u662f\u4e00\u79cd\u6a21\u578b\u8c03\u4f18\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u6784\u5efa\u4ee3\u7406\u6a21\u578b\u6765\u6307\u5bfc\u8d85\u53c2\u6570\u7684\u9009\u62e9\uff0c\u6765\u5bfb\u627e\u6700\u4f18\u7684\u8d85\u53c2\u6570\u7ec4\u5408\u3002\u8d1d\u53f6\u65af\u4f18\u5316\u5728\u9ad8\u7ef4\u5ea6\u8d85\u53c2\u6570\u7a7a\u95f4\u4e2d\u8f83\u7f51\u683c\u641c\u7d22\u548c\u968f\u673a\u641c\u7d22\u66f4\u9ad8\u6548\u3002Python\u7684<code>skopt<\/code>\u5e93\u63d0\u4f9b\u4e86\u8d1d\u53f6\u65af\u4f18\u5316\u7684\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u6a21\u578b\u90e8\u7f72<\/h2>\n<\/p>\n<p><p>\u5728\u6a21\u578b\u8c03\u4f18\u4e4b\u540e\uff0c\u9700\u8981\u5c06\u6a21\u578b\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u4ee5\u4fbf\u8fdb\u884c\u5b9e\u65f6\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><h3>\u4fdd\u5b58\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u6a21\u578b\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u4e4b\u524d\uff0c\u9700\u8981\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002Python\u7684<code>joblib<\/code>\u5e93\u63d0\u4f9b\u4e86\u6a21\u578b\u4fdd\u5b58\u548c\u52a0\u8f7d\u7684\u51fd\u6570\uff0c\u5982<code>dump<\/code>\u3001<code>load<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u6a21\u578b\u670d\u52a1\u5316<\/h3>\n<\/p>\n<p><p>\u5c06\u6a21\u578b\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u53ef\u4ee5\u5c06\u6a21\u578b\u670d\u52a1\u5316\uff0c\u63d0\u4f9bAPI\u63a5\u53e3\u6765\u8fdb\u884c\u5b9e\u65f6\u9884\u6d4b\u3002Python\u7684<code>Flask<\/code>\u3001<code>FastAPI<\/code>\u7b49\u6846\u67b6\u53ef\u4ee5\u7528\u4e8e\u6784\u5efa\u6a21\u578b\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><h2>\u516b\u3001\u5b9e\u6218\u6848\u4f8b<\/h2>\n<\/p>\n<p><p>\u4e0b\u9762\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u6218\u6848\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u56de\u5f52\u5efa\u6a21\u3002<\/p>\n<\/p>\n<p><h3>\u6570\u636e\u51c6\u5907<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u5bfc\u5165\u6240\u9700\u7684\u5e93\u5e76\u52a0\u8f7d\u6570\u636e\u96c6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>from sklearn.model_selection import train_test_split<\/p>\n<p>from sklearn.preprocessing import StandardScaler<\/p>\n<h2><strong>\u52a0\u8f7d\u6570\u636e\u96c6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u67e5\u770b\u6570\u636e\u96c6<\/strong><\/h2>\n<p>print(data.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6570\u636e\u6e05\u6d17<\/h3>\n<\/p>\n<p><p>\u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u6d17\uff0c\u5305\u62ec\u5904\u7406\u7f3a\u5931\u503c\u3001\u53bb\u9664\u91cd\u590d\u6570\u636e\u3001\u5904\u7406\u5f02\u5e38\u503c\u3001\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5904\u7406\u7f3a\u5931\u503c<\/p>\n<p>data = data.dropna()<\/p>\n<h2><strong>\u53bb\u9664\u91cd\u590d\u6570\u636e<\/strong><\/h2>\n<p>data = data.drop_duplicates()<\/p>\n<h2><strong>\u8f6c\u6362\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p>data[&#39;column&#39;] = data[&#39;column&#39;].astype(float)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6570\u636e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u5305\u62ec\u7279\u5f81\u7f29\u653e\u3001\u7279\u5f81\u7f16\u7801\u3001\u7279\u5f81\u9009\u62e9\u3001\u6570\u636e\u5206\u5272\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7279\u5f81\u7f29\u653e<\/p>\n<p>scaler = StandardScaler()<\/p>\n<p>data_scaled = scaler.fit_transform(data)<\/p>\n<h2><strong>\u6570\u636e\u5206\u5272<\/strong><\/h2>\n<p>X = data_scaled[:, :-1]<\/p>\n<p>y = data_scaled[:, -1]<\/p>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6a21\u578b\u9009\u62e9\u548c\u8bad\u7ec3<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u56de\u5f52\u6a21\u578b\uff0c\u5e76\u8fdb\u884c\u8bad\u7ec3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import LinearRegression<\/p>\n<h2><strong>\u9009\u62e9\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(X_train, y_train)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6a21\u578b\u8bc4\u4f30<\/h3>\n<\/p>\n<p><p>\u5bf9\u6a21\u578b\u8fdb\u884c\u8bc4\u4f30\uff0c\u8ba1\u7b97\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u3001\u5747\u65b9\u6839\u8bef\u5dee\uff08RMSE\uff09\u3001\u51b3\u5b9a\u7cfb\u6570\uff08R\u00b2\uff09\u7b49\u8bc4\u4f30\u6307\u6807\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import mean_squared_error, r2_score<\/p>\n<h2><strong>\u9884\u6d4b\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>y_pred = model.predict(X_test)<\/p>\n<h2><strong>\u8ba1\u7b97\u8bc4\u4f30\u6307\u6807<\/strong><\/h2>\n<p>mse = mean_squared_error(y_test, y_pred)<\/p>\n<p>rmse = mse  0.5<\/p>\n<p>r2 = r2_score(y_test, y_pred)<\/p>\n<p>print(f&#39;MSE: {mse}&#39;)<\/p>\n<p>print(f&#39;RMSE: {rmse}&#39;)<\/p>\n<p>print(f&#39;R\u00b2: {r2}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6a21\u578b\u8c03\u4f18<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u7f51\u683c\u641c\u7d22\u5bf9\u6a21\u578b\u8fdb\u884c\u8c03\u4f18\uff0c\u5bfb\u627e\u6700\u4f18\u7684\u8d85\u53c2\u6570\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import GridSearchCV<\/p>\n<h2><strong>\u5b9a\u4e49\u8d85\u53c2\u6570\u7f51\u683c<\/strong><\/h2>\n<p>param_grid = {&#39;fit_intercept&#39;: [True, False], &#39;normalize&#39;: [True, False]}<\/p>\n<h2><strong>\u8fdb\u884c\u7f51\u683c\u641c\u7d22<\/strong><\/h2>\n<p>grid_search = GridSearchCV(model, param_grid, cv=5)<\/p>\n<p>grid_search.fit(X_train, y_train)<\/p>\n<h2><strong>\u8f93\u51fa\u6700\u4f18\u8d85\u53c2\u6570\u7ec4\u5408<\/strong><\/h2>\n<p>print(f&#39;Best parameters: {grid_search.best_params_}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6a21\u578b\u90e8\u7f72<\/h3>\n<\/p>\n<p><p>\u5c06\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\uff0c\u5e76\u901a\u8fc7Flask\u6784\u5efa\u6a21\u578b\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import joblib<\/p>\n<p>from flask import Flask, request, jsonify<\/p>\n<h2><strong>\u4fdd\u5b58\u6a21\u578b<\/strong><\/h2>\n<p>joblib.dump(grid_search.best_estimator_, &#39;model.pkl&#39;)<\/p>\n<h2><strong>\u52a0\u8f7d\u6a21\u578b<\/strong><\/h2>\n<p>model = joblib.load(&#39;model.pkl&#39;)<\/p>\n<h2><strong>\u6784\u5efa\u6a21\u578b\u670d\u52a1<\/strong><\/h2>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/predict&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def predict():<\/p>\n<p>    data = request.get_json(force=True)<\/p>\n<p>    prediction = model.predict([data[&#39;features&#39;]])<\/p>\n<p>    return jsonify({&#39;prediction&#39;: prediction[0]})<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u5b8c\u6210\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u56de\u5f52\u5efa\u6a21\u5b9e\u6218\u6848\u4f8b\u3002\u4ece\u6570\u636e\u51c6\u5907\u3001\u7279\u5f81\u9009\u62e9\u3001\u6a21\u578b\u9009\u62e9\u3001\u6a21\u578b\u8bad\u7ec3\u3001\u6a21\u578b\u8bc4\u4f30\u3001\u6a21\u578b\u8c03\u4f18\u5230\u6a21\u578b\u90e8\u7f72\uff0c\u6574\u4e2a\u8fc7\u7a0b\u5168\u9762\u4e14\u8be6\u7ec6\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e2a\u6848\u4f8b\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u56de\u5f52\u5efa\u6a21\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u56de\u5f52\u5efa\u6a21\u7684\u57fa\u672c\u6982\u5ff5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u56de\u5f52\u5efa\u6a21\u662f\u7edf\u8ba1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u7684\u4e00\u79cd\u91cd\u8981\u6280\u672f\uff0c\u7528\u4e8e\u5206\u6790\u81ea\u53d8\u91cf\u4e0e\u56e0\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u901a\u8fc7\u5efa\u7acb\u56de\u5f52\u6a21\u578b\uff0c\u53ef\u4ee5\u9884\u6d4b\u56e0\u53d8\u91cf\u7684\u503c\uff0c\u4e86\u89e3\u53d8\u91cf\u4e4b\u95f4\u7684\u76f8\u5173\u6027\u3002\u5e38\u89c1\u7684\u56de\u5f52\u6a21\u578b\u5305\u62ec\u7ebf\u6027\u56de\u5f52\u3001\u903b\u8f91\u56de\u5f52\u548c\u591a\u9879\u5f0f\u56de\u5f52\u7b49\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u56de\u5f52\u6a21\u578b\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u56de\u5f52\u6a21\u578b\u901a\u5e38\u53d6\u51b3\u4e8e\u6570\u636e\u7684\u7279\u6027\u548c\u5206\u6790\u7684\u76ee\u7684\u3002\u5982\u679c\u6570\u636e\u5448\u7ebf\u6027\u5173\u7cfb\uff0c\u7ebf\u6027\u56de\u5f52\u53ef\u80fd\u662f\u6700\u4f73\u9009\u62e9\uff1b\u5bf9\u4e8e\u590d\u6742\u7684\u975e\u7ebf\u6027\u5173\u7cfb\uff0c\u591a\u9879\u5f0f\u56de\u5f52\u6216\u5176\u4ed6\u66f4\u590d\u6742\u7684\u6a21\u578b\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\u3002\u4f7f\u7528\u53ef\u89c6\u5316\u5de5\u5177\uff08\u5982\u6563\u70b9\u56fe\uff09\u548c\u76f8\u5173\u6027\u5206\u6790\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u6570\u636e\u7279\u5f81\uff0c\u4ece\u800c\u505a\u51fa\u66f4\u5408\u7406\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bc4\u4f30\u56de\u5f52\u6a21\u578b\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u8bc4\u4f30\u56de\u5f52\u6a21\u578b\u7684\u6027\u80fd\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u6307\u6807\u8fdb\u884c\uff0c\u5305\u62ec\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u3001\u5e73\u5747\u7edd\u5bf9\u8bef\u5dee\uff08MAE\uff09\u548c\u51b3\u5b9a\u7cfb\u6570\uff08R\u00b2\uff09\u3002\u8fd9\u4e9b\u6307\u6807\u80fd\u591f\u53cd\u6620\u6a21\u578b\u9884\u6d4b\u503c\u4e0e\u771f\u5b9e\u503c\u4e4b\u95f4\u7684\u5dee\u5f02\u3002\u4e00\u822c\u6765\u8bf4\uff0cMSE\u548cMAE\u8d8a\u5c0f\uff0c\u6a21\u578b\u7684\u9884\u6d4b\u6548\u679c\u8d8a\u597d\uff1bR\u00b2\u503c\u63a5\u8fd11\u8868\u793a\u6a21\u578b\u89e3\u91ca\u53d8\u91cf\u7684\u80fd\u529b\u8d8a\u5f3a\u3002\u6b64\u5916\uff0c\u4ea4\u53c9\u9a8c\u8bc1\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8bc4\u4f30\u6a21\u578b\u7684\u7a33\u5065\u6027\u548c\u6cdb\u5316\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728 Python \u4e2d\u8fdb\u884c\u56de\u5f52\u5efa\u6a21\u7684\u5173\u952e\u6b65\u9aa4\u5305\u62ec\uff1a\u6570\u636e\u51c6\u5907\u3001\u7279\u5f81\u9009\u62e9\u3001\u6a21\u578b\u9009\u62e9\u3001\u6a21\u578b\u8bad\u7ec3\u3001\u6a21\u578b\u8bc4\u4f30\u3001\u6a21\u578b\u8c03\u4f18\u3001\u5e76 [&hellip;]","protected":false},"author":3,"featured_media":1159183,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159171"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1159171"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159171\/revisions"}],"predecessor-version":[{"id":1159185,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159171\/revisions\/1159185"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1159183"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1159171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1159171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1159171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}