{"id":1175218,"date":"2025-01-15T17:29:03","date_gmt":"2025-01-15T09:29:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1175218.html"},"modified":"2025-01-15T17:29:05","modified_gmt":"2025-01-15T09:29:05","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82r2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1175218.html","title":{"rendered":"python\u5982\u4f55\u6c42R2"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25110732\/731a5242-2207-4fee-9ba9-232fdfa1f4d6.webp\" alt=\"python\u5982\u4f55\u6c42R2\" \/><\/p>\n<p><p> <strong>Python\u6c42R2\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5e93\u51fd\u6570\u3001\u624b\u52a8\u8ba1\u7b97\u3001\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u7b49<\/strong>\u3002\u5728\u8fd9\u51e0\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f\u901a\u8fc7\u5e93\u51fd\u6570\u548c\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u6765\u8ba1\u7b97R2\u503c\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8ba1\u7b97R2\u3002<\/p>\n<\/p>\n<p><p><strong>\u5e93\u51fd\u6570\u8ba1\u7b97R2<\/strong><\/p>\n<\/p>\n<p><p>Python\u4e2d\u7684\u5e93\u51fd\u6570\u5982<code>scikit-learn<\/code>\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97R2\u3002\u4f7f\u7528<code>scikit-learn<\/code>\u7684<code>r2_score<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5feb\u901f\u8ba1\u7b97R2\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import r2_score<\/p>\n<h2><strong>\u771f\u5b9e\u503c\u548c\u9884\u6d4b\u503c<\/strong><\/h2>\n<p>y_true = [3, -0.5, 2, 7]<\/p>\n<p>y_pred = [2.5, 0.0, 2, 8]<\/p>\n<h2><strong>\u8ba1\u7b97R2<\/strong><\/h2>\n<p>r2 = r2_score(y_true, y_pred)<\/p>\n<p>print(&quot;R2 Score:&quot;, r2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u9996\u5148\u5bfc\u5165<code>r2_score<\/code>\u51fd\u6570\uff0c\u7136\u540e\u63d0\u4f9b\u771f\u5b9e\u503c\u548c\u9884\u6d4b\u503c\u5217\u8868\uff0c\u6700\u540e\u8ba1\u7b97\u5e76\u8f93\u51faR2\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u624b\u52a8\u8ba1\u7b97R2<\/h3>\n<\/p>\n<p><p>\u624b\u52a8\u8ba1\u7b97R2\u9700\u8981\u4e86\u89e3R2\u516c\u5f0f\u4ee5\u53ca\u5982\u4f55\u5c06\u5176\u5e94\u7528\u4e8e\u6570\u636e\u3002R2\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>[ R^2 = 1 &#8211; \\frac{\\sum (y_i &#8211; \\hat{y}_i)^2}{\\sum (y_i &#8211; \\bar{y})^2} ]<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li>( y_i ) \u662f\u771f\u5b9e\u503c<\/li>\n<li>( \\hat{y}_i ) \u662f\u9884\u6d4b\u503c<\/li>\n<li>( \\bar{y} ) \u662f\u771f\u5b9e\u503c\u7684\u5747\u503c<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u771f\u5b9e\u503c\u548c\u9884\u6d4b\u503c<\/strong><\/h2>\n<p>y_true = np.array([3, -0.5, 2, 7])<\/p>\n<p>y_pred = np.array([2.5, 0.0, 2, 8])<\/p>\n<h2><strong>\u8ba1\u7b97\u5747\u503c<\/strong><\/h2>\n<p>y_mean = np.mean(y_true)<\/p>\n<h2><strong>\u8ba1\u7b97\u603b\u5e73\u65b9\u548c\u548c\u56de\u5f52\u5e73\u65b9\u548c<\/strong><\/h2>\n<p>ss_total = np.sum((y_true - y_mean)  2)<\/p>\n<p>ss_residual = np.sum((y_true - y_pred)  2)<\/p>\n<h2><strong>\u8ba1\u7b97R2<\/strong><\/h2>\n<p>r2 = 1 - (ss_residual \/ ss_total)<\/p>\n<p>print(&quot;R2 Score:&quot;, r2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8ba1\u7b97\u771f\u5b9e\u503c\u7684\u5747\u503c\uff0c\u7136\u540e\u8ba1\u7b97\u603b\u5e73\u65b9\u548c\u548c\u56de\u5f52\u5e73\u65b9\u548c\uff0c\u6700\u540e\u4f7f\u7528\u516c\u5f0f\u8ba1\u7b97R2\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/h3>\n<\/p>\n<p><p><code>scikit-learn<\/code>\u5e93\u4e2d\u7684\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u53ef\u4ee5\u81ea\u52a8\u8ba1\u7b97R2\u503c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5177\u4f53\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import LinearRegression<\/p>\n<h2><strong>\u6570\u636e\u96c6<\/strong><\/h2>\n<p>X = [[1], [2], [3], [4]]<\/p>\n<p>y = [3, 2, 4, 5]<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<h2><strong>\u62df\u5408\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(X, y)<\/p>\n<h2><strong>\u8ba1\u7b97R2<\/strong><\/h2>\n<p>r2 = model.score(X, y)<\/p>\n<p>print(&quot;R2 Score:&quot;, r2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u7ebf\u6027\u56de\u5f52\u6a21\u578b\uff0c\u4f7f\u7528\u6570\u636e\u96c6\u62df\u5408\u6a21\u578b\uff0c\u7136\u540e\u8ba1\u7b97\u5e76\u8f93\u51faR2\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001R2\u7684\u610f\u4e49\u548c\u5e94\u7528<\/h3>\n<\/p>\n<p><p><strong>R2\u503c\u7684\u610f\u4e49<\/strong><\/p>\n<\/p>\n<p><p>R2\u503c\u662f\u5224\u5b9a\u7cfb\u6570\uff08coefficient of determination\uff09\uff0c\u7528\u4e8e\u8861\u91cf\u6a21\u578b\u7684\u62df\u5408\u4f18\u5ea6\u3002\u5b83\u8868\u793a\u81ea\u53d8\u91cf\u89e3\u91ca\u56e0\u53d8\u91cf\u53d8\u5f02\u7684\u6bd4\u4f8b\u3002R2\u503c\u7684\u8303\u56f4\u662f0\u52301\uff0c\u503c\u8d8a\u63a5\u8fd11\uff0c\u8868\u793a\u6a21\u578b\u5bf9\u6570\u636e\u7684\u89e3\u91ca\u80fd\u529b\u8d8a\u5f3a\u3002<\/p>\n<\/p>\n<p><p><strong>\u5e94\u7528\u9886\u57df<\/strong><\/p>\n<\/p>\n<p><p>R2\u503c\u5728\u8bb8\u591a\u9886\u57df\u90fd\u6709\u5e7f\u6cdb\u5e94\u7528\uff0c\u5305\u62ec\u7ecf\u6d4e\u5b66\u3001\u7edf\u8ba1\u5b66\u3001\u6570\u636e\u79d1\u5b66\u7b49\u3002\u4f8b\u5982\uff0c\u5728\u7ecf\u6d4e\u5b66\u4e2d\uff0cR2\u503c\u53ef\u4ee5\u7528\u4e8e\u8bc4\u4f30\u56de\u5f52\u6a21\u578b\u5bf9\u7ecf\u6d4e\u6307\u6807\u7684\u9884\u6d4b\u80fd\u529b\uff1b\u5728\u6570\u636e\u79d1\u5b66\u4e2d\uff0cR2\u503c\u7528\u4e8e\u8bc4\u4f30<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001R2\u7684\u5c40\u9650\u6027<\/h3>\n<\/p>\n<p><p><strong>\u4e0d\u9002\u7528\u4e8e\u975e\u7ebf\u6027\u6a21\u578b<\/strong><\/p>\n<\/p>\n<p><p>R2\u503c\u9002\u7528\u4e8e\u7ebf\u6027\u56de\u5f52\u6a21\u578b\uff0c\u4f46\u5728\u975e\u7ebf\u6027\u6a21\u578b\u4e2d\uff0c\u5176\u89e3\u91ca\u80fd\u529b\u6709\u9650\u3002\u5728\u975e\u7ebf\u6027\u56de\u5f52\u4e2d\uff0c\u4f7f\u7528\u8c03\u6574\u540e\u7684R2\u503c\u6216\u5176\u4ed6\u8bc4\u4ef7\u6307\u6807\u53ef\u80fd\u66f4\u5408\u9002\u3002<\/p>\n<\/p>\n<p><p><strong>\u65e0\u6cd5\u68c0\u6d4b\u8fc7\u62df\u5408<\/strong><\/p>\n<\/p>\n<p><p>R2\u503c\u65e0\u6cd5\u68c0\u6d4b\u6a21\u578b\u662f\u5426\u8fc7\u62df\u5408\u3002\u5373\u4f7f\u6a21\u578b\u8fc7\u62df\u5408\uff0cR2\u503c\u4e5f\u53ef\u80fd\u5f88\u9ad8\u3002\u56e0\u6b64\uff0c\u5728\u6a21\u578b\u8bc4\u4f30\u65f6\uff0c\u9664\u4e86R2\u503c\uff0c\u8fd8\u5e94\u8003\u8651\u5176\u4ed6\u8bc4\u4ef7\u6307\u6807\u548c\u4ea4\u53c9\u9a8c\u8bc1\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8c03\u6574\u540e\u7684R2\u503c<\/h3>\n<\/p>\n<p><p>\u8c03\u6574\u540e\u7684R2\u503c\uff08adjusted R2\uff09\u662f\u5bf9R2\u503c\u7684\u4e00\u79cd\u4fee\u6b63\uff0c\u8003\u8651\u4e86\u6a21\u578b\u4e2d\u7684\u81ea\u53d8\u91cf\u6570\u91cf\u3002\u5176\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>[ \\text{Adjusted } R^2 = 1 &#8211; \\left( \\frac{1 &#8211; R^2}{n &#8211; k &#8211; 1} \\right) ]<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li>( n ) \u662f\u6837\u672c\u6570\u91cf<\/li>\n<li>( k ) \u662f\u81ea\u53d8\u91cf\u6570\u91cf<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">from sklearn.metrics import r2_score<\/p>\n<h2><strong>\u8ba1\u7b97\u8c03\u6574\u540e\u7684R2\u503c<\/strong><\/h2>\n<p>def adjusted_r2(r2, n, k):<\/p>\n<p>    return 1 - ((1 - r2) * (n - 1) \/ (n - k - 1))<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>n = len(y_true)<\/p>\n<p>k = 1  # \u5047\u8bbe\u53ea\u6709\u4e00\u4e2a\u81ea\u53d8\u91cf<\/p>\n<h2><strong>\u8ba1\u7b97R2<\/strong><\/h2>\n<p>r2 = r2_score(y_true, y_pred)<\/p>\n<h2><strong>\u8ba1\u7b97\u8c03\u6574\u540e\u7684R2<\/strong><\/h2>\n<p>adj_r2 = adjusted_r2(r2, n, k)<\/p>\n<p>print(&quot;Adjusted R2 Score:&quot;, adj_r2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u8ba1\u7b97R2\u503c\uff0c\u7136\u540e\u4f7f\u7528\u516c\u5f0f\u8ba1\u7b97\u8c03\u6574\u540e\u7684R2\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5176\u4ed6\u8bc4\u4ef7\u6307\u6807<\/h3>\n<\/p>\n<p><p>\u9664\u4e86R2\u503c\uff0c\u8fd8\u6709\u5176\u4ed6\u8bb8\u591a\u8bc4\u4ef7\u6307\u6807\u53ef\u4ee5\u7528\u4e8e\u8bc4\u4f30\u56de\u5f52\u6a21\u578b\u7684\u6027\u80fd\uff0c\u5305\u62ec\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u3001\u5747\u65b9\u6839\u8bef\u5dee\uff08RMSE\uff09\u548c\u5e73\u5747\u7edd\u5bf9\u8bef\u5dee\uff08MAE\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u5747\u65b9\u8bef\u5dee\u7528\u4e8e\u8861\u91cf\u9884\u6d4b\u503c\u4e0e\u771f\u5b9e\u503c\u4e4b\u95f4\u7684\u5e73\u5747\u5e73\u65b9\u5dee\u3002\u5176\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>[ \\text{MSE} = \\frac{1}{n} \\sum (y_i &#8211; \\hat{y}_i)^2 ]<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import mean_squared_error<\/p>\n<h2><strong>\u8ba1\u7b97MSE<\/strong><\/h2>\n<p>mse = mean_squared_error(y_true, y_pred)<\/p>\n<p>print(&quot;MSE:&quot;, mse)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5747\u65b9\u6839\u8bef\u5dee\uff08RMSE\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u5747\u65b9\u6839\u8bef\u5dee\u662f\u5747\u65b9\u8bef\u5dee\u7684\u5e73\u65b9\u6839\uff0c\u7528\u4e8e\u8861\u91cf\u9884\u6d4b\u503c\u4e0e\u771f\u5b9e\u503c\u4e4b\u95f4\u7684\u5e73\u5747\u5dee\u5f02\u3002\u5176\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>[ \\text{RMSE} = \\sqrt{\\text{MSE}} ]<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">rmse = np.sqrt(mse)<\/p>\n<p>print(&quot;RMSE:&quot;, rmse)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5e73\u5747\u7edd\u5bf9\u8bef\u5dee\uff08MAE\uff09<\/strong><\/p>\n<\/p>\n<p><p>\u5e73\u5747\u7edd\u5bf9\u8bef\u5dee\u7528\u4e8e\u8861\u91cf\u9884\u6d4b\u503c\u4e0e\u771f\u5b9e\u503c\u4e4b\u95f4\u7684\u5e73\u5747\u7edd\u5bf9\u5dee\u3002\u5176\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>[ \\text{MAE} = \\frac{1}{n} \\sum |y_i &#8211; \\hat{y}_i| ]<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import mean_absolute_error<\/p>\n<h2><strong>\u8ba1\u7b97MAE<\/strong><\/h2>\n<p>mae = mean_absolute_error(y_true, y_pred)<\/p>\n<p>print(&quot;MAE:&quot;, mae)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001R2\u503c\u5728\u4e0d\u540c\u9886\u57df\u7684\u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p><strong>\u7ecf\u6d4e\u5b66<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u7ecf\u6d4e\u5b66\u4e2d\uff0cR2\u503c\u53ef\u4ee5\u7528\u4e8e\u8bc4\u4f30\u56de\u5f52\u6a21\u578b\u5bf9\u7ecf\u6d4e\u6307\u6807\u7684\u9884\u6d4b\u80fd\u529b\u3002\u4f8b\u5982\uff0c\u7814\u7a76\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528R2\u503c\u6765\u8bc4\u4f30\u6a21\u578b\u5bf9GDP\u589e\u957f\u7387\u7684\u89e3\u91ca\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><p><strong>\u533b\u7597<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u533b\u7597\u9886\u57df\uff0cR2\u503c\u53ef\u4ee5\u7528\u4e8e\u8bc4\u4f30\u56de\u5f52\u6a21\u578b\u5bf9\u60a3\u8005\u5065\u5eb7\u6307\u6807\u7684\u9884\u6d4b\u80fd\u529b\u3002\u4f8b\u5982\uff0c\u7814\u7a76\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528R2\u503c\u6765\u8bc4\u4f30\u6a21\u578b\u5bf9\u60a3\u8005\u8840\u538b\u53d8\u5316\u7684\u89e3\u91ca\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><p><strong>\u5de5\u7a0b<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5de5\u7a0b\u9886\u57df\uff0cR2\u503c\u53ef\u4ee5\u7528\u4e8e\u8bc4\u4f30\u56de\u5f52\u6a21\u578b\u5bf9\u5de5\u7a0b\u53c2\u6570\u7684\u9884\u6d4b\u80fd\u529b\u3002\u4f8b\u5982\uff0c\u5de5\u7a0b\u5e08\u53ef\u4ee5\u4f7f\u7528R2\u503c\u6765\u8bc4\u4f30\u6a21\u578b\u5bf9\u6750\u6599\u5f3a\u5ea6\u7684\u89e3\u91ca\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97R2\u503c\uff0c\u5305\u62ec\u4f7f\u7528\u5e93\u51fd\u6570\u3001\u624b\u52a8\u8ba1\u7b97\u548c\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u7b49\u65b9\u6cd5\u3002\u540c\u65f6\uff0c\u672c\u6587\u8fd8\u63a2\u8ba8\u4e86R2\u503c\u7684\u610f\u4e49\u3001\u5e94\u7528\u9886\u57df\u3001\u5c40\u9650\u6027\u3001\u8c03\u6574\u540e\u7684R2\u503c\u4ee5\u53ca\u5176\u4ed6\u8bc4\u4ef7\u6307\u6807\u3002\u901a\u8fc7\u8fd9\u4e9b\u5185\u5bb9\uff0c\u8bfb\u8005\u53ef\u4ee5\u5168\u9762\u4e86\u89e3R2\u503c\u7684\u8ba1\u7b97\u65b9\u6cd5\u53ca\u5176\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7684\u91cd\u8981\u6027\u3002\u5728\u6a21\u578b\u8bc4\u4f30\u65f6\uff0c\u9664\u4e86R2\u503c\uff0c\u8fd8\u5e94\u8003\u8651\u5176\u4ed6\u8bc4\u4ef7\u6307\u6807\uff0c\u4ee5\u4fbf\u66f4\u5168\u9762\u5730\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97R\u00b2\u503c\uff1f<\/strong><br \/>R\u00b2\u503c\uff08\u51b3\u5b9a\u7cfb\u6570\uff09\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528scikit-learn\u5e93\u4e2d\u7684<code>r2_score<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u3002\u9996\u5148\u9700\u8981\u5bfc\u5165\u5e93\uff0c\u7136\u540e\u901a\u8fc7\u771f\u5b9e\u503c\u4e0e\u9884\u6d4b\u503c\u6765\u83b7\u5f97R\u00b2\u503c\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from sklearn.metrics import r2_score\n\n# \u5047\u8bbe\u771f\u5b9e\u503c\u548c\u9884\u6d4b\u503c\u5982\u4e0b\ny_true = [3, -0.5, 2, 7]\ny_pred = [2.5, 0.0, 2, 8]\n\nr2 = r2_score(y_true, y_pred)\nprint(&quot;R\u00b2\u503c:&quot;, r2)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u8f7b\u677e\u5730\u5f97\u5230R\u00b2\u503c\uff0c\u53cd\u6620\u6a21\u578b\u7684\u62df\u5408\u7a0b\u5ea6\u3002<\/p>\n<p><strong>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0cR\u00b2\u503c\u6709\u4ec0\u4e48\u5b9e\u9645\u610f\u4e49\uff1f<\/strong><br \/>R\u00b2\u503c\u7684\u610f\u4e49\u5728\u4e8e\u5b83\u53ef\u4ee5\u5e2e\u52a9\u8bc4\u4f30\u6a21\u578b\u7684\u9884\u6d4b\u80fd\u529b\u3002\u503c\u7684\u8303\u56f4\u57280\u52301\u4e4b\u95f4\uff0c\u63a5\u8fd11\u8868\u793a\u6a21\u578b\u80fd\u591f\u5f88\u597d\u5730\u89e3\u91ca\u6570\u636e\u7684\u53d8\u5316\uff0c\u63a5\u8fd10\u5219\u8bf4\u660e\u6a21\u578b\u7684\u89e3\u91ca\u80fd\u529b\u8f83\u5f31\u3002\u901a\u8fc7\u7406\u89e3R\u00b2\u503c\uff0c\u5206\u6790\u5e08\u53ef\u4ee5\u66f4\u597d\u5730\u5224\u65ad\u6a21\u578b\u7684\u6709\u6548\u6027\uff0c\u4ece\u800c\u8fdb\u884c\u76f8\u5e94\u7684\u8c03\u6574\u548c\u4f18\u5316\u3002<\/p>\n<p><strong>\u5982\u4f55\u63d0\u9ad8\u6a21\u578b\u7684R\u00b2\u503c\uff1f<\/strong><br \/>\u63d0\u9ad8\u6a21\u578b\u7684R\u00b2\u503c\u901a\u5e38\u9700\u8981\u6539\u8fdb\u6570\u636e\u8d28\u91cf\u548c\u6a21\u578b\u9009\u62e9\u3002\u53ef\u5c1d\u8bd5\u4ee5\u4e0b\u65b9\u6cd5\uff1a  <\/p>\n<ol>\n<li><strong>\u6570\u636e\u6e05\u6d17<\/strong>\uff1a\u53bb\u9664\u5f02\u5e38\u503c\u548c\u7f3a\u5931\u503c\uff0c\u4ee5\u63d0\u9ad8\u6570\u636e\u7684\u51c6\u786e\u6027\u3002  <\/li>\n<li><strong>\u7279\u5f81\u5de5\u7a0b<\/strong>\uff1a\u6dfb\u52a0\u6216\u4fee\u6539\u7279\u5f81\uff0c\u4f7f\u5176\u66f4\u80fd\u53cd\u6620\u76ee\u6807\u53d8\u91cf\u7684\u53d8\u5316\u3002  <\/li>\n<li><strong>\u9009\u62e9\u5408\u9002\u7684\u6a21\u578b<\/strong>\uff1a\u4f7f\u7528\u9002\u5408\u6570\u636e\u7279\u6027\u7684\u6a21\u578b\uff0c\u5982\u7ebf\u6027\u56de\u5f52\u3001\u51b3\u7b56\u6811\u7b49\u3002  <\/li>\n<li><strong>\u8d85\u53c2\u6570\u8c03\u4f18<\/strong>\uff1a\u901a\u8fc7\u4ea4\u53c9\u9a8c\u8bc1\u548c\u7f51\u683c\u641c\u7d22\u6765\u4f18\u5316\u6a21\u578b\u53c2\u6570\u3002<br \/>\u8fd9\u4e9b\u6b65\u9aa4\u53ef\u4ee5\u5e2e\u52a9\u63d0\u5347R\u00b2\u503c\uff0c\u589e\u5f3a\u6a21\u578b\u7684\u9884\u6d4b\u80fd\u529b\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"Python\u6c42R2\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5e93\u51fd\u6570\u3001\u624b\u52a8\u8ba1\u7b97\u3001\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u7b49\u3002\u5728\u8fd9\u51e0\u79cd\u65b9\u6cd5\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u662f\u901a\u8fc7\u5e93\u51fd\u6570\u548c\u7ebf\u6027\u56de [&hellip;]","protected":false},"author":3,"featured_media":1175228,"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\/1175218"}],"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=1175218"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175218\/revisions"}],"predecessor-version":[{"id":1175230,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1175218\/revisions\/1175230"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1175228"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1175218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1175218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1175218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}