{"id":937697,"date":"2024-12-26T19:57:08","date_gmt":"2024-12-26T11:57:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/937697.html"},"modified":"2024-12-26T19:57:10","modified_gmt":"2024-12-26T11:57:10","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97vif","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/937697.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97vif"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073436\/a9c88c9d-7aef-4a32-8f4a-88f8949fa46e.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97vif\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8ba1\u7b97VIF\uff08\u65b9\u5dee\u81a8\u80c0\u56e0\u5b50\uff09\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528statsmodels\u5e93\u3001\u7406\u89e3VIF\u7684\u8ba1\u7b97\u539f\u7406\u3001\u9009\u62e9\u6b63\u786e\u7684\u7279\u5f81\u6765\u51cf\u5c11\u591a\u91cd\u5171\u7ebf\u6027\u3002<\/strong>VIF\u662f\u7528\u4e8e\u68c0\u6d4b\u591a\u91cd\u5171\u7ebf\u6027\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u56de\u5f52\u5206\u6790\u4e2d\u7684R\u00b2\u503c\u8fdb\u884c\u8ba1\u7b97\u3002\u8ba1\u7b97VIF\u7684\u5173\u952e\u5728\u4e8e\u7406\u89e3\u6bcf\u4e2a\u72ec\u7acb\u53d8\u91cf\u4e0e\u5176\u4ed6\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u786e\u4fdd\u6a21\u578b\u7684\u7a33\u5b9a\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c<strong>\u4f7f\u7528Python\u4e2d\u7684statsmodels\u5e93\u8ba1\u7b97VIF<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u3002statsmodels\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u800c\u6709\u6548\u7684\u51fd\u6570\u6765\u8ba1\u7b97\u6bcf\u4e2a\u53d8\u91cf\u7684VIF\u503c\u3002\u901a\u8fc7\u5bf9\u6bcf\u4e2a\u72ec\u7acb\u53d8\u91cf\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\uff0c\u5e76\u8ba1\u7b97\u5176R\u00b2\u503c\uff0c\u53ef\u4ee5\u5f97\u51fa\u8be5\u53d8\u91cf\u7684VIF\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/strong>\uff1a\u5728\u5f00\u59cb\u8ba1\u7b97VIF\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5bfc\u5165\u4e86pandas\u3001numpy\u548cstatsmodels\u7b49\u5e93\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u7684\u57fa\u7840\u5de5\u5177\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u51c6\u5907\u6570\u636e<\/strong>\uff1a\u786e\u4fdd\u6570\u636e\u5df2\u7ecf\u88ab\u52a0\u8f7d\u5230\u4e00\u4e2apandas DataFrame\u4e2d\uff0c\u6570\u636e\u5e94\u5305\u62ec\u6240\u6709\u7528\u4e8e\u56de\u5f52\u5206\u6790\u7684\u72ec\u7acb\u53d8\u91cf\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8ba1\u7b97VIF<\/strong>\uff1a\u901a\u8fc7\u5faa\u73af\u904d\u5386DataFrame\u4e2d\u7684\u6bcf\u4e2a\u53d8\u91cf\uff0c\u4f7f\u7528statsmodels\u4e2d\u7684OLS\u51fd\u6570\u8ba1\u7b97\u6bcf\u4e2a\u53d8\u91cf\u7684R\u00b2\u503c\uff0c\u4ece\u800c\u5f97\u51fa\u8be5\u53d8\u91cf\u7684VIF\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u89e3\u91ca\u7ed3\u679c<\/strong>\uff1a\u901a\u5e38\u60c5\u51b5\u4e0b\uff0cVIF\u503c\u5927\u4e8e10\u88ab\u89c6\u4e3a\u591a\u91cd\u5171\u7ebf\u6027\u95ee\u9898\u7684\u8b66\u544a\u4fe1\u53f7\uff0c\u5c3d\u7ba1\u5728\u4e0d\u540c\u7684\u7814\u7a76\u9886\u57df\u53ef\u80fd\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0VIF\u7684\u8ba1\u7b97\uff0c\u5e76\u89e3\u91ca\u6bcf\u4e00\u6b65\u7684\u5177\u4f53\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001VIF\u7684\u57fa\u672c\u6982\u5ff5\u4e0e\u91cd\u8981\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u6df1\u5165\u4e86\u89e3\u8ba1\u7b97\u8fc7\u7a0b\u4e4b\u524d\uff0c\u4e86\u89e3VIF\u7684\u57fa\u672c\u6982\u5ff5\u662f\u5341\u5206\u91cd\u8981\u7684\u3002VIF\u662f\u4e00\u4e2a\u7528\u4e8e\u68c0\u6d4b\u591a\u91cd\u5171\u7ebf\u6027\uff08\u5f53\u4e24\u4e2a\u6216\u591a\u4e2a\u72ec\u7acb\u53d8\u91cf\u5728\u56de\u5f52\u6a21\u578b\u4e2d\u9ad8\u5ea6\u76f8\u5173\u65f6\u7684\u73b0\u8c61\uff09\u7684\u7edf\u8ba1\u91cf\u3002\u591a\u91cd\u5171\u7ebf\u6027\u53ef\u80fd\u5bfc\u81f4\u4f30\u8ba1\u7cfb\u6570\u4e0d\u7a33\u5b9a\uff0c\u5f71\u54cd\u6a21\u578b\u9884\u6d4b\u7684\u51c6\u786e\u6027\u548c\u89e3\u91ca\u6027\u3002<\/p>\n<\/p>\n<p><h4>1. \u4ec0\u4e48\u662fVIF\uff1f<\/h4>\n<\/p>\n<p><p>VIF\u7684\u5168\u79f0\u662fVariance Inflation Factor\uff0c\u5373\u65b9\u5dee\u81a8\u80c0\u56e0\u5b50\u3002\u5b83\u91cf\u5316\u4e86\u4e00\u4e2a\u81ea\u53d8\u91cf\u5728\u56de\u5f52\u6a21\u578b\u4e2d\u88ab\u5176\u4ed6\u81ea\u53d8\u91cf\u89e3\u91ca\u7684\u7a0b\u5ea6\u3002\u5177\u4f53\u5730\u8bf4\uff0cVIF\u503c\u8d8a\u5927\uff0c\u8bf4\u660e\u8be5\u81ea\u53d8\u91cf\u4e0e\u5176\u4ed6\u81ea\u53d8\u91cf\u7684\u7ebf\u6027\u5173\u7cfb\u8d8a\u5f3a\uff0c\u5bfc\u81f4\u5176\u56de\u5f52\u7cfb\u6570\u7684\u6807\u51c6\u8bef\u5dee\u88ab\u653e\u5927\u3002<\/p>\n<\/p>\n<p><h4>2. VIF\u7684\u8ba1\u7b97\u516c\u5f0f<\/h4>\n<\/p>\n<p><p>VIF\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ \\text{VIF} = \\frac{1}{1-R^2} ]<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c( R^2 )\u662f\u5c06\u67d0\u4e2a\u81ea\u53d8\u91cf\u4f5c\u4e3a\u56e0\u53d8\u91cf\uff0c\u5176\u4ed6\u81ea\u53d8\u91cf\u4f5c\u4e3a\u81ea\u53d8\u91cf\u8fdb\u884c\u56de\u5f52\u5206\u6790\u65f6\u7684\u51b3\u5b9a\u7cfb\u6570\u3002\u9ad8VIF\u503c\u610f\u5473\u7740\u8be5\u81ea\u53d8\u91cf\u4e0e\u5176\u4ed6\u81ea\u53d8\u91cf\u9ad8\u5ea6\u76f8\u5173\u3002<\/p>\n<\/p>\n<p><h4>3. \u4e3a\u4ec0\u4e48VIF\u91cd\u8981\uff1f<\/h4>\n<\/p>\n<p><p>\u9ad8VIF\u503c\u8868\u660e\u591a\u91cd\u5171\u7ebf\u6027\u95ee\u9898\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u56de\u5f52\u7cfb\u6570\u7684\u4e0d\u7a33\u5b9a\u6027\uff0c\u5f71\u54cd\u6a21\u578b\u7684\u89e3\u91ca\u6027\u3002<\/li>\n<li>\u5197\u4f59\u53d8\u91cf\u7684\u5b58\u5728\uff0c\u4f7f\u6a21\u578b\u590d\u6742\u4e14\u96be\u4ee5\u89e3\u91ca\u3002<\/li>\n<li>\u5f71\u54cd\u9884\u6d4b\u7684\u51c6\u786e\u6027\u3002<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Python\u8ba1\u7b97VIF<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8ba1\u7b97VIF\u901a\u5e38\u4f7f\u7528statsmodels\u5e93\uff0c\u8be5\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7edf\u8ba1\u5efa\u6a21\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u8ba1\u7b97VIF\u7684\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h4>1. \u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u8ba1\u7b97\u4e4b\u524d\uff0c\u786e\u4fdd\u5bfc\u5165\u4e86pandas\u3001numpy\u548cstatsmodels\u7b49\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import numpy as np<\/p>\n<p>from statsmodels.stats.outliers_influence import variance_inflation_factor<\/p>\n<p>from statsmodels.tools.tools import add_constant<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u51c6\u5907<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u81ea\u53d8\u91cf\u7684\u6570\u636e\u96c6\uff0c\u901a\u5e38\u88ab\u5b58\u50a8\u5728\u4e00\u4e2apandas DataFrame\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e\u96c6<\/p>\n<p>data = {<\/p>\n<p>    &#39;X1&#39;: [2.3, 3.4, 4.1, 5.2, 6.3],<\/p>\n<p>    &#39;X2&#39;: [3.2, 4.1, 5.7, 6.8, 7.9],<\/p>\n<p>    &#39;X3&#39;: [4.5, 5.2, 6.1, 7.3, 8.4]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u8ba1\u7b97VIF<\/h4>\n<\/p>\n<p><p>\u8ba1\u7b97VIF\u65f6\uff0c\u9996\u5148\u9700\u8981\u5728\u6570\u636e\u96c6\u4e2d\u6dfb\u52a0\u5e38\u6570\u9879\uff08Intercept\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6dfb\u52a0\u5e38\u6570\u9879<\/p>\n<p>X = add_constant(df)<\/p>\n<h2><strong>\u8ba1\u7b97VIF<\/strong><\/h2>\n<p>vif_data = pd.DataFrame()<\/p>\n<p>vif_data[&quot;Feature&quot;] = X.columns<\/p>\n<p>vif_data[&quot;VIF&quot;] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])]<\/p>\n<p>print(vif_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u89e3\u91caVIF\u7ed3\u679c<\/h4>\n<\/p>\n<p><p>\u5728\u5f97\u5230VIF\u503c\u540e\uff0c\u5bf9\u7ed3\u679c\u8fdb\u884c\u89e3\u91ca\u662f\u5173\u952e\u7684\u4e00\u6b65\uff1a<\/p>\n<\/p>\n<ul>\n<li>VIF\u503c\u5c0f\u4e8e5\uff1a\u8868\u660e\u591a\u91cd\u5171\u7ebf\u6027\u4e0d\u4e25\u91cd\u3002<\/li>\n<li>VIF\u503c\u57285\u523010\u4e4b\u95f4\uff1a\u8868\u660e\u5b58\u5728\u4e2d\u5ea6\u591a\u91cd\u5171\u7ebf\u6027\u3002<\/li>\n<li>VIF\u503c\u5927\u4e8e10\uff1a\u8b66\u544a\u4fe1\u53f7\uff0c\u63d0\u793a\u6a21\u578b\u53ef\u80fd\u5b58\u5728\u4e25\u91cd\u7684\u591a\u91cd\u5171\u7ebf\u6027\u95ee\u9898\u3002<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u89e3\u51b3\u591a\u91cd\u5171\u7ebf\u6027\u95ee\u9898\u7684\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u68c0\u6d4b\u5230\u9ad8VIF\u503c\u540e\uff0c\u91c7\u53d6\u9002\u5f53\u7684\u63aa\u65bd\u662f\u5fc5\u8981\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1. \u79fb\u9664\u9ad8VIF\u503c\u7684\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u76f4\u63a5\u79fb\u9664\u9ad8VIF\u503c\u7684\u53d8\u91cf\u662f\u89e3\u51b3\u591a\u91cd\u5171\u7ebf\u6027\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u53ef\u80fd\u5bfc\u81f4\u4fe1\u606f\u4e22\u5931\uff0c\u56e0\u6b64\u9700\u8981\u8c28\u614e\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h4>2. \u5408\u5e76\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5f53\u591a\u4e2a\u53d8\u91cf\u9ad8\u5ea6\u76f8\u5173\u65f6\uff0c\u8003\u8651\u5408\u5e76\u8fd9\u4e9b\u53d8\u91cf\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf\uff0c\u4f5c\u4e3a\u8fd9\u4e9b\u53d8\u91cf\u7684\u5e73\u5747\u503c\u6216\u4e3b\u6210\u5206\u3002<\/p>\n<\/p>\n<p><h4>3. \u6b63\u5219\u5316\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5e94\u7528\u5982Lasso\u548cRidge\u56de\u5f52\u7b49\u6b63\u5219\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u51cf\u5c11\u591a\u91cd\u5171\u7ebf\u6027\u5bf9\u6a21\u578b\u7684\u5f71\u54cd\u3002\u8fd9\u4e9b\u65b9\u6cd5\u901a\u8fc7\u6dfb\u52a0\u60e9\u7f5a\u9879\u6765\u9650\u5236\u53d8\u91cf\u7cfb\u6570\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><h4>4. \u589e\u52a0\u6837\u672c\u91cf<\/h4>\n<\/p>\n<p><p>\u589e\u52a0\u6837\u672c\u91cf\u53ef\u80fd\u6709\u52a9\u4e8e\u51cf\u5c11\u591a\u91cd\u5171\u7ebf\u6027\u5e26\u6765\u7684\u4e0d\u5229\u5f71\u54cd\uff0c\u5c24\u5176\u662f\u5728\u6837\u672c\u91cf\u8f83\u5c0f\u7684\u60c5\u51b5\u4e0b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9664\u4e86\u8ba1\u7b97\u548c\u89e3\u51b3\u591a\u91cd\u5171\u7ebf\u6027\u95ee\u9898\u5916\uff0c\u8fd8\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<p><h4>1. \u6570\u636e\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97VIF\u4e4b\u524d\uff0c\u786e\u4fdd\u6570\u636e\u5df2\u7ecf\u8fc7\u6e05\u6d17\u548c\u9884\u5904\u7406\uff0c\u5305\u62ec\u5904\u7406\u7f3a\u5931\u503c\u3001\u5f02\u5e38\u503c\u548c\u6807\u51c6\u5316\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2. \u7279\u5f81\u9009\u62e9<\/h4>\n<\/p>\n<p><p>\u5728\u6784\u5efa\u6a21\u578b\u4e4b\u524d\uff0c\u901a\u8fc7\u76f8\u5173\u5206\u6790\u6216\u7279\u5f81\u9009\u62e9\u65b9\u6cd5\u51cf\u5c11\u5197\u4f59\u53d8\u91cf\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h4>3. \u6301\u7eed\u76d1\u63a7<\/h4>\n<\/p>\n<p><p>\u5728\u6a21\u578b\u4f7f\u7528\u8fc7\u7a0b\u4e2d\uff0c\u6301\u7eed\u76d1\u63a7VIF\u503c\u548c\u6a21\u578b\u6027\u80fd\uff0c\u4ee5\u786e\u4fdd\u6a21\u578b\u7684\u7a33\u5b9a\u6027\u548c\u9884\u6d4b\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h4>4. \u7406\u89e3\u4e1a\u52a1\u80cc\u666f<\/h4>\n<\/p>\n<p><p>\u7ed3\u5408\u4e1a\u52a1\u80cc\u666f\u7406\u89e3\u591a\u91cd\u5171\u7ebf\u6027\u95ee\u9898\u7684\u5f71\u54cd\uff0c\u4ee5\u4fbf\u505a\u51fa\u5408\u7406\u7684\u51b3\u7b56\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u6709\u6548\u5730\u8ba1\u7b97VIF\uff0c\u5e76\u91c7\u53d6\u9002\u5f53\u7684\u63aa\u65bd\u6765\u89e3\u51b3\u591a\u91cd\u5171\u7ebf\u6027\u95ee\u9898\uff0c\u4ee5\u63d0\u9ad8\u56de\u5f52\u6a21\u578b\u7684\u7a33\u5b9a\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fVIF\uff0c\u4e3a\u4ec0\u4e48\u5728\u4f7f\u7528Python\u8fdb\u884c\u56de\u5f52\u5206\u6790\u65f6\u9700\u8981\u8ba1\u7b97\u5b83\uff1f<\/strong><br \/>VIF\uff0c\u5373\u65b9\u5dee\u81a8\u80c0\u56e0\u5b50\uff0c\u662f\u4e00\u79cd\u7528\u4e8e\u68c0\u6d4b\u591a\u91cd\u5171\u7ebf\u6027\u7684\u6307\u6807\u3002\u5728\u56de\u5f52\u5206\u6790\u4e2d\uff0c\u5f53\u81ea\u53d8\u91cf\u4e4b\u95f4\u5b58\u5728\u9ad8\u5ea6\u76f8\u5173\u6027\u65f6\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u6a21\u578b\u7684\u7a33\u5b9a\u6027\u548c\u89e3\u91ca\u529b\u3002\u8ba1\u7b97VIF\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u90a3\u4e9b\u53ef\u80fd\u5bfc\u81f4\u591a\u91cd\u5171\u7ebf\u6027\u95ee\u9898\u7684\u53d8\u91cf\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u51c6\u786e\u6027\u548c\u53ef\u89e3\u91ca\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u8ba1\u7b97VIF\u9700\u8981\u54ea\u4e9b\u5e93\u6216\u5de5\u5177\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>statsmodels<\/code>\u5e93\u6765\u8ba1\u7b97VIF\u3002\u9664\u4e86<code>statsmodels<\/code>\uff0c\u4f60\u53ef\u80fd\u8fd8\u9700\u8981<code>pandas<\/code>\u6765\u5904\u7406\u6570\u636e\u96c6\u3002\u5b89\u88c5\u8fd9\u4e9b\u5e93\u53ef\u4ee5\u901a\u8fc7<code>pip install statsmodels pandas<\/code>\u547d\u4ee4\u5b9e\u73b0\u3002\u786e\u4fdd\u4f60\u7684\u6570\u636e\u4ee5DataFrame\u7684\u5f62\u5f0f\u52a0\u8f7d\uff0c\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884cVIF\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0VIF\u7684\u8ba1\u7b97\uff0c\u5177\u4f53\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8ba1\u7b97VIF\u7684\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\uff1a  <\/p>\n<ol>\n<li>\u51c6\u5907\u6570\u636e\uff0c\u5c06\u81ea\u53d8\u91cf\u5b58\u50a8\u5728\u4e00\u4e2aDataFrame\u4e2d\u3002<\/li>\n<li>\u4f7f\u7528<code>statsmodels<\/code>\u4e2d\u7684<code>variance_inflation_factor<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u6bcf\u4e2a\u81ea\u53d8\u91cf\u7684VIF\u503c\u3002  <\/li>\n<li>\u904d\u5386\u81ea\u53d8\u91cf\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u53d8\u91cf\u7684VIF\uff0c\u5e76\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u65b0\u7684DataFrame\u4e2d\u3002  <\/li>\n<li>\u901a\u8fc7\u5206\u6790VIF\u503c\uff0c\u5224\u65ad\u54ea\u4e9b\u53d8\u91cf\u53ef\u80fd\u5bfc\u81f4\u591a\u91cd\u5171\u7ebf\u6027\uff0c\u5e76\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\uff08\u5982\u5220\u9664\u6216\u5408\u5e76\u53d8\u91cf\uff09\u3002<br \/>\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/li>\n<\/ol>\n<pre><code class=\"language-python\">import pandas as pd\nfrom statsmodels.stats.outliers_influence import variance_inflation_factor\n\n# \u5047\u8bbedf\u662f\u4f60\u7684DataFrame\nX = df[[&#39;variable1&#39;, &#39;variable2&#39;, &#39;variable3&#39;]]\nvif_data = pd.DataFrame()\nvif_data[&quot;Variable&quot;] = X.columns\nvif_data[&quot;VIF&quot;] = [variance_inflation_factor(X.values, i) for i in range(X.shape[1])]\nprint(vif_data)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u89e3\u8bfbVIF\u7684\u503c\uff0c\u4ec0\u4e48\u503c\u8868\u660e\u5b58\u5728\u591a\u91cd\u5171\u7ebf\u6027\uff1f<\/strong><br \/>VIF\u503c\u7684\u89e3\u91ca\u5982\u4e0b\uff1a  <\/p>\n<ul>\n<li>VIF = 1\uff1a\u6ca1\u6709\u591a\u91cd\u5171\u7ebf\u6027\u3002  <\/li>\n<li>1 &lt; VIF &lt; 5\uff1a\u591a\u91cd\u5171\u7ebf\u6027\u53ef\u80fd\u5b58\u5728\uff0c\u4f46\u901a\u5e38\u53ef\u4ee5\u63a5\u53d7\u3002  <\/li>\n<li>VIF \u2265 5\uff1a\u5b58\u5728\u8f83\u5f3a\u7684\u591a\u91cd\u5171\u7ebf\u6027\uff0c\u5e94\u4ed4\u7ec6\u68c0\u67e5\u8fd9\u4e9b\u53d8\u91cf\u3002  <\/li>\n<li>VIF \u2265 10\uff1a\u901a\u5e38\u8ba4\u4e3a\u5b58\u5728\u4e25\u91cd\u7684\u591a\u91cd\u5171\u7ebf\u6027\uff0c\u9700\u8981\u91c7\u53d6\u63aa\u65bd\u6765\u5904\u7406\u3002<br \/>\u7406\u89e3\u8fd9\u4e9b\u503c\u80fd\u591f\u5e2e\u52a9\u4f60\u505a\u51fa\u66f4\u660e\u667a\u7684\u51b3\u7b56\uff0c\u4ee5\u4f18\u5316\u56de\u5f52\u6a21\u578b\u7684\u8868\u73b0\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8ba1\u7b97VIF\uff08\u65b9\u5dee\u81a8\u80c0\u56e0\u5b50\uff09\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528statsmodels\u5e93\u3001\u7406\u89e3VIF\u7684\u8ba1\u7b97\u539f\u7406\u3001\u9009\u62e9\u6b63 [&hellip;]","protected":false},"author":3,"featured_media":937702,"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\/937697"}],"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=937697"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/937697\/revisions"}],"predecessor-version":[{"id":937703,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/937697\/revisions\/937703"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/937702"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=937697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=937697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=937697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}