{"id":1019065,"date":"2024-12-27T12:50:19","date_gmt":"2024-12-27T04:50:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1019065.html"},"modified":"2024-12-27T12:50:22","modified_gmt":"2024-12-27T04:50:22","slug":"python%e5%a6%82%e4%bd%95%e5%bb%ba%e7%ab%8b%e5%9b%9e%e5%bd%92%e6%8b%9f%e5%90%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1019065.html","title":{"rendered":"python\u5982\u4f55\u5efa\u7acb\u56de\u5f52\u62df\u5408"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25161624\/53712f0d-a96c-40eb-b442-69e8bfdfcc32.webp\" alt=\"python\u5982\u4f55\u5efa\u7acb\u56de\u5f52\u62df\u5408\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u8fdb\u884c\u56de\u5f52\u62df\u5408\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<strong>Scikit-Learn\u5e93\u3001Statsmodels\u5e93\u548c\u4f7f\u7528NumPy\u8fdb\u884c\u624b\u52a8\u8ba1\u7b97<\/strong>\u3002\u5176\u4e2d\uff0cScikit-Learn\u56e0\u4e3a\u5176\u7b80\u5355\u6613\u7528\u548c\u5e7f\u6cdb\u7684\u529f\u80fd\u800c\u5e7f\u53d7\u6b22\u8fce\u3002\u5728\u4f7f\u7528Scikit-Learn\u8fdb\u884c\u56de\u5f52\u62df\u5408\u65f6\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\uff0c\u5982\u7ebf\u6027\u56de\u5f52\u3001\u5cad\u56de\u5f52\u6216Lasso\u56de\u5f52\uff0c\u7136\u540e\u51c6\u5907\u6570\u636e\u5e76\u8fdb\u884c\u8bad\u7ec3\u3002\u5728\u8be6\u7ec6\u63cf\u8ff0\u4e2d\uff0c\u6211\u4eec\u5c06\u4ee5\u7ebf\u6027\u56de\u5f52\u4e3a\u4f8b\uff0c\u8bf4\u660e\u5982\u4f55\u4f7f\u7528Scikit-Learn\u8fdb\u884c\u56de\u5f52\u62df\u5408\u3002<\/p>\n<\/p>\n<p><p><strong>Scikit-Learn\u5e93\u662fPython\u4e2d<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6570\u636e\u79d1\u5b66\u7684\u4e00\u4e2a\u91cd\u8981\u5de5\u5177\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u4e2d\u5c0f\u89c4\u6a21\u7684\u6570\u636e\u96c6\u3002\u5bf9\u4e8e\u7ebf\u6027\u56de\u5f52\uff0cScikit-Learn\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u660e\u4e86\u7684API\uff0c\u4f7f\u7528\u6237\u80fd\u591f\u5feb\u901f\u5730\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3\u548c\u8bc4\u4f30\u3002\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u6570\u636e\uff0c\u5e76\u5c06\u6570\u636e\u5212\u5206\u4e3a\u7279\u5f81\uff08X\uff09\u548c\u76ee\u6807\u53d8\u91cf\uff08y\uff09\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_test_split<\/code>\u51fd\u6570\u5c06\u6570\u636e\u96c6\u62c6\u5206\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u3002\u8fd9\u4e00\u6b65\u5bf9\u4e8e\u9a8c\u8bc1\u6a21\u578b\u7684\u6027\u80fd\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8bc4\u4f30\u6a21\u578b\u5728\u672a\u89c1\u8fc7\u7684\u6570\u636e\u4e0a\u7684\u8868\u73b0\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5bfc\u5165<code>LinearRegression<\/code>\u7c7b\u5e76\u5b9e\u4f8b\u5316\u4e00\u4e2a\u6a21\u578b\u5bf9\u8c61\u3002\u4f7f\u7528\u8bad\u7ec3\u6570\u636e\u8c03\u7528<code>fit<\/code>\u65b9\u6cd5\u4ee5\u8bad\u7ec3\u6a21\u578b\u3002\u6a21\u578b\u8bad\u7ec3\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>predict<\/code>\u65b9\u6cd5\u5bf9\u6d4b\u8bd5\u6570\u636e\u8fdb\u884c\u9884\u6d4b\uff0c\u6700\u540e\u4f7f\u7528\u5404\u79cd\u8bc4\u4f30\u6307\u6807\u5982\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u6216\u51b3\u5b9a\u7cfb\u6570\uff08R\u00b2\uff09\u6765\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u56de\u5f52\u62df\u5408\uff0c\u4ecb\u7ecd\u4e0d\u540c\u7684\u5e93\u548c\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u89e3\u91ca\u6a21\u578b\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u7ebf\u6027\u56de\u5f52\u4e0eScikit-Learn<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u56de\u5f52\u5206\u6790\u65f6\uff0c\u7ebf\u6027\u56de\u5f52\u662f\u6700\u57fa\u672c\u7684\u6a21\u578b\u4e4b\u4e00\u3002\u5b83\u5047\u8bbe\u56e0\u53d8\u91cf\u4e0e\u81ea\u53d8\u91cf\u4e4b\u95f4\u5b58\u5728\u7ebf\u6027\u5173\u7cfb\u3002Scikit-Learn\u63d0\u4f9b\u4e86\u4e00\u79cd\u975e\u5e38\u4fbf\u6377\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u7ebf\u6027\u56de\u5f52\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u51c6\u5907\u4e0e\u9884\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u8fdb\u884c\u56de\u5f52\u5206\u6790\u7684\u7b2c\u4e00\u6b65\u662f\u6570\u636e\u51c6\u5907\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff0c\u5982<code>pandas<\/code>\u7528\u4e8e\u6570\u636e\u64cd\u4f5c\uff0c<code>numpy<\/code>\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0c\u4ee5\u53ca<code>matplotlib<\/code>\u548c<code>seaborn<\/code>\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u3002\u6570\u636e\u9884\u5904\u7406\u5305\u62ec\u5904\u7406\u7f3a\u5931\u503c\u3001\u8f6c\u6362\u7c7b\u522b\u53d8\u91cf\u3001\u6807\u51c6\u5316\u6216\u5f52\u4e00\u5316\u6570\u503c\u53d8\u91cf\u7b49\u6b65\u9aa4\u3002\u6240\u6709\u8fd9\u4e9b\u6b65\u9aa4\u7684\u76ee\u7684\u662f\u786e\u4fdd\u6570\u636e\u7684\u8d28\u91cf\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9e\u73b0\u7ebf\u6027\u56de\u5f52<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u65e6\u6570\u636e\u51c6\u5907\u5c31\u7eea\uff0c\u53ef\u4ee5\u4f7f\u7528Scikit-Learn\u4e2d\u7684<code>LinearRegression<\/code>\u7c7b\u6765\u5b9e\u73b0\u7ebf\u6027\u56de\u5f52\u3002\u9996\u5148\uff0c\u5212\u5206\u6570\u636e\u96c6\u4e3a\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\uff0c\u8fd9\u901a\u5e38\u901a\u8fc7<code>train_test_split<\/code>\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u7136\u540e\uff0c\u521b\u5efa<code>LinearRegression<\/code>\u5bf9\u8c61\u5e76\u4f7f\u7528\u8bad\u7ec3\u6570\u636e\u8c03\u7528<code>fit<\/code>\u65b9\u6cd5\u8fdb\u884c\u6a21\u578b\u62df\u5408\u3002\u62df\u5408\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>predict<\/code>\u65b9\u6cd5\u5bf9\u6d4b\u8bd5\u6570\u636e\u8fdb\u884c\u9884\u6d4b\u3002\u8bc4\u4f30\u6a21\u578b\u7684\u5e38\u7528\u6307\u6807\u5305\u62ec\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u3001\u5747\u65b9\u6839\u8bef\u5dee\uff08RMSE\uff09\u548c\u51b3\u5b9a\u7cfb\u6570\uff08R\u00b2\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import train_test_split<\/p>\n<p>from sklearn.linear_model import LinearRegression<\/p>\n<p>from sklearn.metrics import mean_squared_error, r2_score<\/p>\n<h2><strong>\u5047\u8bbe X \u662f\u7279\u5f81\uff0cy \u662f\u76ee\u6807\u53d8\u91cf<\/strong><\/h2>\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>model = LinearRegression()<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<p>y_pred = model.predict(X_test)<\/p>\n<p>print(&quot;Mean Squared Error:&quot;, mean_squared_error(y_test, y_pred))<\/p>\n<p>print(&quot;R\u00b2 Score:&quot;, r2_score(y_test, y_pred))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u975e\u7ebf\u6027\u56de\u5f52\u4e0e\u591a\u9879\u5f0f\u56de\u5f52<\/p>\n<\/p>\n<p><p>\u5f53\u6570\u636e\u4e0d\u6ee1\u8db3\u7ebf\u6027\u5173\u7cfb\u7684\u5047\u8bbe\u65f6\uff0c\u975e\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u53ef\u80fd\u66f4\u4e3a\u5408\u9002\u3002\u591a\u9879\u5f0f\u56de\u5f52\u662f\u975e\u7ebf\u6027\u56de\u5f52\u7684\u4e00\u79cd\u7279\u6b8a\u5f62\u5f0f\uff0c\u901a\u8fc7\u589e\u52a0\u7279\u5f81\u7684\u591a\u9879\u5f0f\u9879\u6765\u6355\u6349\u975e\u7ebf\u6027\u5173\u7cfb\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u591a\u9879\u5f0f\u56de\u5f52<\/strong><\/p>\n<\/p>\n<p><p>\u591a\u9879\u5f0f\u56de\u5f52\u901a\u8fc7\u5c06\u8f93\u5165\u7279\u5f81\u5347\u7ef4\u6210\u591a\u9879\u5f0f\u5f62\u5f0f\u6765\u5904\u7406\u975e\u7ebf\u6027\u5173\u7cfb\u3002Scikit-Learn\u4e2d\u7684<code>PolynomialFeatures<\/code>\u7c7b\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u751f\u6210\u591a\u9879\u5f0f\u7279\u5f81\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import PolynomialFeatures<\/p>\n<p>from sklearn.pipeline import make_pipeline<\/p>\n<h2><strong>\u751f\u6210\u591a\u9879\u5f0f\u7279\u5f81<\/strong><\/h2>\n<p>poly = PolynomialFeatures(degree=2)<\/p>\n<p>X_poly = poly.fit_transform(X)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u7ba1\u9053<\/strong><\/h2>\n<p>model = make_pipeline(PolynomialFeatures(degree=2), LinearRegression())<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<p>y_pred = model.predict(X_test)<\/p>\n<p>print(&quot;Mean Squared Error:&quot;, mean_squared_error(y_test, y_pred))<\/p>\n<p>print(&quot;R\u00b2 Score:&quot;, r2_score(y_test, y_pred))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u591a\u9879\u5f0f\u6b21\u6570<\/strong><\/p>\n<\/p>\n<p><p>\u9009\u62e9\u591a\u9879\u5f0f\u7684\u6b21\u6570\uff08degree\uff09\u662f\u591a\u9879\u5f0f\u56de\u5f52\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u51b3\u7b56\u3002\u6b21\u6570\u8fc7\u4f4e\u53ef\u80fd\u5bfc\u81f4\u6b20\u62df\u5408\uff0c\u800c\u6b21\u6570\u8fc7\u9ad8\u53ef\u80fd\u5bfc\u81f4\u8fc7\u62df\u5408\u3002\u901a\u5e38\uff0c\u4ea4\u53c9\u9a8c\u8bc1\u662f\u9009\u62e9\u5408\u9002\u6b21\u6570\u7684\u6709\u6548\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u5cad\u56de\u5f52\u4e0eLasso\u56de\u5f52<\/p>\n<\/p>\n<p><p>\u5f53\u6570\u636e\u4e2d\u5b58\u5728\u591a\u91cd\u5171\u7ebf\u6027\u6216\u6211\u4eec\u5e0c\u671b\u5bf9\u7279\u5f81\u8fdb\u884c\u9009\u62e9\u65f6\uff0c\u5cad\u56de\u5f52\u548cLasso\u56de\u5f52\u662f\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002\u8fd9\u4e9b\u65b9\u6cd5\u901a\u8fc7\u589e\u52a0\u6b63\u5219\u5316\u9879\u6765\u63a7\u5236\u6a21\u578b\u7684\u590d\u6742\u5ea6\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5cad\u56de\u5f52<\/strong><\/p>\n<\/p>\n<p><p>\u5cad\u56de\u5f52\u901a\u8fc7\u5728\u635f\u5931\u51fd\u6570\u4e2d\u589e\u52a0L2\u6b63\u5219\u5316\u9879\u6765\u60e9\u7f5a\u5927\u7cfb\u6570\uff0c\u4ece\u800c\u51cf\u5c0f\u591a\u91cd\u5171\u7ebf\u6027\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import Ridge<\/p>\n<p>model = Ridge(alpha=1.0)<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<p>y_pred = model.predict(X_test)<\/p>\n<p>print(&quot;Mean Squared Error:&quot;, mean_squared_error(y_test, y_pred))<\/p>\n<p>print(&quot;R\u00b2 Score:&quot;, r2_score(y_test, y_pred))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>Lasso\u56de\u5f52<\/strong><\/p>\n<\/p>\n<p><p>Lasso\u56de\u5f52\u901a\u8fc7\u5728\u635f\u5931\u51fd\u6570\u4e2d\u589e\u52a0L1\u6b63\u5219\u5316\u9879\uff0c\u4f7f\u5f97\u4e00\u4e9b\u7cfb\u6570\u53ef\u4ee5\u88ab\u538b\u7f29\u4e3a0\uff0c\u4ece\u800c\u5b9e\u73b0\u7279\u5f81\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import Lasso<\/p>\n<p>model = Lasso(alpha=0.1)<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<p>y_pred = model.predict(X_test)<\/p>\n<p>print(&quot;Mean Squared Error:&quot;, mean_squared_error(y_test, y_pred))<\/p>\n<p>print(&quot;R\u00b2 Score:&quot;, r2_score(y_test, y_pred))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u56db\u3001\u4f7f\u7528Statsmodels\u8fdb\u884c\u56de\u5f52\u5206\u6790<\/p>\n<\/p>\n<p><p>Statsmodels\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u56de\u5f52\u5206\u6790\u7684\u5f3a\u5927\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u8be6\u7ec6\u7684\u7edf\u8ba1\u4fe1\u606f\u548c\u8bca\u65ad\u5de5\u5177\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u7ebf\u6027\u56de\u5f52<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528Statsmodels\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\u65f6\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u6dfb\u52a0\u5e38\u6570\u9879\uff08\u622a\u8ddd\uff09\u3002\u7136\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>OLS<\/code>\u51fd\u6570\u8fdb\u884c\u666e\u901a\u6700\u5c0f\u4e8c\u4e58\u56de\u5f52\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import statsmodels.api as sm<\/p>\n<p>X = sm.add_constant(X)  # \u6dfb\u52a0\u5e38\u6570\u9879<\/p>\n<p>model = sm.OLS(y, X).fit()<\/p>\n<p>predictions = model.predict(X_test)<\/p>\n<p>print(model.summary())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6a21\u578b\u8bca\u65ad\u4e0e\u89e3\u91ca<\/strong><\/p>\n<\/p>\n<p><p>Statsmodels\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u56de\u5f52\u7ed3\u679c\uff0c\u5305\u62ec\u7cfb\u6570\u7684t\u503c\u3001p\u503c\u3001R\u00b2\u503c\u7b49\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u6b8b\u5dee\u5206\u6790\u548c\u8bca\u65ad\uff0c\u4ee5\u8bc4\u4f30\u6a21\u578b\u7684\u9002\u7528\u6027\u548c\u5047\u8bbe\u7684\u6ee1\u8db3\u7a0b\u5ea6\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u6a21\u578b\u9009\u62e9\u4e0e\u8bc4\u4f30<\/p>\n<\/p>\n<p><p>\u5728\u56de\u5f52\u5206\u6790\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u548c\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u662f\u5173\u952e\u6b65\u9aa4\u3002\u9664\u4e86\u524d\u9762\u63d0\u5230\u7684\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u548c\u51b3\u5b9a\u7cfb\u6570\uff08R\u00b2\uff09\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ea4\u53c9\u9a8c\u8bc1\u3001AIC\u3001BIC\u7b49\u6307\u6807\u8fdb\u884c\u6a21\u578b\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4ea4\u53c9\u9a8c\u8bc1<\/strong><\/p>\n<\/p>\n<p><p>\u4ea4\u53c9\u9a8c\u8bc1\u662f\u4e00\u79cd\u5e38\u7528\u7684\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5728\u6570\u636e\u91cf\u6709\u9650\u7684\u60c5\u51b5\u4e0b\u3002\u901a\u8fc7\u5c06\u6570\u636e\u96c6\u5212\u5206\u4e3a\u591a\u4e2a\u6298\u53e0\uff0c\u5e76\u5728\u6bcf\u4e2a\u6298\u53e0\u4e0a\u8fdb\u884c\u8bad\u7ec3\u548c\u6d4b\u8bd5\uff0c\u53ef\u4ee5\u83b7\u5f97\u6a21\u578b\u6027\u80fd\u7684\u66f4\u53ef\u9760\u4f30\u8ba1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.model_selection import cross_val_score<\/p>\n<p>scores = cross_val_score(model, X, y, cv=5)<\/p>\n<p>print(&quot;Cross-validated scores:&quot;, scores)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u4fe1\u606f\u51c6\u5219<\/strong><\/p>\n<\/p>\n<p><p>Akaike\u4fe1\u606f\u51c6\u5219\uff08AIC\uff09\u548c\u8d1d\u53f6\u65af\u4fe1\u606f\u51c6\u5219\uff08BIC\uff09\u662f\u7528\u4e8e\u6a21\u578b\u9009\u62e9\u7684\u7edf\u8ba1\u91cf\u3002\u5b83\u4eec\u8003\u8651\u4e86\u6a21\u578b\u7684\u62df\u5408\u4f18\u5ea6\u548c\u590d\u6742\u5ea6\uff0c\u8f83\u5c0f\u7684AIC\u6216BIC\u503c\u901a\u5e38\u8868\u793a\u66f4\u597d\u7684\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Statsmodels\u65f6\uff0c\u53ef\u4ee5\u76f4\u63a5\u4ece\u6a21\u578b\u7ed3\u679c\u4e2d\u83b7\u53d6AIC\u548cBIC\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(&quot;AIC:&quot;, model.aic)<\/p>\n<p>print(&quot;BIC:&quot;, model.bic)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u516d\u3001\u6570\u636e\u53ef\u89c6\u5316\u4e0e\u89e3\u91ca<\/p>\n<\/p>\n<p><p>\u6570\u636e\u53ef\u89c6\u5316\u5728\u56de\u5f52\u5206\u6790\u4e2d\u626e\u6f14\u7740\u91cd\u8981\u89d2\u8272\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u548c\u6a21\u578b\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6b8b\u5dee\u5206\u6790<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed8\u5236\u6b8b\u5dee\u56fe\uff0c\u53ef\u4ee5\u68c0\u67e5\u6a21\u578b\u7684\u5047\u8bbe\u662f\u5426\u6210\u7acb\uff0c\u5982\u7ebf\u6027\u5173\u7cfb\u3001\u540c\u65b9\u5dee\u6027\u548c\u6b63\u6001\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>plt.scatter(y_test, y_test - y_pred)<\/p>\n<p>plt.xlabel(&quot;Predicted Values&quot;)<\/p>\n<p>plt.ylabel(&quot;Residuals&quot;)<\/p>\n<p>plt.title(&quot;Residual Plot&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7279\u5f81\u91cd\u8981\u6027<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7ebf\u6027\u6a21\u578b\uff0c\u7279\u5f81\u7684\u91cd\u8981\u6027\u53ef\u4ee5\u901a\u8fc7\u7cfb\u6570\u5927\u5c0f\u6765\u8861\u91cf\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u6a21\u578b\uff0c\u5982\u968f\u673a\u68ee\u6797\u6216\u68af\u5ea6\u63d0\u5347\u51b3\u7b56\u6811\uff0c\u53ef\u4ee5\u4f7f\u7528\u7279\u5f81\u91cd\u8981\u6027\u56fe\u6765\u89e3\u91ca\u6a21\u578b\u51b3\u7b56\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">importances = model.feature_importances_<\/p>\n<p>indices = np.argsort(importances)[::-1]<\/p>\n<p>plt.figure()<\/p>\n<p>plt.title(&quot;Feature importances&quot;)<\/p>\n<p>plt.bar(range(X.shape[1]), importances[indices], align=&quot;center&quot;)<\/p>\n<p>plt.xticks(range(X.shape[1]), indices)<\/p>\n<p>plt.xlim([-1, X.shape[1]])<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u8fdb\u884c\u56de\u5f52\u62df\u5408\uff0c\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\uff0c\u5e76\u89e3\u91ca\u7ed3\u679c\u3002\u8fd9\u4e0d\u4ec5\u4ec5\u662f\u5173\u4e8e\u4ee3\u7801\u5b9e\u73b0\uff0c\u66f4\u662f\u5173\u4e8e\u7406\u89e3\u6570\u636e\u548c\u505a\u51fa\u660e\u667a\u7684\u51b3\u7b56\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u56de\u5f52\u6a21\u578b\u8fdb\u884c\u62df\u5408\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u56de\u5f52\u6a21\u578b\u9700\u8981\u8003\u8651\u6570\u636e\u7684\u7279\u6027\u548c\u76ee\u6807\u3002\u7ebf\u6027\u56de\u5f52\u9002\u7528\u4e8e\u7ebf\u6027\u5173\u7cfb\u7684\u6570\u636e\uff0c\u800c\u591a\u9879\u5f0f\u56de\u5f52\u5219\u9002\u5408\u4e8e\u975e\u7ebf\u6027\u5173\u7cfb\u3002\u901a\u8fc7\u53ef\u89c6\u5316\u6570\u636e\uff08\u5982\u6563\u70b9\u56fe\uff09\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u6570\u636e\u7684\u6a21\u5f0f\u3002\u6b64\u5916\uff0c\u4ea4\u53c9\u9a8c\u8bc1\u53ef\u4ee5\u5e2e\u52a9\u8bc4\u4f30\u6a21\u578b\u7684\u8868\u73b0\uff0c\u786e\u4fdd\u9009\u62e9\u7684\u6a21\u578b\u5728\u672a\u89c1\u6570\u636e\u4e0a\u4e5f\u80fd\u826f\u597d\u62df\u5408\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u4f7f\u7528\u54ea\u4e9b\u5e93\u6765\u8fdb\u884c\u56de\u5f52\u5206\u6790\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u4e8e\u56de\u5f52\u5206\u6790\u3002\u6700\u5e38\u7528\u7684\u662f<code>scikit-learn<\/code>\uff0c\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684API\u6765\u6784\u5efa\u548c\u8bc4\u4f30\u5404\u79cd\u56de\u5f52\u6a21\u578b\u3002<code>statsmodels<\/code>\u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u4e3a\u8be6\u7ec6\u7684\u7edf\u8ba1\u5206\u6790\u529f\u80fd\uff0c\u9002\u5408\u9700\u8981\u6df1\u5165\u7406\u89e3\u6a21\u578b\u7684\u7528\u6237\u3002\u6b64\u5916\uff0c<code>pandas<\/code>\u548c<code>numpy<\/code>\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u5904\u7406\u548c\u8ba1\u7b97\uff0c<code>matplotlib<\/code>\u548c<code>seaborn<\/code>\u5219\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\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\u6027\u80fd\u7684\u5e38\u7528\u65b9\u6cd5\u5305\u62ec\u8ba1\u7b97\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u3001\u5747\u65b9\u6839\u8bef\u5dee\uff08RMSE\uff09\u548c\u51b3\u5b9a\u7cfb\u6570\uff08R\u00b2\uff09\u3002MSE\u548cRMSE\u53ef\u4ee5\u8861\u91cf\u9884\u6d4b\u503c\u4e0e\u5b9e\u9645\u503c\u4e4b\u95f4\u7684\u5dee\u8ddd\uff0c\u503c\u8d8a\u5c0f\u8868\u793a\u6a21\u578b\u8d8a\u597d\u3002R\u00b2\u503c\u5219\u8868\u793a\u6a21\u578b\u89e3\u91ca\u7684\u65b9\u5dee\u6bd4\u4f8b\uff0c\u503c\u8d8a\u63a5\u8fd11\u8868\u660e\u6a21\u578b\u62df\u5408\u6548\u679c\u8d8a\u597d\u3002\u4ea4\u53c9\u9a8c\u8bc1\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u6027\u80fd\u8bc4\u4f30\u65b9\u6cd5\uff0c\u80fd\u591f\u63d0\u4f9b\u5bf9\u6a21\u578b\u7a33\u5065\u6027\u7684\u66f4\u5168\u9762\u7684\u4e86\u89e3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fdb\u884c\u56de\u5f52\u62df\u5408\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Scikit-Learn\u5e93\u3001Statsm [&hellip;]","protected":false},"author":3,"featured_media":1019073,"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\/1019065"}],"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=1019065"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019065\/revisions"}],"predecessor-version":[{"id":1019074,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019065\/revisions\/1019074"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1019073"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1019065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1019065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1019065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}