{"id":1110663,"date":"2025-01-08T17:21:44","date_gmt":"2025-01-08T09:21:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1110663.html"},"modified":"2025-01-08T17:21:46","modified_gmt":"2025-01-08T09:21:46","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%bf%9b%e8%a1%8c%e4%ba%8c%e9%a1%b9%e5%bc%8f%e6%8b%9f%e5%90%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1110663.html","title":{"rendered":"\u5982\u4f55\u7528python\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25073426\/576726d0-fde3-4696-ba05-a6e53df8b20e.webp\" alt=\"\u5982\u4f55\u7528python\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408<\/strong><\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a<strong>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u751f\u6210\u6570\u636e\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u62df\u5408\u3001\u4f7f\u7528Scipy\u8fdb\u884c\u62df\u5408\u3001\u53ef\u89c6\u5316\u7ed3\u679c\u3001\u8bc4\u4f30\u62df\u5408\u6548\u679c<\/strong>\u3002\u5176\u4e2d\u4f7f\u7528NumPy\u8fdb\u884c\u62df\u5408\u662f\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u4f7f\u7528NumPy\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408\u7684\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u4e00\u4e9b\u5fc5\u8981\u7684Python\u5e93\uff0c\u5982NumPy\u548cMatplotlib\u3002NumPy\u7528\u4e8e\u6570\u503c\u8ba1\u7b97\uff0c\u800cMatplotlib\u7528\u4e8e\u53ef\u89c6\u5316\u6570\u636e\u548c\u62df\u5408\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u751f\u6210\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u6f14\u793a\u4e8c\u9879\u5f0f\u62df\u5408\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u751f\u6210\u4e00\u4e9b\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528NumPy\u751f\u6210\u4e00\u4e9b\u6a21\u62df\u6570\u636e\uff0c\u5e76\u5728\u8fd9\u4e9b\u6570\u636e\u4e0a\u6dfb\u52a0\u4e00\u4e9b\u566a\u58f0\u4ee5\u6a21\u62df\u771f\u5b9e\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210x\u6570\u636e<\/p>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<h2><strong>\u751f\u6210y\u6570\u636e\uff0c\u5e76\u6dfb\u52a0\u4e00\u4e9b\u566a\u58f0<\/strong><\/h2>\n<p>y = 2 * x2 + 3 * x + 5 + np.random.normal(0, 10, x.shape)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528NumPy\u8fdb\u884c\u62df\u5408<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u591a\u9879\u5f0f\u62df\u5408\uff0c\u5373<code>numpy.polyfit<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u62df\u5408\u4efb\u610f\u9636\u6570\u7684\u591a\u9879\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408<\/p>\n<p>coefficients = np.polyfit(x, y, 2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>coefficients<\/code>\u5305\u542b\u4e86\u4e8c\u9879\u5f0f\u7684\u7cfb\u6570\uff0c\u5373\u591a\u9879\u5f0f<code>2*x^2 + 3*x + 5<\/code>\u7684\u7cfb\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528Scipy\u8fdb\u884c\u62df\u5408<\/h3>\n<\/p>\n<p><p>\u9664\u4e86NumPy\uff0cScipy\u5e93\u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e9b\u66f4\u9ad8\u7ea7\u7684\u62df\u5408\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>scipy.optimize.curve_fit<\/code>\u51fd\u6570\u6765\u62df\u5408\u4efb\u610f\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.optimize import curve_fit<\/p>\n<h2><strong>\u5b9a\u4e49\u4e8c\u9879\u5f0f\u51fd\u6570<\/strong><\/h2>\n<p>def quadratic_function(x, a, b, c):<\/p>\n<p>    return a * x2 + b * x + c<\/p>\n<h2><strong>\u4f7f\u7528Scipy\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408<\/strong><\/h2>\n<p>params, covariance = curve_fit(quadratic_function, x, y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>params<\/code>\u5305\u542b\u4e86\u62df\u5408\u7684\u53c2\u6570\uff0c\u5373\u4e8c\u9879\u5f0f\u7684\u7cfb\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u53ef\u89c6\u5316\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u62df\u5408\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Matplotlib\u5c06\u539f\u59cb\u6570\u636e\u548c\u62df\u5408\u7ed3\u679c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u62df\u5408\u7684\u7cfb\u6570\u751f\u6210\u62df\u5408\u66f2\u7ebf<\/p>\n<p>fitted_y = np.polyval(coefficients, x)<\/p>\n<h2><strong>\u7ed8\u5236\u539f\u59cb\u6570\u636e\u548c\u62df\u5408\u66f2\u7ebf<\/strong><\/h2>\n<p>plt.scatter(x, y, label=&#39;Data&#39;)<\/p>\n<p>plt.plot(x, fitted_y, color=&#39;red&#39;, label=&#39;Fitted Curve&#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\u62df\u5408\u6548\u679c\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b8b\u5dee\u5e73\u65b9\u548c\uff08RSS\uff09\u6765\u8bc4\u4f30\u62df\u5408\u7684\u597d\u574f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u6b8b\u5dee\u5e73\u65b9\u548c<\/p>\n<p>residuals = y - fitted_y<\/p>\n<p>rss = np.sum(residuals2)<\/p>\n<p>print(f&#39;Residual Sum of Squares: {rss}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408\u3002\u6211\u4eec\u9996\u5148\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\uff0c\u7136\u540e\u751f\u6210\u6570\u636e\uff0c\u5e76\u4f7f\u7528NumPy\u548cScipy\u8fdb\u884c\u62df\u5408\u3002\u6700\u540e\uff0c\u6211\u4eec\u53ef\u89c6\u5316\u4e86\u62df\u5408\u7ed3\u679c\uff0c\u5e76\u8bc4\u4f30\u4e86\u62df\u5408\u6548\u679c\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>from scipy.optimize import curve_fit<\/p>\n<h2><strong>\u751f\u6210x\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<h2><strong>\u751f\u6210y\u6570\u636e\uff0c\u5e76\u6dfb\u52a0\u4e00\u4e9b\u566a\u58f0<\/strong><\/h2>\n<p>y = 2 * x2 + 3 * x + 5 + np.random.normal(0, 10, x.shape)<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408<\/strong><\/h2>\n<p>coefficients = np.polyfit(x, y, 2)<\/p>\n<h2><strong>\u4f7f\u7528\u62df\u5408\u7684\u7cfb\u6570\u751f\u6210\u62df\u5408\u66f2\u7ebf<\/strong><\/h2>\n<p>fitted_y = np.polyval(coefficients, x)<\/p>\n<h2><strong>\u4f7f\u7528Scipy\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408<\/strong><\/h2>\n<p>def quadratic_function(x, a, b, c):<\/p>\n<p>    return a * x2 + b * x + c<\/p>\n<p>params, covariance = curve_fit(quadratic_function, x, y)<\/p>\n<h2><strong>\u7ed8\u5236\u539f\u59cb\u6570\u636e\u548c\u62df\u5408\u66f2\u7ebf<\/strong><\/h2>\n<p>plt.scatter(x, y, label=&#39;Data&#39;)<\/p>\n<p>plt.plot(x, fitted_y, color=&#39;red&#39;, label=&#39;Fitted Curve&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u8ba1\u7b97\u6b8b\u5dee\u5e73\u65b9\u548c<\/strong><\/h2>\n<p>residuals = y - fitted_y<\/p>\n<p>rss = np.sum(residuals2)<\/p>\n<p>print(f&#39;Residual Sum of Squares: {rss}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u6e05\u6670\u5730\u770b\u5230\u5982\u4f55\u5728Python\u4e2d\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408\uff0c\u5e76\u901a\u8fc7\u53ef\u89c6\u5316\u548c\u8bc4\u4f30\u6765\u9a8c\u8bc1\u62df\u5408\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4e8c\u9879\u5f0f\u62df\u5408\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u573a\u666f\uff1f<\/strong><br \/>\u4e8c\u9879\u5f0f\u62df\u5408\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u7814\u7a76\u4e2d\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u63cf\u8ff0\u548c\u9884\u6d4b\u6570\u636e\u8d8b\u52bf\u65f6\u3002\u4f8b\u5982\uff0c\u5728\u7ecf\u6d4e\u5b66\u4e2d\uff0c\u4e8c\u9879\u5f0f\u62df\u5408\u53ef\u4ee5\u7528\u6765\u5206\u6790\u80a1\u7968\u4ef7\u683c\u7684\u53d8\u5316\u8d8b\u52bf\uff1b\u5728\u751f\u7269\u7edf\u8ba1\u4e2d\uff0c\u7814\u7a76\u4eba\u5458\u53ef\u80fd\u4f1a\u7528\u5b83\u6765\u5206\u6790\u5b9e\u9a8c\u6570\u636e\u7684\u5206\u5e03\u3002\u6b64\u5916\uff0c\u5de5\u7a0b\u9886\u57df\u7684\u5b9e\u9a8c\u6570\u636e\u5206\u6790\u3001\u6c14\u8c61\u6570\u636e\u7684\u8d8b\u52bf\u9884\u6d4b\u7b49\u4e5f\u5e38\u5e38\u4f7f\u7528\u4e8c\u9879\u5f0f\u62df\u5408\u3002<\/p>\n<p><strong>\u5728\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408\u65f6\uff0c\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u591a\u9879\u5f0f\u9636\u6570\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u591a\u9879\u5f0f\u9636\u6570\u662f\u786e\u4fdd\u62df\u5408\u6548\u679c\u7684\u5173\u952e\u3002\u8fc7\u4f4e\u7684\u9636\u6570\u53ef\u80fd\u65e0\u6cd5\u6355\u6349\u6570\u636e\u4e2d\u7684\u590d\u6742\u8d8b\u52bf\uff0c\u800c\u8fc7\u9ad8\u7684\u9636\u6570\u5219\u53ef\u80fd\u5bfc\u81f4\u8fc7\u62df\u5408\u3002\u901a\u5e38\u5efa\u8bae\u901a\u8fc7\u4ea4\u53c9\u9a8c\u8bc1\u6765\u8bc4\u4f30\u4e0d\u540c\u9636\u6570\u7684\u6a21\u578b\u8868\u73b0\uff0c\u6bd4\u8f83\u5404\u9636\u6570\u6a21\u578b\u7684\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u6216\u51b3\u5b9a\u7cfb\u6570\uff08R\u00b2\uff09\uff0c\u4ece\u800c\u9009\u62e9\u6700\u4f73\u9636\u6570\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408\u65f6\uff0c\u5982\u4f55\u5904\u7406\u6570\u636e\u4e2d\u7684\u5f02\u5e38\u503c\uff1f<\/strong><br \/>\u5904\u7406\u5f02\u5e38\u503c\u662f\u6570\u636e\u9884\u5904\u7406\u4e2d\u7684\u91cd\u8981\u73af\u8282\u3002\u5728\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408\u4e4b\u524d\uff0c\u53ef\u4ee5\u901a\u8fc7\u53ef\u89c6\u5316\u624b\u6bb5\uff08\u5982\u7bb1\u7ebf\u56fe\u6216\u6563\u70b9\u56fe\uff09\u6765\u8bc6\u522b\u5f02\u5e38\u503c\u3002\u5e38\u89c1\u7684\u5904\u7406\u65b9\u6cd5\u5305\u62ec\uff1a\u5220\u9664\u5f02\u5e38\u503c\u3001\u4f7f\u7528\u7a33\u5065\u7edf\u8ba1\u65b9\u6cd5\uff08\u5982\u4e2d\u4f4d\u6570\uff09\u8fdb\u884c\u62df\u5408\uff0c\u6216\u8005\u5bf9\u6570\u636e\u8fdb\u884c\u8f6c\u6362\uff08\u5982\u5bf9\u6570\u53d8\u6362\uff09\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u5229\u7528\u5e93\u5982Pandas\u548cNumPy\u6765\u8f85\u52a9\u5904\u7406\u6570\u636e\uff0c\u63d0\u9ad8\u62df\u5408\u7684\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408 \u5728Python\u4e2d\u8fdb\u884c\u4e8c\u9879\u5f0f\u62df\u5408\u7684\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u5fc5\u8981\u7684\u5e93\u3001\u751f\u6210\u6570\u636e\u3001\u4f7f\u7528N [&hellip;]","protected":false},"author":3,"featured_media":1110669,"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\/1110663"}],"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=1110663"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110663\/revisions"}],"predecessor-version":[{"id":1110672,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1110663\/revisions\/1110672"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1110669"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1110663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1110663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1110663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}