{"id":1172122,"date":"2025-01-15T16:44:43","date_gmt":"2025-01-15T08:44:43","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172122.html"},"modified":"2025-01-15T16:44:45","modified_gmt":"2025-01-15T08:44:45","slug":"python%e6%9c%80%e4%bd%b3%e5%80%bc%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1172122.html","title":{"rendered":"python\u6700\u4f73\u503c\u5982\u4f55\u8bbe\u7f6e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26074255\/f4552e30-ba1d-45b7-a4b6-009775999e57.webp\" alt=\"python\u6700\u4f73\u503c\u5982\u4f55\u8bbe\u7f6e\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8bbe\u7f6e\u6700\u4f73\u503c\uff08\u4e5f\u79f0\u4e3a\u8d85\u53c2\u6570\u4f18\u5316\uff09\u662f\u4e00\u4e2a\u6781\u5176\u91cd\u8981\u7684\u4efb\u52a1\uff0c\u56e0\u4e3a\u4e0d\u540c\u7684\u503c\u4f1a\u5bf9\u6a21\u578b\u7684\u6027\u80fd\u4ea7\u751f\u663e\u8457\u5f71\u54cd\u3002<strong>\u4f7f\u7528\u7f51\u683c\u641c\u7d22\u3001\u968f\u673a\u641c\u7d22\u3001\u8d1d\u53f6\u65af\u4f18\u5316\u3001\u4ea4\u53c9\u9a8c\u8bc1\u3001\u4f7f\u7528\u5b66\u4e60\u66f2\u7ebf<\/strong>\u662f\u8bbe\u7f6e\u6700\u4f73\u503c\u7684\u4e00\u4e9b\u6709\u6548\u65b9\u6cd5\u3002\u5176\u4e2d\uff0c\u7f51\u683c\u641c\u7d22\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u6280\u672f\uff0c\u5b83\u901a\u8fc7\u7a77\u4e3e\u641c\u7d22\u6765\u5bfb\u627e\u6700\u4f73\u7ec4\u5408\u3002\u7f51\u683c\u641c\u7d22\uff08Grid Search\uff09\u662f\u4e00\u79cd\u7cfb\u7edf\u5730\u904d\u5386\u591a\u4e2a\u53c2\u6570\u7ec4\u5408\u7684\u6280\u672f\uff0c\u80fd\u591f\u4fdd\u8bc1\u627e\u5230\u6700\u4f18\u89e3\u3002\u5b83\u901a\u8fc7\u4e3a\u6bcf\u4e2a\u53c2\u6570\u6307\u5b9a\u4e00\u7ec4\u53ef\u80fd\u7684\u503c\uff0c\u7136\u540e\u901a\u8fc7\u4ea4\u53c9\u9a8c\u8bc1\u8bc4\u4f30\u6bcf\u4e2a\u7ec4\u5408\u6765\u9009\u62e9\u6700\u4f73\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7f51\u683c\u641c\u7d22\uff08Grid Search\uff09<\/h3>\n<\/p>\n<p><p>\u7f51\u683c\u641c\u7d22\u662f\u4e00\u79cd\u7b80\u5355\u4e14\u5e38\u7528\u7684\u8d85\u53c2\u6570\u4f18\u5316\u65b9\u6cd5\u3002\u5b83\u901a\u8fc7\u7cfb\u7edf\u5730\u904d\u5386\u6307\u5b9a\u7684\u53c2\u6570\u503c\u7ec4\u5408\u6765\u627e\u5230\u6700\u4f18\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u539f\u7406<\/h4>\n<\/p>\n<p><p>\u7f51\u683c\u641c\u7d22\u7684\u57fa\u672c\u539f\u7406\u662f\u5c06\u6bcf\u4e2a\u8d85\u53c2\u6570\u7684\u53ef\u80fd\u53d6\u503c\u7ec4\u6210\u4e00\u4e2a\u7f51\u683c\uff0c\u7136\u540e\u5bf9\u6bcf\u4e2a\u7ec4\u5408\u8fdb\u884c\u4ea4\u53c9\u9a8c\u8bc1\uff0c\u6700\u7ec8\u9009\u62e9\u8868\u73b0\u6700\u597d\u7684\u53c2\u6570\u7ec4\u5408\u3002\u8fd9\u4e2a\u65b9\u6cd5\u867d\u7136\u7b80\u5355\uff0c\u4f46\u7531\u4e8e\u9700\u8981\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u8ba1\u7b97\u91cf\u8f83\u5927\uff0c\u9002\u7528\u4e8e\u53c2\u6570\u7a7a\u95f4\u8f83\u5c0f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<p><p>\uff081\uff09\u5b9a\u4e49\u53c2\u6570\u7f51\u683c\uff1a\u4e3a\u6bcf\u4e2a\u8d85\u53c2\u6570\u6307\u5b9a\u4e00\u7ec4\u53ef\u80fd\u7684\u53d6\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import GridSearchCV<\/p>\n<p>from sklearn.ensemble import RandomForestClassifier<\/p>\n<p>param_grid = {<\/p>\n<p>    &#39;n_estimators&#39;: [100, 200, 300],<\/p>\n<p>    &#39;max_depth&#39;: [None, 10, 20, 30],<\/p>\n<p>    &#39;min_samples_split&#39;: [2, 5, 10]<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff082\uff09\u521d\u59cb\u5316\u6a21\u578b\uff1a\u9009\u62e9\u8981\u4f18\u5316\u7684\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">model = RandomForestClassifier()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff083\uff09\u6267\u884c\u7f51\u683c\u641c\u7d22\uff1a\u4f7f\u7528\u7f51\u683c\u641c\u7d22\u6765\u5bfb\u627e\u6700\u4f73\u53c2\u6570\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">grid_search = GridSearchCV(estimator=model, param_grid=param_grid, cv=5)<\/p>\n<p>grid_search.fit(X_tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n, y_train)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff084\uff09\u67e5\u770b\u6700\u4f73\u53c2\u6570\uff1a\u901a\u8fc7<code>best_params_<\/code>\u5c5e\u6027\u83b7\u53d6\u6700\u4f18\u53c2\u6570\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">best_params = grid_search.best_params_<\/p>\n<p>print(&quot;Best parameters found: &quot;, best_params)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u968f\u673a\u641c\u7d22\uff08Random Search\uff09<\/h3>\n<\/p>\n<p><p>\u968f\u673a\u641c\u7d22\u662f\u4e00\u79cd\u6539\u8fdb\u7684\u8d85\u53c2\u6570\u4f18\u5316\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7\u968f\u673a\u91c7\u6837\u53c2\u6570\u7a7a\u95f4\u6765\u5bfb\u627e\u6700\u4f73\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u539f\u7406<\/h4>\n<\/p>\n<p><p>\u4e0e\u7f51\u683c\u641c\u7d22\u4e0d\u540c\uff0c\u968f\u673a\u641c\u7d22\u4e0d\u4f1a\u904d\u5386\u6240\u6709\u53ef\u80fd\u7684\u7ec4\u5408\uff0c\u800c\u662f\u5728\u53c2\u6570\u7a7a\u95f4\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u5b9a\u6570\u91cf\u7684\u7ec4\u5408\u8fdb\u884c\u8bc4\u4f30\u3002\u8fd9\u6837\u53ef\u4ee5\u5728\u5927\u5e45\u964d\u4f4e\u8ba1\u7b97\u91cf\u7684\u540c\u65f6\uff0c\u4ecd\u7136\u6709\u8f83\u9ad8\u7684\u6982\u7387\u627e\u5230\u63a5\u8fd1\u6700\u4f18\u7684\u53c2\u6570\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<p><p>\uff081\uff09\u5b9a\u4e49\u53c2\u6570\u5206\u5e03\uff1a\u4e3a\u6bcf\u4e2a\u8d85\u53c2\u6570\u6307\u5b9a\u4e00\u4e2a\u6982\u7387\u5206\u5e03\u6216\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import RandomizedSearchCV<\/p>\n<p>from scipy.stats import randint<\/p>\n<p>param_dist = {<\/p>\n<p>    &#39;n_estimators&#39;: randint(100, 500),<\/p>\n<p>    &#39;max_depth&#39;: [None, 10, 20, 30],<\/p>\n<p>    &#39;min_samples_split&#39;: randint(2, 11)<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff082\uff09\u521d\u59cb\u5316\u6a21\u578b\uff1a\u9009\u62e9\u8981\u4f18\u5316\u7684\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">model = RandomForestClassifier()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff083\uff09\u6267\u884c\u968f\u673a\u641c\u7d22\uff1a\u4f7f\u7528\u968f\u673a\u641c\u7d22\u6765\u5bfb\u627e\u6700\u4f73\u53c2\u6570\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_search = RandomizedSearchCV(estimator=model, param_distributions=param_dist, n_iter=50, cv=5)<\/p>\n<p>random_search.fit(X_train, y_train)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff084\uff09\u67e5\u770b\u6700\u4f73\u53c2\u6570\uff1a\u901a\u8fc7<code>best_params_<\/code>\u5c5e\u6027\u83b7\u53d6\u6700\u4f18\u53c2\u6570\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">best_params = random_search.best_params_<\/p>\n<p>print(&quot;Best parameters found: &quot;, best_params)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8d1d\u53f6\u65af\u4f18\u5316\uff08Bayesian Optimization\uff09<\/h3>\n<\/p>\n<p><p>\u8d1d\u53f6\u65af\u4f18\u5316\u662f\u4e00\u79cd\u66f4\u4e3a\u667a\u80fd\u7684\u8d85\u53c2\u6570\u4f18\u5316\u65b9\u6cd5\uff0c\u5b83\u901a\u8fc7\u6784\u5efa\u4ee3\u7406\u6a21\u578b\u6765\u9884\u6d4b\u53c2\u6570\u7a7a\u95f4\u7684\u8868\u73b0\uff0c\u4ece\u800c\u66f4\u9ad8\u6548\u5730\u627e\u5230\u6700\u4f18\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u539f\u7406<\/h4>\n<\/p>\n<p><p>\u8d1d\u53f6\u65af\u4f18\u5316\u5229\u7528\u4ee3\u7406\u6a21\u578b\uff08\u901a\u5e38\u662f\u9ad8\u65af\u8fc7\u7a0b\uff09\u6765\u6a21\u62df\u76ee\u6807\u51fd\u6570\u7684\u8868\u73b0\uff0c\u5e76\u6839\u636e\u9884\u6d4b\u7684\u7ed3\u679c\u9009\u62e9\u4e0b\u4e00\u4e2a\u8bc4\u4f30\u7684\u53c2\u6570\u7ec4\u5408\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u4f1a\u4e0d\u65ad\u8fed\u4ee3\uff0c\u9010\u6b65\u6536\u655b\u5230\u6700\u4f18\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<p><p>\uff081\uff09\u5b89\u88c5\u5fc5\u8981\u7684\u5e93\uff1a\u8d1d\u53f6\u65af\u4f18\u5316\u901a\u5e38\u9700\u8981\u4f7f\u7528<code>bayesian-optimization<\/code>\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install bayesian-optimization<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff082\uff09\u5b9a\u4e49\u76ee\u6807\u51fd\u6570\uff1a\u76ee\u6807\u51fd\u6570\u5e94\u8be5\u8fd4\u56de\u6a21\u578b\u7684\u8bc4\u4f30\u6307\u6807\uff08\u5982\u51c6\u786e\u7387\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bayes_opt import BayesianOptimization<\/p>\n<p>from sklearn.model_selection import cross_val_score<\/p>\n<p>def rf_cv(n_estimators, max_depth, min_samples_split):<\/p>\n<p>    model = RandomForestClassifier(<\/p>\n<p>        n_estimators=int(n_estimators),<\/p>\n<p>        max_depth=int(max_depth),<\/p>\n<p>        min_samples_split=int(min_samples_split)<\/p>\n<p>    )<\/p>\n<p>    return cross_val_score(model, X_train, y_train, cv=5).mean()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff083\uff09\u5b9a\u4e49\u53c2\u6570\u7a7a\u95f4\uff1a\u4e3a\u6bcf\u4e2a\u8d85\u53c2\u6570\u6307\u5b9a\u4e00\u4e2a\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">param_bounds = {<\/p>\n<p>    &#39;n_estimators&#39;: (100, 500),<\/p>\n<p>    &#39;max_depth&#39;: (10, 30),<\/p>\n<p>    &#39;min_samples_split&#39;: (2, 11)<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff084\uff09\u6267\u884c\u8d1d\u53f6\u65af\u4f18\u5316\uff1a\u4f7f\u7528\u8d1d\u53f6\u65af\u4f18\u5316\u6765\u5bfb\u627e\u6700\u4f73\u53c2\u6570\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">optimizer = BayesianOptimization(f=rf_cv, pbounds=param_bounds, random_state=42)<\/p>\n<p>optimizer.maximize(init_points=10, n_iter=50)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff085\uff09\u67e5\u770b\u6700\u4f73\u53c2\u6570\uff1a\u901a\u8fc7<code>max<\/code>\u5c5e\u6027\u83b7\u53d6\u6700\u4f18\u53c2\u6570\u7ec4\u5408\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">best_params = optimizer.max[&#39;params&#39;]<\/p>\n<p>print(&quot;Best parameters found: &quot;, best_params)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4ea4\u53c9\u9a8c\u8bc1\uff08Cross-Validation\uff09<\/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\u591a\u6b21\u5206\u5272\u6570\u636e\u96c6\u6765\u83b7\u5f97\u66f4\u7a33\u5b9a\u7684\u8bc4\u4f30\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u539f\u7406<\/h4>\n<\/p>\n<p><p>\u4ea4\u53c9\u9a8c\u8bc1\u7684\u57fa\u672c\u539f\u7406\u662f\u5c06\u6570\u636e\u96c6\u5206\u4e3aK\u4e2a\u5b50\u96c6\uff0c\u7136\u540e\u8fdb\u884cK\u6b21\u8bad\u7ec3\u548c\u6d4b\u8bd5\uff0c\u6bcf\u6b21\u4f7f\u7528\u4e00\u4e2a\u5b50\u96c6\u4f5c\u4e3a\u6d4b\u8bd5\u96c6\uff0c\u5176\u4ed6\u5b50\u96c6\u4f5c\u4e3a\u8bad\u7ec3\u96c6\u3002\u6700\u7ec8\u7684\u8bc4\u4f30\u7ed3\u679c\u662fK\u6b21\u6d4b\u8bd5\u7ed3\u679c\u7684\u5e73\u5747\u503c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<p><p>\uff081\uff09\u9009\u62e9\u4ea4\u53c9\u9a8c\u8bc1\u65b9\u6cd5\uff1a\u5e38\u89c1\u7684\u4ea4\u53c9\u9a8c\u8bc1\u65b9\u6cd5\u5305\u62ecK\u6298\u4ea4\u53c9\u9a8c\u8bc1\u3001\u7559\u4e00\u6cd5\u4ea4\u53c9\u9a8c\u8bc1\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import cross_val_score<\/p>\n<p>from sklearn.ensemble import RandomForestClassifier<\/p>\n<p>model = RandomForestClassifier()<\/p>\n<p>scores = cross_val_score(model, X_train, y_train, cv=5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff082\uff09\u67e5\u770b\u8bc4\u4f30\u7ed3\u679c\uff1a\u901a\u8fc7<code>mean<\/code>\u65b9\u6cd5\u83b7\u53d6\u5e73\u5747\u8bc4\u4f30\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;Cross-validation scores: &quot;, scores)<\/p>\n<p>print(&quot;Mean score: &quot;, scores.mean())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5b66\u4e60\u66f2\u7ebf\uff08Learning Curve\uff09<\/h3>\n<\/p>\n<p><p>\u5b66\u4e60\u66f2\u7ebf\u662f\u4e00\u79cd\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u968f\u8bad\u7ec3\u6570\u636e\u91cf\u53d8\u5316\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\u548c\u6570\u636e\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u539f\u7406<\/h4>\n<\/p>\n<p><p>\u5b66\u4e60\u66f2\u7ebf\u901a\u8fc7\u5728\u4e0d\u540c\u5927\u5c0f\u7684\u8bad\u7ec3\u96c6\u4e0a\u8bad\u7ec3\u6a21\u578b\uff0c\u5e76\u8bc4\u4f30\u5176\u5728\u8bad\u7ec3\u96c6\u548c\u9a8c\u8bc1\u96c6\u4e0a\u7684\u8868\u73b0\uff0c\u4ece\u800c\u53cd\u6620\u51fa\u6a21\u578b\u7684\u5b66\u4e60\u8fc7\u7a0b\u548c\u6570\u636e\u9700\u6c42\u3002\u901a\u8fc7\u89c2\u5bdf\u5b66\u4e60\u66f2\u7ebf\uff0c\u6211\u4eec\u53ef\u4ee5\u5224\u65ad\u6a21\u578b\u662f\u5426\u5b58\u5728\u8fc7\u62df\u5408\u6216\u6b20\u62df\u5408\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<\/p>\n<p><p>\uff081\uff09\u751f\u6210\u5b66\u4e60\u66f2\u7ebf\uff1a\u4f7f\u7528<code>learning_curve<\/code>\u51fd\u6570\u751f\u6210\u5b66\u4e60\u66f2\u7ebf\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import learning_curve<\/p>\n<p>import numpy as np<\/p>\n<p>train_sizes, train_scores, test_scores = learning_curve(<\/p>\n<p>    model, X_train, y_train, cv=5, n_jobs=-1, train_sizes=np.linspace(0.1, 1.0, 10)<\/p>\n<p>)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff082\uff09\u8ba1\u7b97\u5e73\u5747\u5f97\u5206\uff1a\u8ba1\u7b97\u8bad\u7ec3\u96c6\u548c\u9a8c\u8bc1\u96c6\u7684\u5e73\u5747\u5f97\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">train_scores_mean = np.mean(train_scores, axis=1)<\/p>\n<p>test_scores_mean = np.mean(test_scores, axis=1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\uff083\uff09\u7ed8\u5236\u5b66\u4e60\u66f2\u7ebf\uff1a\u4f7f\u7528<code>matplotlib<\/code>\u7ed8\u5236\u5b66\u4e60\u66f2\u7ebf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.figure()<\/p>\n<p>plt.plot(train_sizes, train_scores_mean, &#39;o-&#39;, color=&quot;r&quot;, label=&quot;Training score&quot;)<\/p>\n<p>plt.plot(train_sizes, test_scores_mean, &#39;o-&#39;, color=&quot;g&quot;, label=&quot;Cross-validation score&quot;)<\/p>\n<p>plt.xlabel(&quot;Training examples&quot;)<\/p>\n<p>plt.ylabel(&quot;Score&quot;)<\/p>\n<p>plt.legend(loc=&quot;best&quot;)<\/p>\n<p>plt.title(&quot;Learning Curve&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u4f18\u5316\u8d85\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u8fdb\u884c\u8d85\u53c2\u6570\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5148\u4f7f\u7528\u968f\u673a\u641c\u7d22\u6765\u7c97\u7565\u641c\u7d22\u53c2\u6570\u7a7a\u95f4\uff0c\u7136\u540e\u518d\u4f7f\u7528\u7f51\u683c\u641c\u7d22\u5bf9\u7ed3\u679c\u8fdb\u884c\u7cbe\u7ec6\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u521d\u6b65\u968f\u673a\u641c\u7d22<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528\u968f\u673a\u641c\u7d22\u5728\u8f83\u5927\u7684\u53c2\u6570\u7a7a\u95f4\u4e2d\u8fdb\u884c\u521d\u6b65\u641c\u7d22\uff0c\u627e\u5230\u8868\u73b0\u8f83\u597d\u7684\u53c2\u6570\u8303\u56f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_search = RandomizedSearchCV(estimator=model, param_distributions=param_dist, n_iter=50, cv=5)<\/p>\n<p>random_search.fit(X_train, y_train)<\/p>\n<p>best_params_random = random_search.best_params_<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u7cbe\u7ec6\u7f51\u683c\u641c\u7d22<\/h4>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u6839\u636e\u968f\u673a\u641c\u7d22\u7684\u7ed3\u679c\uff0c\u7f29\u5c0f\u53c2\u6570\u8303\u56f4\uff0c\u4f7f\u7528\u7f51\u683c\u641c\u7d22\u8fdb\u884c\u7cbe\u7ec6\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">param_grid_fine = {<\/p>\n<p>    &#39;n_estimators&#39;: [best_params_random[&#39;n_estimators&#39;]-50, best_params_random[&#39;n_estimators&#39;], best_params_random[&#39;n_estimators&#39;]+50],<\/p>\n<p>    &#39;max_depth&#39;: [best_params_random[&#39;max_depth&#39;]-5, best_params_random[&#39;max_depth&#39;], best_params_random[&#39;max_depth&#39;]+5],<\/p>\n<p>    &#39;min_samples_split&#39;: [best_params_random[&#39;min_samples_split&#39;]-1, best_params_random[&#39;min_samples_split&#39;], best_params_random[&#39;min_samples_split&#39;]+1]<\/p>\n<p>}<\/p>\n<p>grid_search_fine = GridSearchCV(estimator=model, param_grid=param_grid_fine, cv=5)<\/p>\n<p>grid_search_fine.fit(X_train, y_train)<\/p>\n<p>best_params_fine = grid_search_fine.best_params_<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u53c2\u6570\u8c03\u4f18\u7684\u5b9e\u8df5\u5efa\u8bae<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u53c2\u6570\u8c03\u4f18\u65f6\uff0c\u6709\u51e0\u4e2a\u5b9e\u8df5\u5efa\u8bae\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u6548\u7387\u548c\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5206\u6b65\u8fdb\u884c<\/h4>\n<\/p>\n<p><p>\u53c2\u6570\u8c03\u4f18\u6700\u597d\u5206\u6b65\u8fdb\u884c\uff0c\u5148\u5bf9\u5f71\u54cd\u8f83\u5927\u7684\u53c2\u6570\u8fdb\u884c\u8c03\u6574\uff0c\u7136\u540e\u518d\u8c03\u6574\u6b21\u8981\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u968f\u673a\u68ee\u6797\uff0c\u53ef\u4ee5\u5148\u8c03\u6574\u6811\u7684\u6570\u91cf\u548c\u6df1\u5ea6\uff0c\u7136\u540e\u518d\u8c03\u6574\u5206\u88c2\u6807\u51c6\u548c\u53f6\u8282\u70b9\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528\u8f83\u5c0f\u7684\u6570\u636e\u96c6<\/h4>\n<\/p>\n<p><p>\u5728\u521d\u6b65\u8c03\u6574\u53c2\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u8f83\u5c0f\u7684\u6570\u636e\u96c6\u8fdb\u884c\u5feb\u901f\u6d4b\u8bd5\uff0c\u627e\u5230\u8f83\u597d\u7684\u53c2\u6570\u8303\u56f4\u540e\uff0c\u518d\u4f7f\u7528\u5168\u91cf\u6570\u636e\u8fdb\u884c\u7cbe\u7ec6\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5173\u6ce8\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b<\/h4>\n<\/p>\n<p><p>\u5728\u8c03\u6574\u53c2\u6570\u65f6\uff0c\u4e0d\u4ec5\u8981\u5173\u6ce8\u6a21\u578b\u5728\u8bad\u7ec3\u96c6\u4e0a\u7684\u8868\u73b0\uff0c\u66f4\u8981\u5173\u6ce8\u5176\u5728\u9a8c\u8bc1\u96c6\u4e0a\u7684\u8868\u73b0\uff0c\u4ee5\u907f\u514d\u8fc7\u62df\u5408\u548c\u6b20\u62df\u5408\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u7ed3\u5408\u4e1a\u52a1\u9700\u6c42<\/h4>\n<\/p>\n<p><p>\u53c2\u6570\u8c03\u4f18\u4e0d\u4ec5\u4ec5\u662f\u4e3a\u4e86\u63d0\u9ad8\u6a21\u578b\u7684\u51c6\u786e\u7387\uff0c\u8fd8\u8981\u7ed3\u5408\u4e1a\u52a1\u9700\u6c42\uff0c\u4f8b\u5982\u6a21\u578b\u7684\u8ba1\u7b97\u6548\u7387\u3001\u54cd\u5e94\u901f\u5ea6\u7b49\u3002\u9009\u62e9\u9002\u5f53\u7684\u53c2\u6570\u5e73\u8861\u6a21\u578b\u6027\u80fd\u548c\u5b9e\u9645\u5e94\u7528\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u6848\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u8d85\u53c2\u6570\u4f18\u5316\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9645\u6848\u4f8b\u6765\u6f14\u793a\u6574\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u95ee\u9898\u63cf\u8ff0<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u8981\u4f7f\u7528\u968f\u673a\u68ee\u6797\u6a21\u578b\u5bf9\u4e00\u4e2a\u5206\u7c7b\u4efb\u52a1\u8fdb\u884c\u5efa\u6a21\uff0c\u76ee\u6807\u662f\u901a\u8fc7\u8c03\u6574\u8d85\u53c2\u6570\u63d0\u9ad8\u6a21\u578b\u7684\u51c6\u786e\u7387\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u51c6\u5907<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u51c6\u5907\u597d\u8bad\u7ec3\u6570\u636e\u548c\u6d4b\u8bd5\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.datasets import load_iris<\/p>\n<p>from sklearn.model_selection import train_test_split<\/p>\n<p>data = load_iris()<\/p>\n<p>X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2, random_state=42)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u521d\u6b65\u968f\u673a\u641c\u7d22<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u968f\u673a\u641c\u7d22\u5728\u8f83\u5927\u7684\u53c2\u6570\u7a7a\u95f4\u4e2d\u8fdb\u884c\u521d\u6b65\u641c\u7d22\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">param_dist = {<\/p>\n<p>    &#39;n_estimators&#39;: randint(100, 500),<\/p>\n<p>    &#39;max_depth&#39;: [None, 10, 20, 30],<\/p>\n<p>    &#39;min_samples_split&#39;: randint(2, 11)<\/p>\n<p>}<\/p>\n<p>random_search = RandomizedSearchCV(estimator=RandomForestClassifier(), param_distributions=param_dist, n_iter=50, cv=5)<\/p>\n<p>random_search.fit(X_train, y_train)<\/p>\n<p>best_params_random = random_search.best_params_<\/p>\n<p>print(&quot;Best parameters from random search: &quot;, best_params_random)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u7cbe\u7ec6\u7f51\u683c\u641c\u7d22<\/h4>\n<\/p>\n<p><p>\u6839\u636e\u968f\u673a\u641c\u7d22\u7684\u7ed3\u679c\uff0c\u7f29\u5c0f\u53c2\u6570\u8303\u56f4\uff0c\u4f7f\u7528\u7f51\u683c\u641c\u7d22\u8fdb\u884c\u7cbe\u7ec6\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">param_grid_fine = {<\/p>\n<p>    &#39;n_estimators&#39;: [best_params_random[&#39;n_estimators&#39;]-50, best_params_random[&#39;n_estimators&#39;], best_params_random[&#39;n_estimators&#39;]+50],<\/p>\n<p>    &#39;max_depth&#39;: [best_params_random[&#39;max_depth&#39;]-5, best_params_random[&#39;max_depth&#39;], best_params_random[&#39;max_depth&#39;]+5],<\/p>\n<p>    &#39;min_samples_split&#39;: [best_params_random[&#39;min_samples_split&#39;]-1, best_params_random[&#39;min_samples_split&#39;], best_params_random[&#39;min_samples_split&#39;]+1]<\/p>\n<p>}<\/p>\n<p>grid_search_fine = GridSearchCV(estimator=RandomForestClassifier(), param_grid=param_grid_fine, cv=5)<\/p>\n<p>grid_search_fine.fit(X_train, y_train)<\/p>\n<p>best_params_fine = grid_search_fine.best_params_<\/p>\n<p>print(&quot;Best parameters from grid search: &quot;, best_params_fine)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5\u3001\u6a21\u578b\u8bc4\u4f30<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u6700\u4f73\u53c2\u6570\u8bad\u7ec3\u6a21\u578b\uff0c\u5e76\u5728\u6d4b\u8bd5\u96c6\u4e0a\u8fdb\u884c\u8bc4\u4f30\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">model_best = RandomForestClassifier(best_params_fine)<\/p>\n<p>model_best.fit(X_train, y_train)<\/p>\n<p>accuracy = model_best.score(X_test, y_test)<\/p>\n<p>print(&quot;Model accuracy with best parameters: &quot;, accuracy)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u7cfb\u7edf\u5730\u8fdb\u884c\u8d85\u53c2\u6570\u4f18\u5316\uff0c\u627e\u5230\u6700\u4f18\u53c2\u6570\u7ec4\u5408\uff0c\u4ece\u800c\u63d0\u5347\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u8bbe\u7f6e\u6700\u4f73\u503c\u7684\u65b9\u6cd5\uff0c\u5305\u62ec<strong>\u7f51\u683c\u641c\u7d22\u3001\u968f\u673a\u641c\u7d22\u3001\u8d1d\u53f6\u65af\u4f18\u5316\u3001\u4ea4\u53c9\u9a8c\u8bc1\u3001\u5b66\u4e60\u66f2\u7ebf<\/strong>\u7b49\u3002\u540c\u65f6\uff0c\u6211\u4eec\u901a\u8fc7\u5b9e\u9645\u6848\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u8fdb\u884c\u53c2\u6570\u8c03\u4f18\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u548c\u5b9e\u8df5\u5efa\u8bae\uff0c\u80fd\u591f\u5e2e\u52a9\u5927\u5bb6\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u6a21\u578b\u8c03\u4f18\uff0c\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u548c\u6cdb\u5316\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bbe\u7f6e\u6700\u4f73\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8bbe\u7f6e\u6700\u4f73\u503c\u901a\u5e38\u6d89\u53ca\u5230\u8c03\u6574\u53c2\u6570\u4ee5\u4f18\u5316\u6a21\u578b\u6216\u7b97\u6cd5\u7684\u6027\u80fd\u3002\u53ef\u4ee5\u901a\u8fc7\u4ea4\u53c9\u9a8c\u8bc1\u3001\u7f51\u683c\u641c\u7d22\u6216\u968f\u673a\u641c\u7d22\u7b49\u65b9\u6cd5\u6765\u627e\u5230\u6700\u4f73\u53c2\u6570\u3002\u4f7f\u7528\u50cfScikit-learn\u8fd9\u6837\u7684\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e9b\u6280\u672f\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u51c6\u786e\u6027\u548c\u6548\u7387\u3002<\/p>\n<p><strong>\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u9879\u76ee\u4e2d\uff0c\u5982\u4f55\u786e\u5b9a\u6700\u4f73\u503c\uff1f<\/strong><br \/>\u786e\u5b9a\u6700\u4f73\u503c\u9700\u8981\u8bc4\u4f30\u4e0d\u540c\u53c2\u6570\u5bf9\u6a21\u578b\u6027\u80fd\u7684\u5f71\u54cd\u3002\u53ef\u4ee5\u901a\u8fc7\u7ed8\u5236\u5b66\u4e60\u66f2\u7ebf\u3001\u6df7\u6dc6\u77e9\u9635\u548cROC\u66f2\u7ebf\u7b49\u53ef\u89c6\u5316\u5de5\u5177\u6765\u89c2\u5bdf\u6a21\u578b\u7684\u8868\u73b0\u3002\u540c\u65f6\uff0c\u91c7\u7528K\u6298\u4ea4\u53c9\u9a8c\u8bc1\u53ef\u4ee5\u6709\u6548\u907f\u514d\u8fc7\u62df\u5408\uff0c\u5e76\u5e2e\u52a9\u627e\u5230\u6700\u4f18\u53c2\u6570\u3002<\/p>\n<p><strong>\u6700\u4f73\u503c\u8bbe\u7f6e\u5bf9\u7a0b\u5e8f\u6027\u80fd\u6709\u4f55\u5f71\u54cd\uff1f<\/strong><br \/>\u8bbe\u7f6e\u6700\u4f73\u503c\u80fd\u591f\u663e\u8457\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u51c6\u786e\u6027\u3002\u5728\u6570\u636e\u79d1\u5b66\u548c\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u5408\u7406\u7684\u53c2\u6570\u914d\u7f6e\u80fd\u591f\u63d0\u9ad8\u6a21\u578b\u5bf9\u65b0\u6570\u636e\u7684\u6cdb\u5316\u80fd\u529b\uff0c\u51cf\u5c11\u8bef\u5dee\uff0c\u4ece\u800c\u4f7f\u5f97\u9884\u6d4b\u7ed3\u679c\u66f4\u52a0\u53ef\u9760\u3002\u8fd9\u5bf9\u4e8e\u5b9e\u9645\u5e94\u7528\u4e2d\u81f3\u5173\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u9762\u4e34\u590d\u6742\u6570\u636e\u96c6\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8bbe\u7f6e\u6700\u4f73\u503c\uff08\u4e5f\u79f0\u4e3a\u8d85\u53c2\u6570\u4f18\u5316\uff09\u662f\u4e00\u4e2a\u6781\u5176\u91cd\u8981\u7684\u4efb\u52a1\uff0c\u56e0\u4e3a\u4e0d\u540c\u7684\u503c\u4f1a\u5bf9\u6a21\u578b\u7684\u6027\u80fd\u4ea7\u751f\u663e\u8457\u5f71\u54cd\u3002\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1172125,"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\/1172122"}],"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=1172122"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172122\/revisions"}],"predecessor-version":[{"id":1172126,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1172122\/revisions\/1172126"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1172125"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1172122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1172122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1172122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}