{"id":1161209,"date":"2025-01-13T19:13:47","date_gmt":"2025-01-13T11:13:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161209.html"},"modified":"2025-01-13T19:13:50","modified_gmt":"2025-01-13T11:13:50","slug":"python-r2%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161209.html","title":{"rendered":"python r2\u5982\u4f55\u8ba1\u7b97"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202526\/d88f75f0-97f7-47da-b00a-70c7042c09a5.webp\" alt=\"python r2\u5982\u4f55\u8ba1\u7b97\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u7b97R\u00b2\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528scikit-learn\u3001statsmodels\u7b49\u5e93\u3001\u624b\u52a8\u8ba1\u7b97\u3001\u4f7f\u7528numpy\u7b49<\/strong>\u3002\u5176\u4e2d\u63a8\u8350\u4f7f\u7528scikit-learn\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u5355\u3001\u4fbf\u6377\u4e14\u529f\u80fd\u5f3a\u5927\u7684\u63a5\u53e3\u3002<strong>R\u00b2\uff08\u51b3\u5b9a\u7cfb\u6570\uff09\u662f\u4e00\u79cd\u8861\u91cf\u6a21\u578b\u62df\u5408\u4f18\u5ea6\u7684\u7edf\u8ba1\u91cf\uff0c\u8868\u793a\u81ea\u53d8\u91cf\u89e3\u91ca\u56e0\u53d8\u91cf\u53d8\u5f02\u7684\u6bd4\u4f8b<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528scikit-learn\u5e93\u8ba1\u7b97R\u00b2\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528scikit-learn\u5e93\u8ba1\u7b97R\u00b2<\/h3>\n<\/p>\n<p><p>Scikit-learn\u662f\u4e00\u4e2a\u5f3a\u5927\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e93\uff0c\u63d0\u4f9b\u4e86\u8ba1\u7b97R\u00b2\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528scikit-learn\u5e93\u8ba1\u7b97\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u7684R\u00b2\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from sklearn.linear_model import LinearRegression<\/p>\n<p>from sklearn.metrics import r2_score<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([[1], [2], [3], [4], [5]])<\/p>\n<p>y = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<p>model.fit(X, y)<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>y_pred = model.predict(X)<\/p>\n<h2><strong>\u8ba1\u7b97R\u00b2<\/strong><\/h2>\n<p>r2 = r2_score(y, y_pred)<\/p>\n<p>print(&quot;R\u00b2:&quot;, r2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u6211\u4eec\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff0c\u5e76\u751f\u6210\u793a\u4f8b\u6570\u636e\u3002\u7136\u540e\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u5e76\u8fdb\u884c\u8bad\u7ec3\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u8fdb\u884c\u9884\u6d4b\uff0c\u6700\u540e\u4f7f\u7528<code>r2_score<\/code>\u51fd\u6570\u8ba1\u7b97R\u00b2\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528statsmodels\u5e93\u8ba1\u7b97R\u00b2<\/h3>\n<\/p>\n<p><p>Statsmodels\u5e93\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u7edf\u8ba1\u5efa\u6a21\u5e93\uff0c\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97R\u00b2\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528statsmodels\u5e93\u8ba1\u7b97R\u00b2\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import statsmodels.api as sm<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([1, 2, 3, 4, 5])<\/p>\n<p>y = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u6dfb\u52a0\u5e38\u6570\u9879<\/strong><\/h2>\n<p>X = sm.add_constant(X)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u62df\u5408\u6a21\u578b<\/strong><\/h2>\n<p>model = sm.OLS(y, X).fit()<\/p>\n<h2><strong>\u8f93\u51fa\u6a21\u578b\u6458\u8981<\/strong><\/h2>\n<p>print(model.summary())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>sm.add_constant<\/code>\u51fd\u6570\u7528\u4e8e\u6dfb\u52a0\u5e38\u6570\u9879\uff0c\u7136\u540e\u4f7f\u7528<code>sm.OLS<\/code>\u521b\u5efa\u4e00\u4e2a\u666e\u901a\u6700\u5c0f\u4e8c\u4e58\u56de\u5f52\u6a21\u578b\u5e76\u8fdb\u884c\u62df\u5408\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>model.summary()<\/code>\u8f93\u51fa\u6a21\u578b\u6458\u8981\uff0c\u5176\u4e2d\u5305\u62ecR\u00b2\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u624b\u52a8\u8ba1\u7b97R\u00b2<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4e0d\u60f3\u4f7f\u7528\u4efb\u4f55\u5e93\uff0c\u4e5f\u53ef\u4ee5\u624b\u52a8\u8ba1\u7b97R\u00b2\u3002\u4ee5\u4e0b\u662f\u624b\u52a8\u8ba1\u7b97R\u00b2\u7684\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([1, 2, 3, 4, 5])<\/p>\n<p>y = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u8ba1\u7b97y\u7684\u5e73\u5747\u503c<\/strong><\/h2>\n<p>y_mean = np.mean(y)<\/p>\n<h2><strong>\u8ba1\u7b97\u603b\u5e73\u65b9\u548c\uff08\u603b\u53d8\u5dee\uff09<\/strong><\/h2>\n<p>ss_tot = np.sum((y - y_mean)  2)<\/p>\n<h2><strong>\u8ba1\u7b97\u56de\u5f52\u5e73\u65b9\u548c\uff08\u89e3\u91ca\u53d8\u5dee\uff09<\/strong><\/h2>\n<p>ss_res = np.sum((y - (0.5 + 1 * X))  2)<\/p>\n<h2><strong>\u8ba1\u7b97R\u00b2<\/strong><\/h2>\n<p>r2 = 1 - (ss_res \/ ss_tot)<\/p>\n<p>print(&quot;R\u00b2:&quot;, r2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u6211\u4eec\u8ba1\u7b97y\u7684\u5e73\u5747\u503c\uff0c\u7136\u540e\u8ba1\u7b97\u603b\u5e73\u65b9\u548c\u548c\u56de\u5f52\u5e73\u65b9\u548c\uff0c\u6700\u540e\u6839\u636eR\u00b2\u516c\u5f0f\u8ba1\u7b97R\u00b2\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528numpy\u5e93\u8ba1\u7b97R\u00b2<\/h3>\n<\/p>\n<p><p>Numpy\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u4e5f\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97R\u00b2\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528numpy\u5e93\u8ba1\u7b97R\u00b2\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([1, 2, 3, 4, 5])<\/p>\n<p>y = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u8ba1\u7b97y\u7684\u5e73\u5747\u503c<\/strong><\/h2>\n<p>y_mean = np.mean(y)<\/p>\n<h2><strong>\u8ba1\u7b97\u603b\u5e73\u65b9\u548c\uff08\u603b\u53d8\u5dee\uff09<\/strong><\/h2>\n<p>ss_tot = np.sum((y - y_mean)  2)<\/p>\n<h2><strong>\u8ba1\u7b97\u56de\u5f52\u5e73\u65b9\u548c\uff08\u89e3\u91ca\u53d8\u5dee\uff09<\/strong><\/h2>\n<p>ss_res = np.sum((y - (0.5 + 1 * X))  2)<\/p>\n<h2><strong>\u8ba1\u7b97R\u00b2<\/strong><\/h2>\n<p>r2 = 1 - (ss_res \/ ss_tot)<\/p>\n<p>print(&quot;R\u00b2:&quot;, r2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528numpy\u5e93\u8ba1\u7b97\u4e86\u603b\u5e73\u65b9\u548c\u548c\u56de\u5f52\u5e73\u65b9\u548c\uff0c\u7136\u540e\u6839\u636eR\u00b2\u516c\u5f0f\u8ba1\u7b97R\u00b2\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001R\u00b2\u7684\u89e3\u91ca<\/h3>\n<\/p>\n<p><p>R\u00b2\u7684\u503c\u4ecb\u4e8e0\u548c1\u4e4b\u95f4\uff0c\u8868\u793a\u6a21\u578b\u89e3\u91ca\u4e86\u56e0\u53d8\u91cf\u53d8\u5f02\u7684\u6bd4\u4f8b\u3002<strong>\u8f83\u9ad8\u7684R\u00b2\u503c\u8868\u793a\u6a21\u578b\u5bf9\u6570\u636e\u7684\u62df\u5408\u5ea6\u8f83\u597d<\/strong>\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9ad8R\u00b2\u503c\u5e76\u4e0d\u4e00\u5b9a\u8868\u793a\u6a21\u578b\u662f\u597d\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u8fc7\u62df\u5408\u6570\u636e\u3002<strong>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u8bc4\u4f30\u6307\u6807\u548c\u4ea4\u53c9\u9a8c\u8bc1\u7b49\u65b9\u6cd5\u6765\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd<\/strong>\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4e0d\u540c\u5e93\u8ba1\u7b97R\u00b2\u7684\u5f02\u540c<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u4e0d\u540c\u7684\u5e93\u8ba1\u7b97R\u00b2\u7684\u65b9\u6cd5\u7565\u6709\u4e0d\u540c\uff0c\u4f46\u57fa\u672c\u539f\u7406\u662f\u76f8\u540c\u7684\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u4e3b\u8981\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u4e60\u60ef\u3002\u5982\u679c\u4f60\u4e60\u60ef\u4f7f\u7528scikit-learn\u5e93\u8fdb\u884c\u673a\u5668\u5b66\u4e60\u5efa\u6a21\uff0c\u90a3\u4e48\u4f7f\u7528scikit-learn\u5e93\u8ba1\u7b97R\u00b2\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff1b\u5982\u679c\u4f60\u66f4\u559c\u6b22\u4f7f\u7528statsmodels\u5e93\u8fdb\u884c\u7edf\u8ba1\u5efa\u6a21\uff0c\u90a3\u4e48\u4f7f\u7528statsmodels\u5e93\u8ba1\u7b97R\u00b2\u4e5f\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u8ba1\u7b97R\u00b2\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97R\u00b2\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u9884\u5904\u7406<\/strong>\uff1a\u5728\u8fdb\u884c\u5efa\u6a21\u548c\u8ba1\u7b97R\u00b2\u4e4b\u524d\uff0c\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9002\u5f53\u7684\u9884\u5904\u7406\uff0c\u5305\u62ec\u53bb\u9664\u5f02\u5e38\u503c\u3001\u6807\u51c6\u5316\u7b49\u3002<\/li>\n<li><strong>\u6a21\u578b\u9009\u62e9<\/strong>\uff1a\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\u5bf9\u4e8e\u8ba1\u7b97R\u00b2\u975e\u5e38\u91cd\u8981\uff0c\u4e0d\u540c\u7684\u6a21\u578b\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684R\u00b2\u503c\u3002<\/li>\n<li><strong>\u8fc7\u62df\u5408\u548c\u6b20\u62df\u5408<\/strong>\uff1a\u9ad8R\u00b2\u503c\u5e76\u4e0d\u4e00\u5b9a\u8868\u793a\u6a21\u578b\u662f\u597d\u7684\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u4f1a\u8fc7\u62df\u5408\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u7ed3\u5408\u5176\u4ed6\u8bc4\u4f30\u6307\u6807\u548c\u4ea4\u53c9\u9a8c\u8bc1\u7b49\u65b9\u6cd5\u6765\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\u3002<\/li>\n<li><strong>\u89e3\u91caR\u00b2\u503c<\/strong>\uff1aR\u00b2\u503c\u7684\u89e3\u91ca\u9700\u8981\u7ed3\u5408\u5177\u4f53\u7684\u5e94\u7528\u80cc\u666f\u548c\u6570\u636e\u60c5\u51b5\uff0c\u4e0d\u80fd\u7b80\u5355\u5730\u8ba4\u4e3a\u9ad8R\u00b2\u503c\u8868\u793a\u6a21\u578b\u597d\u3002<\/li>\n<li><strong>\u4f7f\u7528\u4e0d\u540c\u5e93\u7684\u7ed3\u679c\u4e00\u81f4\u6027<\/strong>\uff1a\u5728\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u8ba1\u7b97R\u00b2\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u8ba1\u7b97\u7ed3\u679c\u7684\u4e00\u81f4\u6027\uff0c\u4ee5\u514d\u56e0\u4e3a\u5b9e\u73b0\u7ec6\u8282\u7684\u5dee\u5f02\u5bfc\u81f4\u7ed3\u679c\u4e0d\u4e00\u81f4\u3002<\/li>\n<\/ol>\n<p><h3>\u516b\u3001R\u00b2\u7684\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u7b80\u5355\u7684\u7ebf\u6027\u56de\u5f52\uff0cR\u00b2\u8fd8\u53ef\u4ee5\u7528\u4e8e\u5176\u4ed6\u7c7b\u578b\u7684\u56de\u5f52\u6a21\u578b\uff0c\u5982\u591a\u5143\u7ebf\u6027\u56de\u5f52\u3001\u5cad\u56de\u5f52\u3001Lasso\u56de\u5f52\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u591a\u5143\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u8ba1\u7b97R\u00b2\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from sklearn.linear_model import LinearRegression<\/p>\n<p>from sklearn.metrics import r2_score<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6]])<\/p>\n<p>y = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<p>model.fit(X, y)<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>y_pred = model.predict(X)<\/p>\n<h2><strong>\u8ba1\u7b97R\u00b2<\/strong><\/h2>\n<p>r2 = r2_score(y, y_pred)<\/p>\n<p>print(&quot;R\u00b2:&quot;, r2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u751f\u6210\u4e86\u591a\u5143\u7ebf\u6027\u56de\u5f52\u7684\u793a\u4f8b\u6570\u636e\uff0c\u5e76\u4f7f\u7528scikit-learn\u5e93\u8ba1\u7b97\u4e86R\u00b2\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u770b\u51fa\uff0c<strong>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u8ba1\u7b97R\u00b2\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528scikit-learn\u3001statsmodels\u3001\u624b\u52a8\u8ba1\u7b97\u3001\u4f7f\u7528numpy\u7b49<\/strong>\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u4e3b\u8981\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u4e60\u60ef\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<strong>\u5728\u8ba1\u7b97R\u00b2\u65f6\uff0c\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9002\u5f53\u7684\u9884\u5904\u7406\uff0c\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b\uff0c\u5e76\u7ed3\u5408\u5176\u4ed6\u8bc4\u4f30\u6307\u6807\u548c\u4ea4\u53c9\u9a8c\u8bc1\u7b49\u65b9\u6cd5\u6765\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd<\/strong>\u3002\u6b64\u5916\uff0c<strong>R\u00b2\u7684\u89e3\u91ca\u9700\u8981\u7ed3\u5408\u5177\u4f53\u7684\u5e94\u7528\u80cc\u666f\u548c\u6570\u636e\u60c5\u51b5\uff0c\u4e0d\u80fd\u7b80\u5355\u5730\u8ba4\u4e3a\u9ad8R\u00b2\u503c\u8868\u793a\u6a21\u578b\u597d<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u4f60\u80fd\u591f\u638c\u63e1Python\u8ba1\u7b97R\u00b2\u7684\u65b9\u6cd5\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u8fd0\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8ba1\u7b97R\u00b2\u503c\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0cR\u00b2\u503c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8ba1\u7b97\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u5229\u7528<code>scikit-learn<\/code>\u5e93\u4e2d\u7684<code>r2_score<\/code>\u51fd\u6570\u3002\u53ef\u4ee5\u5148\u901a\u8fc7\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u62df\u5408\u6570\u636e\uff0c\u7136\u540e\u8c03\u7528<code>r2_score<\/code>\u51fd\u6570\u6765\u8ba1\u7b97R\u00b2\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">from sklearn.linear_model import LinearRegression\nfrom sklearn.metrics import r2_score\nimport numpy as np\n\n# \u793a\u4f8b\u6570\u636e\nX = np.array([[1], [2], [3], [4], [5]])\ny = np.array([1, 2, 3, 4, 5])\n\n# \u62df\u5408\u6a21\u578b\nmodel = LinearRegression()\nmodel.fit(X, y)\ny_pred = model.predict(X)\n\n# \u8ba1\u7b97R\u00b2\u503c\nr2 = r2_score(y, y_pred)\nprint(f&quot;R\u00b2\u503c\u4e3a: {r2}&quot;)\n<\/code><\/pre>\n<p><strong>R\u00b2\u503c\u7684\u610f\u4e49\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>R\u00b2\u503c\uff08\u51b3\u5b9a\u7cfb\u6570\uff09\u8868\u793a\u6a21\u578b\u89e3\u91ca\u4e86\u6570\u636e\u53d8\u5f02\u7684\u6bd4\u4f8b\u3002\u503c\u57df\u57280\u52301\u4e4b\u95f4\uff0c0\u8868\u793a\u6a21\u578b\u6ca1\u6709\u89e3\u91ca\u4efb\u4f55\u53d8\u5f02\uff0c1\u5219\u8868\u793a\u6a21\u578b\u5b8c\u7f8e\u5730\u89e3\u91ca\u4e86\u6240\u6709\u53d8\u5f02\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0cR\u00b2\u503c\u8d8a\u63a5\u8fd11\uff0c\u6a21\u578b\u7684\u9884\u6d4b\u80fd\u529b\u8d8a\u5f3a\u3002<\/p>\n<p><strong>R\u00b2\u503c\u662f\u5426\u603b\u662f\u53ef\u9760\u7684\u6307\u6807\uff1f<\/strong><br \/>\u867d\u7136R\u00b2\u503c\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u6a21\u578b\u8bc4\u4f30\u6307\u6807\uff0c\u4f46\u5b83\u5e76\u4e0d\u662f\u7edd\u5bf9\u53ef\u9760\u7684\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5728\u5b58\u5728\u5f02\u5e38\u503c\u6216\u6a21\u578b\u8fc7\u62df\u5408\u7684\u60c5\u51b5\u4e0b\uff0cR\u00b2\u503c\u53ef\u80fd\u4f1a\u8bef\u5bfc\u7528\u6237\u3002\u56e0\u6b64\uff0c\u7ed3\u5408\u5176\u4ed6\u8bc4\u4f30\u6307\u6807\uff08\u5982\u5747\u65b9\u6839\u8bef\u5deeRMSE\u6216\u5e73\u5747\u7edd\u5bf9\u8bef\u5deeMAE\uff09\u6765\u7efc\u5408\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\u662f\u660e\u667a\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u7b97R\u00b2\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528scikit-learn\u3001statsmodels\u7b49\u5e93\u3001\u624b\u52a8\u8ba1\u7b97\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1161217,"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\/1161209"}],"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=1161209"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161209\/revisions"}],"predecessor-version":[{"id":1161221,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161209\/revisions\/1161221"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161217"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}