{"id":957409,"date":"2024-12-27T02:59:49","date_gmt":"2024-12-26T18:59:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/957409.html"},"modified":"2024-12-27T02:59:51","modified_gmt":"2024-12-26T18:59:51","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e7%ba%bf%e6%80%a7%e6%8b%9f%e5%90%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/957409.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u7ebf\u6027\u62df\u5408"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100728\/5a5b69ce-fb45-4d48-83b7-8ae1df639699.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u7ebf\u6027\u62df\u5408\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5b9e\u73b0\u7ebf\u6027\u62df\u5408\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u76f8\u5173\u5e93\u3001\u51c6\u5907\u6570\u636e\u3001\u4f7f\u7528\u62df\u5408\u51fd\u6570\u3001\u8f93\u51fa\u7ed3\u679c\u3002\u9996\u5148\uff0c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u5982NumPy\u548cMatplotlib\uff0c\u51c6\u5907\u597d\u6570\u636e\u96c6\u3002\u63a5\u7740\uff0c\u4f7f\u7528SciPy\u5e93\u4e2d\u7684<code>linregress<\/code>\u51fd\u6570\u6216NumPy\u7684<code>polyfit<\/code>\u51fd\u6570\u6765\u6267\u884c\u7ebf\u6027\u62df\u5408\u3002\u6700\u540e\uff0c\u901a\u8fc7Matplotlib\u5e93\u5c06\u62df\u5408\u7ed3\u679c\u53ef\u89c6\u5316\uff0c\u5e76\u6839\u636eR-squared\u503c\u8bc4\u4f30\u62df\u5408\u6548\u679c\u3002<\/strong>  <\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u6df1\u5165\u5730\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7ebf\u6027\u62df\u5408\uff0c\u4e0b\u9762\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e00\u8fc7\u7a0b\u7684\u6bcf\u4e2a\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7ebf\u6027\u62df\u5408\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5bfc\u5165Python\u4e2d\u5e38\u7528\u7684\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u53ef\u89c6\u5316\u5e93\u3002\u5e38\u7528\u7684\u5e93\u5305\u62ecNumPy\u3001SciPy\u548cMatplotlib\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>NumPy<\/strong>\uff1a\u7528\u4e8e\u9ad8\u6548\u5730\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97\u3002<\/li>\n<li><strong>SciPy<\/strong>\uff1a\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u6570\u5b66\u3001\u79d1\u5b66\u548c\u5de5\u7a0b\u6a21\u5757\uff0c\u5305\u62ec\u7ebf\u6027\u56de\u5f52\u6a21\u5757\u3002<\/li>\n<li><strong>Matplotlib<\/strong>\uff1a\u7528\u4e8e\u521b\u5efa\u9759\u6001\u3001\u52a8\u753b\u548c\u4ea4\u4e92\u5f0f\u53ef\u89c6\u5316\u56fe\u8868\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>from scipy import stats<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u51c6\u5907\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u7ebf\u6027\u62df\u5408\u4e4b\u524d\uff0c\u9700\u8981\u51c6\u5907\u597d\u6570\u636e\u3002\u6570\u636e\u53ef\u4ee5\u662f\u4ece\u5b9e\u9a8c\u3001\u7814\u7a76\u4e2d\u6536\u96c6\u7684\u6570\u636e\uff0c\u4e5f\u53ef\u4ee5\u662f\u968f\u673a\u751f\u6210\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u793a\u4f8b\u6570\u636e<\/p>\n<p>x = np.array([1, 2, 3, 4, 5])<\/p>\n<p>y = np.array([2, 4, 5, 4, 5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528SciPy\u8fdb\u884c\u7ebf\u6027\u62df\u5408<\/h3>\n<\/p>\n<p><p>SciPy\u5e93\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u51fd\u6570<code>linregress<\/code>\uff0c\u7528\u4e8e\u8ba1\u7b97\u4e00\u5143\u7ebf\u6027\u56de\u5f52\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>slope<\/strong>\uff1a\u7ebf\u6027\u56de\u5f52\u65b9\u7a0b\u7684\u659c\u7387\u3002<\/li>\n<li><strong>intercept<\/strong>\uff1a\u7ebf\u6027\u56de\u5f52\u65b9\u7a0b\u7684\u622a\u8ddd\u3002<\/li>\n<li><strong>r_value<\/strong>\uff1a\u76f8\u5173\u7cfb\u6570\u7684\u5e73\u65b9\uff0c\u79f0\u4e3aR-squared\uff0c\u53cd\u6620\u6a21\u578b\u7684\u62df\u5408\u4f18\u5ea6\u3002<\/li>\n<li><strong>p_value<\/strong>\uff1a\u68c0\u9a8c\u659c\u7387\u4e3a\u96f6\u7684\u5047\u8bbe\u7684p\u503c\u3002<\/li>\n<li><strong>std_err<\/strong>\uff1a\u659c\u7387\u7684\u6807\u51c6\u8bef\u5dee\u3002<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u7ebf\u6027\u62df\u5408<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u4e2a\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528NumPy\u7684<code>polyfit<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">coefficients = np.polyfit(x, y, 1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ul>\n<li><strong>coefficients<\/strong>\uff1a\u8fd4\u56de\u4e00\u4e2a\u6570\u7ec4\uff0c\u5176\u4e2d\u7b2c\u4e00\u4e2a\u5143\u7d20\u662f\u659c\u7387\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u662f\u622a\u8ddd\u3002<\/li>\n<\/ul>\n<p><h3>\u4e94\u3001\u7ed8\u5236\u62df\u5408\u76f4\u7ebf<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Matplotlib\u7ed8\u5236\u539f\u59cb\u6570\u636e\u548c\u62df\u5408\u76f4\u7ebf\uff0c\u4ee5\u53ef\u89c6\u5316\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">plt.scatter(x, y, color=&#39;red&#39;, label=&#39;Data Points&#39;)<\/p>\n<p>plt.plot(x, slope*x + intercept, color=&#39;blue&#39;, label=&#39;Fitted Line&#39;)<\/p>\n<p>plt.xlabel(&#39;X&#39;)<\/p>\n<p>plt.ylabel(&#39;Y&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8bc4\u4f30\u62df\u5408\u6548\u679c<\/h3>\n<\/p>\n<p><p>\u8bc4\u4f30\u7ebf\u6027\u62df\u5408\u7684\u6548\u679c\u901a\u5e38\u4f9d\u8d56\u4e8eR-squared\u503c\u3002R-squared\u503c\u63a5\u8fd11\u8868\u793a\u6a21\u578b\u4e0e\u6570\u636e\u7684\u62df\u5408\u5ea6\u8f83\u597d\u3002\u4e5f\u53ef\u4ee5\u901a\u8fc7\u89c2\u5bdf\u62df\u5408\u7ebf\u4e0e\u6570\u636e\u70b9\u7684\u63a5\u8fd1\u7a0b\u5ea6\u6765\u8fdb\u884c\u76f4\u89c2\u8bc4\u4f30\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406\u591a\u7ef4\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u7ebf\u6027\u62df\u5408\u4e2d\uff0c\u901a\u5e38\u5904\u7406\u7684\u662f\u4e00\u7ef4\u6570\u636e\uff0c\u4f46\u5728\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u53ef\u4ee5\u662f\u591a\u7ef4\u7684\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u5143\u7ebf\u6027\u56de\u5f52\u6765\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u591a\u5143\u7ebf\u6027\u56de\u5f52\u7684\u5b9e\u73b0\u53ef\u4ee5\u901a\u8fc7Scikit-learn\u5e93\u6765\u5b8c\u6210\u3002Scikit-learn\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e93\uff0c\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u591a\u5143\u7ebf\u6027\u56de\u5f52\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import LinearRegression<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u591a\u7ef4\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([[1, 2], [2, 3], [3, 4], [4, 5]])<\/p>\n<p>y = np.array([2, 3, 4, 5])<\/p>\n<h2><strong>\u521b\u5efa\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<p>model.fit(X, y)<\/p>\n<h2><strong>\u8f93\u51fa\u56de\u5f52\u7cfb\u6570\u548c\u622a\u8ddd<\/strong><\/h2>\n<p>print(&#39;Coefficients:&#39;, model.coef_)<\/p>\n<p>print(&#39;Intercept:&#39;, model.intercept_)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5904\u7406\u975e\u7ebf\u6027\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u53ef\u80fd\u4e0d\u9002\u5408\u7ebf\u6027\u62df\u5408\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u5c06\u6570\u636e\u8fdb\u884c\u53d8\u6362\uff08\u5982\u5bf9\u6570\u53d8\u6362\uff09\u6216\u4f7f\u7528\u975e\u7ebf\u6027\u62df\u5408\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u7ebf\u6027\u62df\u5408\u662f\u6570\u636e\u5206\u6790\u4e2d\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7Python\u4e2d\u7684NumPy\u3001SciPy\u548cMatplotlib\u7b49\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5b9e\u73b0\u7ebf\u6027\u62df\u5408\uff0c\u5e76\u5bf9\u7ed3\u679c\u8fdb\u884c\u53ef\u89c6\u5316\u548c\u8bc4\u4f30\u3002\u5728\u5904\u7406\u590d\u6742\u6216\u591a\u7ef4\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u501f\u52a9Scikit-learn\u5e93\u8fdb\u884c\u591a\u5143\u7ebf\u6027\u56de\u5f52\u3002\u7ebf\u6027\u62df\u5408\u4e0d\u4ec5\u80fd\u591f\u5e2e\u52a9\u6211\u4eec\u7406\u89e3\u6570\u636e\u7684\u8d8b\u52bf\uff0c\u8fd8\u80fd\u7528\u4e8e\u9884\u6d4b\u548c\u51b3\u7b56\u3002\u901a\u8fc7\u4e0d\u65ad\u7ec3\u4e60\u548c\u5e94\u7528\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u638c\u63e1\u7ebf\u6027\u62df\u5408\u6280\u672f\uff0c\u4e3a\u6570\u636e\u5206\u6790\u63d0\u4f9b\u6709\u529b\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u7ebf\u6027\u62df\u5408\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8fdb\u884c\u7ebf\u6027\u62df\u5408\u901a\u5e38\u4f7f\u7528NumPy\u548cMatplotlib\u5e93\u3002\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u8fd9\u4e24\u4e2a\u5e93\u3002\u63a5\u7740\uff0c\u51c6\u5907\u597d\u60a8\u7684\u6570\u636e\u96c6\uff0c\u5305\u62ec\u81ea\u53d8\u91cf\u548c\u56e0\u53d8\u91cf\u3002\u4f7f\u7528NumPy\u7684<code>polyfit<\/code>\u51fd\u6570\u53ef\u4ee5\u8ba1\u7b97\u6700\u4f73\u62df\u5408\u7ebf\u7684\u7cfb\u6570\uff0c\u7136\u540e\u5229\u7528Matplotlib\u7ed8\u5236\u6570\u636e\u70b9\u548c\u62df\u5408\u7ebf\uff0c\u4ee5\u4fbf\u76f4\u89c2\u5c55\u793a\u7ed3\u679c\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u7ebf\u6027\u62df\u5408\uff1f<\/strong><br \/>Python\u4e2d\u5e38\u7528\u7684\u7ebf\u6027\u62df\u5408\u5e93\u5305\u62ecNumPy\u3001SciPy\u548cstatsmodels\u3002NumPy\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u591a\u9879\u5f0f\u62df\u5408\u529f\u80fd\uff0cSciPy\u5219\u53ef\u4ee5\u8fdb\u884c\u66f4\u590d\u6742\u7684\u7ebf\u6027\u56de\u5f52\u5206\u6790\uff0c\u800cstatsmodels\u5219\u63d0\u4f9b\u4e86\u66f4\u4e3a\u8be6\u5c3d\u7684\u7edf\u8ba1\u5206\u6790\u548c\u7ed3\u679c\u89e3\u8bfb\u529f\u80fd\uff0c\u9002\u5408\u9700\u8981\u6df1\u5165\u5206\u6790\u7684\u7528\u6237\u3002<\/p>\n<p><strong>\u7ebf\u6027\u62df\u5408\u7684\u7ed3\u679c\u5982\u4f55\u8bc4\u4f30\uff1f<\/strong><br \/>\u7ebf\u6027\u62df\u5408\u7684\u7ed3\u679c\u53ef\u4ee5\u901a\u8fc7R\u5e73\u65b9\u503c\u3001\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u7b49\u6307\u6807\u8fdb\u884c\u8bc4\u4f30\u3002R\u5e73\u65b9\u503c\u53cd\u6620\u4e86\u6a21\u578b\u5bf9\u6570\u636e\u7684\u62df\u5408\u7a0b\u5ea6\uff0c\u503c\u8d8a\u63a5\u8fd11\u8868\u793a\u62df\u5408\u6548\u679c\u8d8a\u597d\uff1b\u5747\u65b9\u8bef\u5dee\u5219\u662f\u9884\u6d4b\u503c\u4e0e\u5b9e\u9645\u503c\u4e4b\u95f4\u5dee\u5f02\u7684\u5e73\u65b9\u7684\u5e73\u5747\u503c\uff0c\u6570\u503c\u8d8a\u5c0f\u8868\u793a\u9884\u6d4b\u8d8a\u51c6\u786e\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6b8b\u5dee\u5206\u6790\u6765\u5224\u65ad\u6a21\u578b\u7684\u9002\u7528\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9e\u73b0\u7ebf\u6027\u62df\u5408\u7684\u6838\u5fc3\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u76f8\u5173\u5e93\u3001\u51c6\u5907\u6570\u636e\u3001\u4f7f\u7528\u62df\u5408\u51fd\u6570\u3001\u8f93\u51fa\u7ed3\u679c\u3002\u9996\u5148\uff0c\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u5982 [&hellip;]","protected":false},"author":3,"featured_media":957411,"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\/957409"}],"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=957409"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/957409\/revisions"}],"predecessor-version":[{"id":957412,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/957409\/revisions\/957412"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/957411"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=957409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=957409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=957409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}