{"id":948867,"date":"2024-12-27T00:07:38","date_gmt":"2024-12-26T16:07:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/948867.html"},"modified":"2024-12-27T00:07:40","modified_gmt":"2024-12-26T16:07:40","slug":"aic-bic-%e5%a6%82%e4%bd%95python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/948867.html","title":{"rendered":"AIC BIC \u5982\u4f55python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25084038\/95037ba5-db6a-468a-9645-c9548f7f58c0.webp\" alt=\"AIC BIC \u5982\u4f55python\" \/><\/p>\n<p><p> <strong><a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>C\uff08Akaike Information Criterion\uff09\u548cBIC\uff08Bayesian Information Criterion\uff09\u662f\u5728\u6a21\u578b\u9009\u62e9\u4e2d\u5e38\u7528\u7684\u51c6\u5219\uff0c\u7528\u4e8e\u8bc4\u4f30\u7edf\u8ba1\u6a21\u578b\u7684\u62df\u5408\u4f18\u5ea6\u548c\u590d\u6742\u5ea6\u3002AIC\u503e\u5411\u4e8e\u9009\u62e9\u66f4\u590d\u6742\u7684\u6a21\u578b\uff0cBIC\u5219\u66f4\u504f\u5411\u9009\u62e9\u66f4\u7b80\u5355\u7684\u6a21\u578b\u3001AIC\u548cBIC\u90fd\u4f9d\u8d56\u4e8e\u6700\u5927\u4f3c\u7136\u4f30\u8ba1\uff0c\u4f46BIC\u5bf9\u6837\u672c\u91cf\u66f4\u4e3a\u654f\u611f\u3002<\/strong>AIC\u7684\u4e3b\u8981\u4f18\u70b9\u5728\u4e8e\u5176\u7075\u6d3b\u6027\uff0c\u5b83\u80fd\u591f\u5904\u7406\u4e0d\u540c\u590d\u6742\u5ea6\u7684\u6a21\u578b\uff0c\u4ece\u800c\u5e2e\u52a9\u8bc6\u522b\u6700\u4f73\u6a21\u578b\u3002\u53e6\u4e00\u65b9\u9762\uff0cBIC\u7684\u4f18\u70b9\u5728\u4e8e\u5176\u5bf9\u6a21\u578b\u590d\u6742\u5ea6\u7684\u60e9\u7f5a\u529b\u5ea6\u66f4\u5927\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u5927\u6837\u672c\u60c5\u5883\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528AIC\u548cBIC\u8fdb\u884c\u6a21\u578b\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001AIC\u548cBIC\u7684\u57fa\u672c\u6982\u5ff5<\/p>\n<\/p>\n<p><p>AIC\u548cBIC\u662f\u4e24\u79cd\u4fe1\u606f\u51c6\u5219\uff0c\u7528\u4e8e\u6a21\u578b\u9009\u62e9\u548c\u8bc4\u4ef7\u3002AIC\u7531\u8d64\u6c60\u4fe1\u606f\u51c6\u5219\u5f97\u540d\uff0c\u65e8\u5728\u5bfb\u627e\u4e00\u4e2a\u5728\u89e3\u91ca\u6570\u636e\u65f6\u635f\u5931\u4fe1\u606f\u6700\u5c11\u7684\u6a21\u578b\u3002BIC\uff0c\u5373\u8d1d\u53f6\u65af\u4fe1\u606f\u51c6\u5219\uff0c\u5219\u662f\u5728AIC\u7684\u57fa\u7840\u4e0a\u8003\u8651\u4e86\u6837\u672c\u91cf\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>AIC\u7684\u5b9a\u4e49<\/strong>\uff1aAIC = 2k &#8211; 2ln(L)\uff0c\u5176\u4e2dk\u662f\u6a21\u578b\u53c2\u6570\u7684\u6570\u91cf\uff0cL\u662f\u6700\u5927\u4f3c\u7136\u4f30\u8ba1\u3002<\/li>\n<li><strong>BIC\u7684\u5b9a\u4e49<\/strong>\uff1aBIC = ln(n)k &#8211; 2ln(L)\uff0c\u5176\u4e2dn\u662f\u6837\u672c\u91cf\u3002<\/li>\n<\/ul>\n<p><p>AIC\u504f\u5411\u4e8e\u9009\u62e9\u5177\u6709\u66f4\u591a\u53c2\u6570\u7684\u6a21\u578b\uff0c\u800cBIC\u5219\u66f4\u4e25\u683c\u5730\u60e9\u7f5a\u6a21\u578b\u7684\u590d\u6742\u5ea6\u3002\u56e0\u6b64\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0cBIC\u4f1a\u504f\u5411\u4e8e\u9009\u62e9\u66f4\u7b80\u5355\u7684\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001Python\u4e2d\u8ba1\u7b97AIC\u548cBIC<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5404\u79cd\u7edf\u8ba1\u5e93\u6765\u8ba1\u7b97\u6a21\u578b\u7684AIC\u548cBIC\u3002\u6700\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>statsmodels<\/code>\u548c<code>scikit-learn<\/code>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u8ba1\u7b97AIC\u548cBIC\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Statsmodels\u8ba1\u7b97AIC\u548cBIC<\/h3>\n<\/p>\n<p><p><code>statsmodels<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u4e13\u7528\u4e8e\u4f30\u8ba1\u7edf\u8ba1\u6a21\u578b\u548c\u8fdb\u884c\u7edf\u8ba1\u6d4b\u8bd5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>statsmodels<\/code>\u8ba1\u7b97AIC\u548cBIC\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Statsmodels<\/strong><\/li>\n<\/ol>\n<p><p>\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u5b89\u88c5<code>statsmodels<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install statsmodels<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u62df\u5408\u6a21\u578b<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>statsmodels<\/code>\u4e2d\u7684\u6a21\u578b\u62df\u5408\u529f\u80fd\u6765\u62df\u5408\u4f60\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import statsmodels.api as sm<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>np.random.seed(0)<\/p>\n<p>X = np.random.rand(100, 1)<\/p>\n<p>y = 3 * X.squeeze() + 2 + np.random.randn(100) * 0.5<\/p>\n<h2><strong>\u6dfb\u52a0\u5e38\u6570\u9879\uff08\u622a\u8ddd\uff09<\/strong><\/h2>\n<p>X = sm.add_constant(X)<\/p>\n<h2><strong>\u62df\u5408\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = sm.OLS(y, X).fit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u8ba1\u7b97AIC\u548cBIC<\/strong><\/li>\n<\/ol>\n<p><p>\u4e00\u65e6\u6a21\u578b\u88ab\u62df\u5408\uff0c\u5c31\u53ef\u4ee5\u8f7b\u677e\u8ba1\u7b97AIC\u548cBIC\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">aic = model.aic<\/p>\n<p>bic = model.bic<\/p>\n<p>print(f&quot;AIC: {aic}&quot;)<\/p>\n<p>print(f&quot;BIC: {bic}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528Scikit-learn\u8ba1\u7b97AIC\u548cBIC<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>scikit-learn<\/code>\u5e76\u6ca1\u6709\u76f4\u63a5\u8ba1\u7b97AIC\u548cBIC\u7684\u51fd\u6570\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u6700\u5927\u4f3c\u7136\u4f30\u8ba1\u7684\u7ed3\u679c\u624b\u52a8\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5Scikit-learn<\/strong><\/li>\n<\/ol>\n<p><p>\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u4e2d\u5df2\u5b89\u88c5<code>scikit-learn<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install scikit-learn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u62df\u5408\u6a21\u578b<\/strong><\/li>\n<\/ol>\n<p><p>\u4f7f\u7528<code>scikit-learn<\/code>\u62df\u5408\u7ebf\u6027\u56de\u5f52\u6a21\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import LinearRegression<\/p>\n<p>from sklearn.metrics import mean_squared_error<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>np.random.seed(0)<\/p>\n<p>X = np.random.rand(100, 1)<\/p>\n<p>y = 3 * X.squeeze() + 2 + np.random.randn(100) * 0.5<\/p>\n<h2><strong>\u62df\u5408\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression().fit(X, y)<\/p>\n<h2><strong>\u9884\u6d4b\u7ed3\u679c<\/strong><\/h2>\n<p>y_pred = model.predict(X)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u624b\u52a8\u8ba1\u7b97AIC\u548cBIC<\/strong><\/li>\n<\/ol>\n<p><p>\u6839\u636e\u5747\u65b9\u8bef\u5dee\u548c\u6a21\u578b\u53c2\u6570\u624b\u52a8\u8ba1\u7b97AIC\u548cBIC\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">n = len(y)<\/p>\n<p>k = X.shape[1] + 1  # \u53c2\u6570\u6570\u91cf\uff08\u5305\u62ec\u622a\u8ddd\uff09<\/p>\n<p>mse = mean_squared_error(y, y_pred)<\/p>\n<p>log_likelihood = -n\/2 * np.log(2 * np.pi * mse) - n\/2<\/p>\n<p>aic = 2 * k - 2 * log_likelihood<\/p>\n<p>bic = np.log(n) * k - 2 * log_likelihood<\/p>\n<p>print(f&quot;AIC: {aic}&quot;)<\/p>\n<p>print(f&quot;BIC: {bic}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001AIC\u548cBIC\u5728\u6a21\u578b\u9009\u62e9\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>AIC\u548cBIC\u7684\u5e94\u7528\u4e0d\u4ec5\u9650\u4e8e\u7ebf\u6027\u56de\u5f52\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7edf\u8ba1\u6a21\u578b\uff0c\u5982\u5e7f\u4e49\u7ebf\u6027\u6a21\u578b\u3001\u65f6\u95f4\u5e8f\u5217\u6a21\u578b\u7b49\u3002\u5173\u952e\u5728\u4e8e\u9009\u62e9\u80fd\u591f\u6700\u597d\u5730\u5e73\u8861\u62df\u5408\u4f18\u5ea6\u548c\u6a21\u578b\u590d\u6742\u5ea6\u7684\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cAIC\u548cBIC\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u9009\u62e9\u6700\u9002\u5408\u7684\u6570\u636e\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u65f6\u95f4\u5e8f\u5217\u9884\u6d4b<\/strong>\uff1a\u9009\u62e9\u5408\u9002\u7684ARIMA\u6a21\u578b\u6216\u5176\u4ed6\u65f6\u95f4\u5e8f\u5217\u6a21\u578b\u3002<\/li>\n<li><strong>\u56de\u5f52\u5206\u6790<\/strong>\uff1a\u9009\u62e9\u5305\u542b\u4e0d\u540c\u9884\u6d4b\u53d8\u91cf\u7684\u7ebf\u6027\u6216\u975e\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u3002<\/li>\n<li><strong>\u5206\u7c7b\u95ee\u9898<\/strong>\uff1a\u8bc4\u4f30\u903b\u8f91\u56de\u5f52\u6a21\u578b\u6216\u5176\u4ed6\u5206\u7c7b\u6a21\u578b\u7684\u9002\u5408\u6027\u3002<\/li>\n<\/ul>\n<p><h3>\u591a\u6a21\u578b\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u4e2a\u6a21\u578b\u4e4b\u95f4\u8fdb\u884c\u6bd4\u8f83\u65f6\uff0c\u901a\u5e38\u9009\u62e9\u5177\u6709\u6700\u4f4eAIC\u548cBIC\u503c\u7684\u6a21\u578b\u3002\u7136\u800c\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cAIC\u548cBIC\u7684\u9009\u62e9\u6807\u51c6\u53ef\u80fd\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u7279\u522b\u662f\u5728\u6837\u672c\u91cf\u8f83\u5c0f\u65f6\u3002\u56e0\u6b64\uff0c\u5efa\u8bae\u7ed3\u5408\u5176\u4ed6\u6a21\u578b\u8bc4\u4f30\u6307\u6807\uff08\u5982\u4ea4\u53c9\u9a8c\u8bc1\uff09\u8fdb\u884c\u7efc\u5408\u5224\u65ad\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001AIC\u548cBIC\u7684\u4f18\u7f3a\u70b9<\/p>\n<\/p>\n<p><h3>AIC\u7684\u4f18\u70b9\u548c\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u80fd\u591f\u5904\u7406\u4e0d\u540c\u590d\u6742\u5ea6\u7684\u6a21\u578b\u3002<\/li>\n<li><strong>\u6613\u4e8e\u8ba1\u7b97<\/strong>\uff1a\u57fa\u4e8e\u6700\u5927\u4f3c\u7136\u4f30\u8ba1\uff0c\u8ba1\u7b97\u7b80\u5355\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u503e\u5411\u4e8e\u590d\u6742\u6a21\u578b<\/strong>\uff1a\u53ef\u80fd\u5bfc\u81f4\u8fc7\u62df\u5408\uff0c\u5c24\u5176\u5728\u6837\u672c\u91cf\u8f83\u5c0f\u65f6\u3002<\/li>\n<li><strong>\u4e0d\u9002\u5408\u5c0f\u6837\u672c<\/strong>\uff1a\u5728\u5c0f\u6837\u672c\u60c5\u5883\u4e0b\uff0cAIC\u53ef\u80fd\u4f1a\u9009\u62e9\u8fc7\u4e8e\u590d\u6742\u7684\u6a21\u578b\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><h3>BIC\u7684\u4f18\u70b9\u548c\u7f3a\u70b9<\/h3>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u5f3a\u60e9\u7f5a\u4f5c\u7528<\/strong>\uff1a\u5bf9\u6a21\u578b\u590d\u6742\u5ea6\u60e9\u7f5a\u66f4\u5927\uff0c\u9632\u6b62\u8fc7\u62df\u5408\u3002<\/li>\n<li><strong>\u9002\u5408\u5927\u6837\u672c<\/strong>\uff1a\u5728\u5927\u6837\u672c\u60c5\u5883\u4e0b\u8868\u73b0\u826f\u597d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u504f\u5411\u7b80\u5355\u6a21\u578b<\/strong>\uff1a\u53ef\u80fd\u5bfc\u81f4\u6b20\u62df\u5408\uff0c\u7279\u522b\u5728\u6837\u672c\u91cf\u8f83\u5c0f\u65f6\u3002<\/li>\n<li><strong>\u5bf9\u5148\u9a8c\u4fe1\u606f\u654f\u611f<\/strong>\uff1aBIC\u57fa\u4e8e\u8d1d\u53f6\u65af\u6846\u67b6\uff0c\u53ef\u80fd\u53d7\u5230\u5148\u9a8c\u9009\u62e9\u7684\u5f71\u54cd\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><p>\u4e94\u3001AIC\u548cBIC\u7684\u5b9e\u9645\u6848\u4f8b<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3AIC\u548cBIC\u7684\u5b9e\u9645\u5e94\u7528\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u5177\u4f53\u7684\u6848\u4f8b\u3002<\/p>\n<\/p>\n<p><h3>\u6848\u4f8b\uff1a\u9009\u62e9\u6700\u4f73\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6570\u636e\u96c6\uff0c\u5176\u4e2d\u5305\u542b\u591a\u4e2a\u7279\u5f81\uff0c\u6211\u4eec\u5e0c\u671b\u901a\u8fc7\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u9884\u6d4b\u76ee\u6807\u53d8\u91cf\u3002\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u591a\u4e2a\u6a21\u578b\uff0c\u5206\u522b\u5305\u542b\u4e0d\u540c\u7684\u7279\u5f81\u7ec4\u5408\uff0c\u5e76\u4f7f\u7528AIC\u548cBIC\u6765\u9009\u62e9\u6700\u4f73\u6a21\u578b\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u51c6\u5907<\/strong><\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u51c6\u5907\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u96c6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>np.random.seed(0)<\/p>\n<p>data = pd.DataFrame({<\/p>\n<p>    &#39;feature1&#39;: np.random.rand(100),<\/p>\n<p>    &#39;feature2&#39;: np.random.rand(100),<\/p>\n<p>    &#39;feature3&#39;: np.random.rand(100),<\/p>\n<p>    &#39;target&#39;: np.random.rand(100)<\/p>\n<p>})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6784\u5efa\u591a\u4e2a\u6a21\u578b<\/strong><\/li>\n<\/ol>\n<p><p>\u6211\u4eec\u6784\u5efa\u591a\u4e2a\u7ebf\u6027\u56de\u5f52\u6a21\u578b\uff0c\u5206\u522b\u5305\u542b\u4e0d\u540c\u7684\u7279\u5f81\u7ec4\u5408\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import LinearRegression<\/p>\n<p>from sklearn.metrics import mean_squared_error<\/p>\n<h2><strong>\u5b9a\u4e49\u7279\u5f81\u7ec4\u5408<\/strong><\/h2>\n<p>feature_sets = [<\/p>\n<p>    [&#39;feature1&#39;],<\/p>\n<p>    [&#39;feature1&#39;, &#39;feature2&#39;],<\/p>\n<p>    [&#39;feature1&#39;, &#39;feature2&#39;, &#39;feature3&#39;]<\/p>\n<p>]<\/p>\n<h2><strong>\u5b58\u50a8\u6a21\u578b\u7684AIC\u548cBIC\u503c<\/strong><\/h2>\n<p>results = []<\/p>\n<p>for features in feature_sets:<\/p>\n<p>    X = data[features]<\/p>\n<p>    y = data[&#39;target&#39;]<\/p>\n<p>    # \u62df\u5408\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/p>\n<p>    model = LinearRegression().fit(X, y)<\/p>\n<p>    y_pred = model.predict(X)<\/p>\n<p>    # \u8ba1\u7b97AIC\u548cBIC<\/p>\n<p>    n = len(y)<\/p>\n<p>    k = X.shape[1] + 1  # \u53c2\u6570\u6570\u91cf\uff08\u5305\u62ec\u622a\u8ddd\uff09<\/p>\n<p>    mse = mean_squared_error(y, y_pred)<\/p>\n<p>    log_likelihood = -n\/2 * np.log(2 * np.pi * mse) - n\/2<\/p>\n<p>    aic = 2 * k - 2 * log_likelihood<\/p>\n<p>    bic = np.log(n) * k - 2 * log_likelihood<\/p>\n<p>    results.append({&#39;features&#39;: features, &#39;AIC&#39;: aic, &#39;BIC&#39;: bic})<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>for result in results:<\/p>\n<p>    print(f&quot;Features: {result[&#39;features&#39;]}, AIC: {result[&#39;AIC&#39;]}, BIC: {result[&#39;BIC&#39;]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u9009\u62e9\u6700\u4f73\u6a21\u578b<\/strong><\/li>\n<\/ol>\n<p><p>\u6839\u636eAIC\u548cBIC\u503c\uff0c\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u6700\u4f73\u7684\u7279\u5f81\u7ec4\u5408\u3002\u901a\u5e38\u9009\u62e9AIC\u548cBIC\u503c\u8f83\u5c0f\u7684\u6a21\u578b\u3002\u7136\u800c\uff0c\u5e94\u8be5\u7ed3\u5408\u5176\u4ed6\u8bc4\u4ef7\u6307\u6807\u548c\u5b9e\u9645\u9700\u6c42\u505a\u51fa\u51b3\u7b56\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u7ed3\u8bba<\/p>\n<\/p>\n<p><p>AIC\u548cBIC\u662f\u5f3a\u5927\u7684\u6a21\u578b\u9009\u62e9\u5de5\u5177\uff0c\u5728\u7edf\u8ba1\u5efa\u6a21\u4e2d\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u4fe1\u606f\u51c6\u5219\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5e73\u8861\u6a21\u578b\u7684\u62df\u5408\u4f18\u5ea6\u548c\u590d\u6742\u5ea6\uff0c\u4ece\u800c\u9009\u62e9\u6700\u9002\u5408\u7684\u6570\u636e\u6a21\u578b\u3002\u7136\u800c\uff0c\u5728\u5e94\u7528\u8fd9\u4e9b\u6807\u51c6\u65f6\uff0c\u9700\u7ed3\u5408\u5176\u4ed6\u8bc4\u4ef7\u6307\u6807\u548c\u5b9e\u9645\u4e1a\u52a1\u9700\u6c42\uff0c\u4ee5\u786e\u4fdd\u9009\u62e9\u7684\u6a21\u578b\u5177\u6709\u826f\u597d\u7684\u9884\u6d4b\u6027\u80fd\u548c\u89e3\u91ca\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fAIC\u548cBIC\uff0c\u5b83\u4eec\u7684\u4f5c\u7528\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>AIC\uff08\u8d64\u6c60\u4fe1\u606f\u91cf\u51c6\u5219\uff09\u548cBIC\uff08\u8d1d\u53f6\u65af\u4fe1\u606f\u91cf\u51c6\u5219\uff09\u662f\u7528\u4e8e\u6a21\u578b\u9009\u62e9\u7684\u7edf\u8ba1\u6307\u6807\u3002\u5b83\u4eec\u53ef\u4ee5\u5e2e\u52a9\u8bc4\u4f30\u4e0d\u540c\u6a21\u578b\u7684\u4f18\u52a3\uff0c\u5c24\u5176\u662f\u5728\u6bd4\u8f83\u5177\u6709\u4e0d\u540c\u590d\u6742\u5ea6\u7684\u6a21\u578b\u65f6\u3002AIC\u65e8\u5728\u6700\u5c0f\u5316\u4fe1\u606f\u635f\u5931\uff0c\u800cBIC\u5219\u66f4\u504f\u5411\u4e8e\u60e9\u7f5a\u590d\u6742\u6a21\u578b\u3002\u4f7f\u7528\u8fd9\u4e24\u4e2a\u6307\u6807\u53ef\u4ee5\u5e2e\u52a9\u4f60\u9009\u62e9\u9002\u5408\u6570\u636e\u7684\u6700\u4f73\u6a21\u578b\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8ba1\u7b97AIC\u548cBIC\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0cAIC\u548cBIC\u53ef\u4ee5\u901a\u8fc7statsmodels\u5e93\u8f7b\u677e\u8ba1\u7b97\u3002\u9996\u5148\uff0c\u4f7f\u7528statsmodels\u6784\u5efa\u56de\u5f52\u6a21\u578b\uff0c\u7136\u540e\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528<code>model.aic<\/code>\u548c<code>model.bic<\/code>\u5c5e\u6027\u83b7\u53d6\u76f8\u5e94\u7684\u503c\u3002\u6b64\u5916\uff0c\u4e00\u4e9b\u5176\u4ed6\u5e93\u5982scikit-learn\u4e5f\u63d0\u4f9b\u4e86\u8ba1\u7b97AIC\u548cBIC\u7684\u529f\u80fd\uff0c\u901a\u5e38\u9700\u8981\u624b\u52a8\u5b9e\u73b0\u8fd9\u4e9b\u516c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u4ee5\u964d\u4f4eAIC\u548cBIC\u503c\uff1f<\/strong><br \/>\u964d\u4f4eAIC\u548cBIC\u503c\u901a\u5e38\u610f\u5473\u7740\u9009\u62e9\u4e00\u4e2a\u80fd\u591f\u89e3\u91ca\u6570\u636e\u540c\u65f6\u53c8\u4e0d\u81f3\u4e8e\u8fc7\u4e8e\u590d\u6742\u7684\u6a21\u578b\u3002\u53ef\u4ee5\u901a\u8fc7\u5c1d\u8bd5\u4e0d\u540c\u7684\u6a21\u578b\uff0c\u5305\u62ec\u7ebf\u6027\u56de\u5f52\u3001\u51b3\u7b56\u6811\u7b49\uff0c\u6765\u6bd4\u8f83\u5b83\u4eec\u7684AIC\u548cBIC\u503c\u3002\u4f7f\u7528\u4ea4\u53c9\u9a8c\u8bc1\u7b49\u65b9\u6cd5\u53ef\u4ee5\u8fdb\u4e00\u6b65\u9a8c\u8bc1\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\uff0c\u4ee5\u786e\u4fdd\u6240\u9009\u6a21\u578b\u5728\u65b0\u6570\u636e\u4e0a\u7684\u8868\u73b0\u4e5f\u826f\u597d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"AIC\uff08Akaike Information Criterion\uff09\u548cBIC\uff08Bayesian Informat [&hellip;]","protected":false},"author":3,"featured_media":948877,"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\/948867"}],"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=948867"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948867\/revisions"}],"predecessor-version":[{"id":948878,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/948867\/revisions\/948878"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/948877"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=948867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=948867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=948867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}