{"id":1141403,"date":"2025-01-08T22:30:47","date_gmt":"2025-01-08T14:30:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1141403.html"},"modified":"2025-01-08T22:30:49","modified_gmt":"2025-01-08T14:30:49","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e4%b8%a4%e4%b8%aa%e5%8f%98%e9%87%8f%e7%9a%84%e5%a4%8d%e5%b7%a5%e7%ba%bf%e6%80%a7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1141403.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u7684\u590d\u5de5\u7ebf\u6027"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104129\/be56c5f4-a2a4-4bd7-9bd9-454c8d4419ed.webp\" alt=\"python\u4e2d\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u7684\u590d\u5de5\u7ebf\u6027\" \/><\/p>\n<h2><strong>Python\u4e2d\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u7684\u590d\u5de5\u7ebf\u6027<\/strong><\/h2>\n<p><p>\u5728Python\u4e2d\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u7684\u590d\u5de5\u7ebf\u6027\u53ef\u4ee5\u901a\u8fc7<strong>\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u3001\u7ed8\u5236\u6563\u70b9\u56fe\u3001\u4f7f\u7528\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong>\u7b49\u65b9\u6cd5\u3002<strong>\u76f8\u5173\u7cfb\u6570<\/strong>\u662f\u4e00\u4e2a\u6570\u503c\uff0c\u7528\u6765\u8861\u91cf\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7ebf\u6027\u5173\u7cfb\u7684\u5f3a\u5ea6\u548c\u65b9\u5411\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u76f8\u5173\u7cfb\u6570<\/h2>\n<\/p>\n<p><p>\u76f8\u5173\u7cfb\u6570\uff08Correlation Coefficient\uff09\u662f\u7edf\u8ba1\u5b66\u4e2d\u7528\u6765\u8861\u91cf\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7ebf\u6027\u5173\u7cfb\u7684\u5f3a\u5ea6\u548c\u65b9\u5411\u7684\u6570\u503c\uff0c\u901a\u5e38\u7528\u7b26\u53f7\u201cr\u201d\u8868\u793a\u3002\u76f8\u5173\u7cfb\u6570\u7684\u53d6\u503c\u8303\u56f4\u4ece-1\u52301\uff0c\u5176\u4e2d\uff1a<\/p>\n<\/p>\n<ul>\n<li>r = 1 \u8868\u793a\u5b8c\u5168\u6b63\u76f8\u5173\uff0c\u5373\u4e24\u4e2a\u53d8\u91cf\u5448\u73b0\u5b8c\u5168\u76f8\u540c\u7684\u53d8\u5316\u8d8b\u52bf\uff1b<\/li>\n<li>r = -1 \u8868\u793a\u5b8c\u5168\u8d1f\u76f8\u5173\uff0c\u5373\u4e24\u4e2a\u53d8\u91cf\u5448\u73b0\u5b8c\u5168\u76f8\u53cd\u7684\u53d8\u5316\u8d8b\u52bf\uff1b<\/li>\n<li>r = 0 \u8868\u793a\u6ca1\u6709\u7ebf\u6027\u5173\u7cfb\u3002<\/li>\n<\/ul>\n<p><p>Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\uff0c\u5982NumPy\u3001Pandas\u548cSciPy\u3002<\/p>\n<\/p>\n<p><h3>NumPy\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u7684\u4e00\u4e2a\u57fa\u7840\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u7684\u51fd\u6570<code>numpy.corrcoef<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528NumPy\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u53d8\u91cf\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>x = np.array([1, 2, 3, 4, 5])<\/p>\n<p>y = np.array([2, 4, 6, 8, 10])<\/p>\n<h2><strong>\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u77e9\u9635<\/strong><\/h2>\n<p>correlation_matrix = np.corrcoef(x, y)<\/p>\n<h2><strong>\u83b7\u53d6\u76f8\u5173\u7cfb\u6570<\/strong><\/h2>\n<p>correlation_coefficient = correlation_matrix[0, 1]<\/p>\n<p>print(&quot;\u76f8\u5173\u7cfb\u6570: &quot;, correlation_coefficient)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u793a\u4f8b\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0cNumPy\u63d0\u4f9b\u7684<code>corrcoef<\/code>\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u76f8\u5173\u7cfb\u6570\u77e9\u9635\uff0c\u5176\u4e2d[0, 1]\u4f4d\u7f6e\u7684\u503c\u5c31\u662f\u4e24\u4e2a\u53d8\u91cf\u7684\u76f8\u5173\u7cfb\u6570\u3002<\/p>\n<\/p>\n<p><h3>Pandas\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u975e\u5e38\u6d41\u884c\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u65b9\u4fbf\u7684\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u7684\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Pandas\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>data = {&#39;x&#39;: [1, 2, 3, 4, 5], &#39;y&#39;: [2, 4, 6, 8, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570<\/strong><\/h2>\n<p>correlation_coefficient = df[&#39;x&#39;].corr(df[&#39;y&#39;])<\/p>\n<p>print(&quot;\u76f8\u5173\u7cfb\u6570: &quot;, correlation_coefficient)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>SciPy\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570<\/h3>\n<\/p>\n<p><p>SciPy\u662fPython\u4e2d\u7684\u4e00\u4e2a\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7edf\u8ba1\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528SciPy\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.stats import pearsonr<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u53d8\u91cf\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 4, 6, 8, 10]<\/p>\n<h2><strong>\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u548cP\u503c<\/strong><\/h2>\n<p>correlation_coefficient, p_value = pearsonr(x, y)<\/p>\n<p>print(&quot;\u76f8\u5173\u7cfb\u6570: &quot;, correlation_coefficient)<\/p>\n<p>print(&quot;P\u503c: &quot;, p_value)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>pearsonr<\/code>\u51fd\u6570\u4e0d\u4ec5\u8fd4\u56de\u4e86\u76f8\u5173\u7cfb\u6570\uff0c\u8fd8\u8fd4\u56de\u4e86P\u503c\u3002P\u503c\u7528\u4e8e\u68c0\u9a8c\u76f8\u5173\u7cfb\u6570\u7684\u663e\u8457\u6027\uff0c\u901a\u5e38P\u503c\u5c0f\u4e8e0.05\u8868\u793a\u76f8\u5173\u7cfb\u6570\u663e\u8457\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u7ed8\u5236\u6563\u70b9\u56fe<\/h2>\n<\/p>\n<p><p>\u6563\u70b9\u56fe\uff08Scatter Plot\uff09\u662f\u4e00\u79cd\u7528\u6765\u663e\u793a\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u5173\u7cfb\u7684\u56fe\u8868\u3002\u5728\u6563\u70b9\u56fe\u4e2d\uff0c\u6bcf\u4e2a\u70b9\u8868\u793a\u4e00\u4e2a\u6570\u636e\u6837\u672c\u7684\u4e24\u4e2a\u53d8\u91cf\u7684\u53d6\u503c\u3002\u901a\u8fc7\u89c2\u5bdf\u6563\u70b9\u56fe\u7684\u5206\u5e03\u5f62\u72b6\uff0c\u53ef\u4ee5\u521d\u6b65\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u662f\u5426\u5b58\u5728\u7ebf\u6027\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Matplotlib\u7ed8\u5236\u6563\u70b9\u56fe<\/h3>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u7ed8\u5236\u6563\u70b9\u56fe\u7684\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Matplotlib\u7ed8\u5236\u6563\u70b9\u56fe\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u53d8\u91cf\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 4, 6, 8, 10]<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>plt.scatter(x, y)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;Scatter Plot of x and y&#39;)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;y&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u4ee5\u770b\u51fa\uff0c\u4f7f\u7528Matplotlib\u7ed8\u5236\u6563\u70b9\u56fe\u975e\u5e38\u7b80\u5355\u3002\u901a\u8fc7\u89c2\u5bdf\u751f\u6210\u7684\u6563\u70b9\u56fe\uff0c\u5982\u679c\u70b9\u7684\u5206\u5e03\u5927\u81f4\u6cbf\u7740\u4e00\u6761\u76f4\u7ebf\uff0c\u5c31\u53ef\u4ee5\u8ba4\u4e3a\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u5b58\u5728\u7ebf\u6027\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Seaborn\u7ed8\u5236\u6563\u70b9\u56fe<\/h3>\n<\/p>\n<p><p>Seaborn\u662f\u57fa\u4e8eMatplotlib\u7684\u9ad8\u7ea7\u7ed8\u56fe\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u7f8e\u89c2\u548c\u7b80\u6d01\u7684\u7ed8\u56fe\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Seaborn\u7ed8\u5236\u6563\u70b9\u56fe\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import seaborn as sns<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>data = {&#39;x&#39;: [1, 2, 3, 4, 5], &#39;y&#39;: [2, 4, 6, 8, 10]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u7ed8\u5236\u6563\u70b9\u56fe<\/strong><\/h2>\n<p>sns.scatterplot(x=&#39;x&#39;, y=&#39;y&#39;, data=df)<\/p>\n<h2><strong>\u6dfb\u52a0\u6807\u9898\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>plt.title(&#39;Scatter Plot of x and y&#39;)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;y&#39;)<\/p>\n<h2><strong>\u663e\u793a\u56fe\u8868<\/strong><\/h2>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Seaborn\u7684\u7ed8\u56fe\u51fd\u6570\u6bd4Matplotlib\u66f4\u52a0\u7b80\u6d01\uff0c\u5e76\u4e14\u9ed8\u8ba4\u6837\u5f0f\u66f4\u52a0\u7f8e\u89c2\u3002\u901a\u8fc7\u89c2\u5bdf\u751f\u6210\u7684\u6563\u70b9\u56fe\uff0c\u540c\u6837\u53ef\u4ee5\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/h2>\n<\/p>\n<p><p>\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u662f\u4e00\u79cd\u7edf\u8ba1\u65b9\u6cd5\uff0c\u7528\u4e8e\u63cf\u8ff0\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\u3002\u901a\u8fc7\u62df\u5408\u4e00\u6761\u76f4\u7ebf\uff0c\u53ef\u4ee5\u91cf\u5316\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002Python\u4e2d\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u5e93\u6765\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\u5206\u6790\uff0c\u5982SciPy\u3001Statsmodels\u548cScikit-learn\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528SciPy\u8fdb\u884c\u7ebf\u6027\u56de\u5f52<\/h3>\n<\/p>\n<p><p>SciPy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u6027\u56de\u5f52\u51fd\u6570<code>linregress<\/code>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528SciPy\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.stats import linregress<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u53d8\u91cf\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 4, 6, 8, 10]<\/p>\n<h2><strong>\u8fdb\u884c\u7ebf\u6027\u56de\u5f52<\/strong><\/h2>\n<p>slope, intercept, r_value, p_value, std_err = linregress(x, y)<\/p>\n<p>print(&quot;\u659c\u7387: &quot;, slope)<\/p>\n<p>print(&quot;\u622a\u8ddd: &quot;, intercept)<\/p>\n<p>print(&quot;\u76f8\u5173\u7cfb\u6570: &quot;, r_value)<\/p>\n<p>print(&quot;P\u503c: &quot;, p_value)<\/p>\n<p>print(&quot;\u6807\u51c6\u8bef\u5dee: &quot;, std_err)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>linregress<\/code>\u51fd\u6570\u8fd4\u56de\u4e86\u7ebf\u6027\u56de\u5f52\u7684\u591a\u4e2a\u7edf\u8ba1\u91cf\uff0c\u5305\u62ec\u659c\u7387\u3001\u622a\u8ddd\u3001\u76f8\u5173\u7cfb\u6570\u3001P\u503c\u548c\u6807\u51c6\u8bef\u5dee\u3002\u901a\u8fc7\u8fd9\u4e9b\u7edf\u8ba1\u91cf\uff0c\u53ef\u4ee5\u5168\u9762\u4e86\u89e3\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Statsmodels\u8fdb\u884c\u7ebf\u6027\u56de\u5f52<\/h3>\n<\/p>\n<p><p>Statsmodels\u662fPython\u4e2d\u7684\u4e00\u4e2a\u7edf\u8ba1\u5efa\u6a21\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u52a0\u4e30\u5bcc\u7684\u7ebf\u6027\u56de\u5f52\u5206\u6790\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Statsmodels\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import statsmodels.api as sm<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u53d8\u91cf\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>x = [1, 2, 3, 4, 5]<\/p>\n<p>y = [2, 4, 6, 8, 10]<\/p>\n<h2><strong>\u6dfb\u52a0\u5e38\u6570\u9879<\/strong><\/h2>\n<p>x = sm.add_constant(x)<\/p>\n<h2><strong>\u8fdb\u884c\u7ebf\u6027\u56de\u5f52<\/strong><\/h2>\n<p>model = sm.OLS(y, x).fit()<\/p>\n<h2><strong>\u6253\u5370\u56de\u5f52\u7ed3\u679c<\/strong><\/h2>\n<p>print(model.summary())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Statsmodels\u7684<code>OLS<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u7ebf\u6027\u56de\u5f52\u7684\u8be6\u7ec6\u7ed3\u679c\u548c\u7edf\u8ba1\u68c0\u9a8c\uff0c\u901a\u8fc7<code>summary<\/code>\u51fd\u6570\u53ef\u4ee5\u6253\u5370\u56de\u5f52\u7ed3\u679c\u7684\u8be6\u7ec6\u62a5\u544a\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528Scikit-learn\u8fdb\u884c\u7ebf\u6027\u56de\u5f52<\/h3>\n<\/p>\n<p><p>Scikit-learn\u662fPython\u4e2d\u7684\u4e00\u4e2a<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u56de\u5f52\u5206\u6790\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Scikit-learn\u8fdb\u884c\u7ebf\u6027\u56de\u5f52\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import LinearRegression<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u53d8\u91cf\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>x = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)<\/p>\n<p>y = np.array([2, 4, 6, 8, 10])<\/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>\u83b7\u53d6\u56de\u5f52\u7cfb\u6570<\/strong><\/h2>\n<p>slope = model.coef_[0]<\/p>\n<p>intercept = model.intercept_<\/p>\n<p>print(&quot;\u659c\u7387: &quot;, slope)<\/p>\n<p>print(&quot;\u622a\u8ddd: &quot;, intercept)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Scikit-learn\u7684<code>LinearRegression<\/code>\u7c7b\u63d0\u4f9b\u4e86\u7b80\u5355\u548c\u9ad8\u6548\u7684\u7ebf\u6027\u56de\u5f52\u5206\u6790\u529f\u80fd\u3002\u901a\u8fc7\u62df\u5408\u6a21\u578b\uff0c\u53ef\u4ee5\u5f97\u5230\u56de\u5f52\u7cfb\u6570\uff08\u5373\u659c\u7387\uff09\u548c\u622a\u8ddd\uff0c\u4ece\u800c\u91cf\u5316\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u7684\u590d\u5de5\u7ebf\u6027\u53ef\u4ee5\u901a\u8fc7<strong>\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u3001\u7ed8\u5236\u6563\u70b9\u56fe\u3001\u4f7f\u7528\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong>\u7b49\u65b9\u6cd5\u3002\u76f8\u5173\u7cfb\u6570\u53ef\u4ee5\u5b9a\u91cf\u8861\u91cf\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7ebf\u6027\u5173\u7cfb\u7684\u5f3a\u5ea6\u548c\u65b9\u5411\uff1b\u6563\u70b9\u56fe\u53ef\u4ee5\u901a\u8fc7\u53ef\u89c6\u5316\u624b\u6bb5\u521d\u6b65\u5224\u65ad\u7ebf\u6027\u5173\u7cfb\uff1b\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u53ef\u4ee5\u91cf\u5316\u548c\u63cf\u8ff0\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\u3002\u901a\u8fc7\u7ed3\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5168\u9762\u3001\u51c6\u786e\u5730\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u590d\u5de5\u7ebf\u6027\u5173\u7cfb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u662f\u5426\u5b58\u5728\u7ebf\u6027\u5173\u7cfb\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u6563\u70b9\u56fe\u6765\u76f4\u89c2\u5730\u89c2\u5bdf\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5982\u679c\u6563\u70b9\u56fe\u5448\u73b0\u51fa\u4e00\u6761\u76f4\u7ebf\u7684\u8d8b\u52bf\uff0c\u8bf4\u660e\u8fd9\u4e24\u4e2a\u53d8\u91cf\u53ef\u80fd\u5b58\u5728\u7ebf\u6027\u5173\u7cfb\u3002\u6b64\u5916\uff0c\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u4e5f\u662f\u4e00\u79cd\u6709\u6548\u7684\u65b9\u6cd5\uff0c\u503c\u63a5\u8fd11\u6216-1\u901a\u5e38\u8868\u793a\u5f3a\u7ebf\u6027\u5173\u7cfb\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u4f7f\u7528\u7edf\u8ba1\u68c0\u9a8c\u6765\u9a8c\u8bc1\u7ebf\u6027\u5173\u7cfb\uff1f<\/strong><br \/>\u5229\u7528\u7ebf\u6027\u56de\u5f52\u5206\u6790\u53ef\u4ee5\u6709\u6548\u5730\u68c0\u9a8c\u4e24\u4e2a\u53d8\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\u3002\u901a\u8fc7\u4f7f\u7528<code>scipy.stats.linregress<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u83b7\u5f97\u659c\u7387\u3001\u622a\u8ddd\u4ee5\u53ca\u76f8\u5173\u7cfb\u6570\uff0c\u4ece\u800c\u5224\u65ad\u53d8\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\u662f\u5426\u663e\u8457\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u65b9\u6cd5\u6765\u63a2\u6d4b\u7ebf\u6027\u5173\u7cfb\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u673a\u5668\u5b66\u4e60\u4e2d\u7684\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u80fd\u591f\u5e2e\u52a9\u5224\u65ad\u53d8\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\u3002\u901a\u8fc7\u8bad\u7ec3\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u5e76\u8bc4\u4f30\u5176\u6027\u80fd\uff0c\u53ef\u4ee5\u4e86\u89e3\u81ea\u53d8\u91cf\u5bf9\u56e0\u53d8\u91cf\u7684\u5f71\u54cd\u7a0b\u5ea6\uff0c\u8fdb\u800c\u5224\u65ad\u7ebf\u6027\u5173\u7cfb\u7684\u5b58\u5728\u4e0e\u5f3a\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u5982\u4f55\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u7684\u590d\u5de5\u7ebf\u6027 \u5728Python\u4e2d\u5224\u65ad\u4e24\u4e2a\u53d8\u91cf\u7684\u590d\u5de5\u7ebf\u6027\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u76f8\u5173\u7cfb\u6570\u3001\u7ed8\u5236\u6563\u70b9 [&hellip;]","protected":false},"author":3,"featured_media":1141409,"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\/1141403"}],"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=1141403"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141403\/revisions"}],"predecessor-version":[{"id":1141412,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1141403\/revisions\/1141412"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1141409"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1141403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1141403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1141403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}