{"id":1075318,"date":"2025-01-08T11:43:00","date_gmt":"2025-01-08T03:43:00","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1075318.html"},"modified":"2025-01-08T11:43:03","modified_gmt":"2025-01-08T03:43:03","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%8b%9f%e5%90%88%e5%8f%8c%e6%8c%87%e6%95%b0%e5%87%bd%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1075318.html","title":{"rendered":"\u5982\u4f55\u7528Python\u62df\u5408\u53cc\u6307\u6570\u51fd\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24180452\/86f0d20b-a95d-4e56-befa-3684d63b1ee2.webp\" alt=\"\u5982\u4f55\u7528Python\u62df\u5408\u53cc\u6307\u6570\u51fd\u6570\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7528Python\u62df\u5408\u53cc\u6307\u6570\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p><strong>\u7528Python\u62df\u5408\u53cc\u6307\u6570\u51fd\u6570\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u6240\u9700\u5e93\u3001\u5b9a\u4e49\u53cc\u6307\u6570\u51fd\u6570\u3001\u751f\u6210\u6216\u5bfc\u5165\u6570\u636e\u3001\u4f7f\u7528\u66f2\u7ebf\u62df\u5408\u51fd\u6570\u62df\u5408\u6570\u636e\u3001\u8bc4\u4f30\u62df\u5408\u7ed3\u679c<\/strong>\u3002\u5728\u8fd9\u4e9b\u6b65\u9aa4\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u62df\u5408\u65b9\u6cd5\u548c\u521d\u59cb\u53c2\u6570\u975e\u5e38\u5173\u952e\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u8fdb\u884c\u6bcf\u4e00\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5bfc\u5165\u6240\u9700\u5e93<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8fdb\u884c\u6570\u636e\u62df\u5408\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528<code>numpy<\/code>\u548c<code>scipy<\/code>\u5e93\u3002<code>numpy<\/code>\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u6570\u636e\uff0c\u800c<code>scipy<\/code>\u4e2d\u7684<code>curve_fit<\/code>\u51fd\u6570\u662f\u5b9e\u73b0\u62df\u5408\u7684\u5173\u952e\u5de5\u5177\u3002\u6b64\u5916\uff0c<code>matplotlib<\/code>\u5e93\u5e38\u7528\u4e8e\u53ef\u89c6\u5316\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>from scipy.optimize import curve_fit<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9a\u4e49\u53cc\u6307\u6570\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u53cc\u6307\u6570\u51fd\u6570\u901a\u5e38\u8868\u793a\u4e3a\uff1a<code>f(x) = A * exp(B * x) + C * exp(D * x)<\/code>\uff0c\u5176\u4e2d<code>A<\/code>\u3001<code>B<\/code>\u3001<code>C<\/code>\u3001<code>D<\/code>\u662f\u5f85\u62df\u5408\u7684\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def double_exponential(x, A, B, C, D):<\/p>\n<p>    return A * np.exp(B * x) + C * np.exp(D * x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u751f\u6210\u6216\u5bfc\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u6f14\u793a\uff0c\u6211\u4eec\u53ef\u4ee5\u751f\u6210\u4e00\u4e9b\u7b26\u5408\u53cc\u6307\u6570\u51fd\u6570\u7684\u6570\u636e\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u53ef\u4ee5\u6765\u81ea\u5b9e\u9a8c\u6d4b\u91cf\u3001\u89c2\u6d4b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u6570\u636e<\/p>\n<p>x_data = np.linspace(0, 10, 100)<\/p>\n<p>A_true, B_true, C_true, D_true = 2.5, -1.3, 0.7, -0.1<\/p>\n<p>y_data = double_exponential(x_data, A_true, B_true, C_true, D_true)<\/p>\n<h2><strong>\u6dfb\u52a0\u566a\u58f0<\/strong><\/h2>\n<p>noise = 0.2 * np.random.normal(size=x_data.size)<\/p>\n<p>y_data = y_data + noise<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u66f2\u7ebf\u62df\u5408\u51fd\u6570\u62df\u5408\u6570\u636e<\/h3>\n<\/p>\n<p><p><code>curve_fit<\/code>\u51fd\u6570\u662f<code>scipy.optimize<\/code>\u6a21\u5757\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\uff0c\u7528\u4e8e\u975e\u7ebf\u6027\u6700\u5c0f\u4e8c\u4e58\u6cd5\u62df\u5408\u3002\u9700\u8981\u63d0\u4f9b\u62df\u5408\u51fd\u6570\u3001x\u6570\u636e\u3001y\u6570\u636e\u4ee5\u53ca\u521d\u59cb\u53c2\u6570\u4f30\u8ba1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521d\u59cb\u53c2\u6570\u4f30\u8ba1<\/p>\n<p>initial_guess = [1.0, -1.0, 1.0, -0.1]<\/p>\n<h2><strong>\u62df\u5408<\/strong><\/h2>\n<p>popt, pcov = curve_fit(double_exponential, x_data, y_data, p0=initial_guess)<\/p>\n<h2><strong>\u62df\u5408\u7ed3\u679c<\/strong><\/h2>\n<p>A_fit, B_fit, C_fit, D_fit = popt<\/p>\n<p>print(f&quot;\u62df\u5408\u53c2\u6570: A = {A_fit}, B = {B_fit}, C = {C_fit}, D = {D_fit}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u8bc4\u4f30\u62df\u5408\u7ed3\u679c<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u6bd4\u8f83\u62df\u5408\u66f2\u7ebf\u548c\u539f\u59cb\u6570\u636e\u6765\u8bc4\u4f30\u62df\u5408\u6548\u679c\u3002\u7ed8\u5236\u62df\u5408\u66f2\u7ebf\u548c\u6570\u636e\u70b9\u6765\u8fdb\u884c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u62df\u5408\u66f2\u7ebf<\/p>\n<p>y_fit = double_exponential(x_data, *popt)<\/p>\n<h2><strong>\u7ed8\u56fe<\/strong><\/h2>\n<p>plt.scatter(x_data, y_data, label=&#39;Data&#39;)<\/p>\n<p>plt.plot(x_data, y_fit, label=&#39;Fit&#39;, color=&#39;red&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55<\/h3>\n<\/p>\n<p><h4>1\u3001\u521d\u59cb\u53c2\u6570\u9009\u62e9<\/h4>\n<\/p>\n<p><p>\u5408\u9002\u7684\u521d\u59cb\u53c2\u6570\u9009\u62e9\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u62df\u5408\u7684\u901f\u5ea6\u548c\u7cbe\u5ea6\u3002\u5728\u672a\u77e5\u53c2\u6570\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u7684\u7279\u6027\u6765\u4f30\u8ba1\u521d\u59cb\u503c\u3002\u4f8b\u5982\uff0c\u901a\u8fc7\u89c2\u5bdf\u6570\u636e\u7684\u53d8\u5316\u8303\u56f4\u548c\u8d8b\u52bf\u6765\u4f30\u8ba1<code>A<\/code>\u3001<code>B<\/code>\u3001<code>C<\/code>\u3001<code>D<\/code>\u7684\u521d\u503c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u62df\u5408\u8d28\u91cf\u8bc4\u4f30<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u89c6\u89c9\u6bd4\u8f83\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7edf\u8ba1\u6307\u6807\u5982<code>R^2<\/code>\u3001\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u7b49\u6765\u91cf\u5316\u62df\u5408\u8d28\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import r2_score, mean_squared_error<\/p>\n<h2><strong>\u8ba1\u7b97R^2\u548cMSE<\/strong><\/h2>\n<p>r2 = r2_score(y_data, y_fit)<\/p>\n<p>mse = mean_squared_error(y_data, y_fit)<\/p>\n<p>print(f&quot;R^2: {r2}, MSE: {mse}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u5f02\u5e38\u503c<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u4e2d\u53ef\u80fd\u5b58\u5728\u5f02\u5e38\u503c\uff0c\u8fd9\u4e9b\u5f02\u5e38\u503c\u4f1a\u5f71\u54cd\u62df\u5408\u7ed3\u679c\u3002\u53ef\u4ee5\u4f7f\u7528\u9c81\u68d2\u62df\u5408\u65b9\u6cd5\uff0c\u5982RANSAC\u7b97\u6cd5\uff0c\u6765\u5904\u7406\u5f02\u5e38\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import RANSACRegressor<\/p>\n<h2><strong>\u4f7f\u7528RANSAC\u8fdb\u884c\u62df\u5408<\/strong><\/h2>\n<p>ransac = RANSACRegressor()<\/p>\n<p>ransac.fit(x_data.reshape(-1, 1), y_data)<\/p>\n<p>y_ransac_fit = ransac.predict(x_data.reshape(-1, 1))<\/p>\n<h2><strong>\u7ed8\u56fe<\/strong><\/h2>\n<p>plt.scatter(x_data, y_data, label=&#39;Data&#39;)<\/p>\n<p>plt.plot(x_data, y_ransac_fit, label=&#39;RANSAC Fit&#39;, color=&#39;red&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u66f4\u591a\u590d\u6742\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53cc\u6307\u6570\u6a21\u578b\u53ef\u80fd\u4e0d\u8db3\u4ee5\u63cf\u8ff0\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u66f4\u590d\u6742\u7684\u6a21\u578b\u5982\u591a\u9879\u5f0f\u6a21\u578b\u3001\u4e09\u6b21\u6307\u6570\u6a21\u578b\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def triple_exponential(x, A, B, C, D, E, F):<\/p>\n<p>    return A * np.exp(B * x) + C * np.exp(D * x) + E * np.exp(F * x)<\/p>\n<h2><strong>\u521d\u59cb\u53c2\u6570\u4f30\u8ba1<\/strong><\/h2>\n<p>initial_guess_triple = [1.0, -1.0, 1.0, -0.1, 1.0, -0.05]<\/p>\n<h2><strong>\u62df\u5408\u4e09\u6b21\u6307\u6570\u6a21\u578b<\/strong><\/h2>\n<p>popt_triple, _ = curve_fit(triple_exponential, x_data, y_data, p0=initial_guess_triple)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u6210\u529f\u5730\u62df\u5408\u53cc\u6307\u6570\u51fd\u6570\uff0c\u5e76\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55\u6a21\u578b\u3002\u9009\u62e9\u5408\u9002\u7684\u62df\u5408\u65b9\u6cd5\u548c\u521d\u59cb\u53c2\u6570\u5bf9\u4e8e\u83b7\u5f97\u9ad8\u8d28\u91cf\u7684\u62df\u5408\u7ed3\u679c\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u9009\u62e9\u9002\u5408\u7684\u5e93\u6765\u8fdb\u884c\u53cc\u6307\u6570\u51fd\u6570\u62df\u5408\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u5e38\u7528\u7684\u5e93\u6709NumPy\u3001SciPy\u548cMatplotlib\u3002NumPy\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\uff0cSciPy\u5219\u5305\u542b\u4e86\u4f18\u5316\u548c\u62df\u5408\u529f\u80fd\uff0c\u800cMatplotlib\u5219\u7528\u4e8e\u53ef\u89c6\u5316\u7ed3\u679c\u3002\u5bf9\u4e8e\u53cc\u6307\u6570\u51fd\u6570\u62df\u5408\uff0cSciPy\u7684<code>curve_fit<\/code>\u51fd\u6570\u975e\u5e38\u9002\u5408\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8f7b\u677e\u5730\u62df\u5408\u81ea\u5b9a\u4e49\u51fd\u6570\u3002<\/p>\n<p><strong>\u53cc\u6307\u6570\u51fd\u6570\u7684\u5b9a\u4e49\u662f\u4ec0\u4e48\uff0c\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\uff1f<\/strong><br \/>\u53cc\u6307\u6570\u51fd\u6570\u901a\u5e38\u8868\u793a\u4e3a (y = A \\cdot e^{(-\\alpha x)} + B \\cdot e^{(-\\beta x)})\uff0c\u5176\u4e2dA\u548cB\u662f\u7cfb\u6570\uff0c(\\alpha)\u548c(\\beta)\u662f\u6307\u6570\u8870\u51cf\u7387\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e2a\u516c\u5f0f\uff0c\u5e76\u4f7f\u7528<code>curve_fit<\/code>\u6765\u62df\u5408\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\nfrom scipy.optimize import curve_fit\n\ndef double_exponential(x, A, alpha, B, beta):\n    return A * np.exp(-alpha * x) + B * np.exp(-beta * x)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u8bc4\u4f30\u62df\u5408\u7ed3\u679c\u7684\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u62df\u5408\u7ed3\u679c\u7684\u51c6\u786e\u6027\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u62df\u5408\u4f18\u5ea6\uff08\u5982R\u00b2\u503c\uff09\u3001\u6b8b\u5dee\u5206\u6790\u548c\u53ef\u89c6\u5316\u62df\u5408\u66f2\u7ebf\u4e0e\u539f\u59cb\u6570\u636e\u7684\u5bf9\u6bd4\u6765\u8bc4\u4f30\u3002\u4f7f\u7528Matplotlib\u7ed8\u5236\u5b9e\u9645\u6570\u636e\u70b9\u548c\u62df\u5408\u66f2\u7ebf\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u5224\u65ad\u62df\u5408\u7684\u6548\u679c\u3002R\u00b2\u503c\u8d8a\u63a5\u8fd11\uff0c\u8868\u793a\u62df\u5408\u6548\u679c\u8d8a\u597d\u3002<\/p>\n<p><strong>\u5728\u5904\u7406\u566a\u58f0\u6570\u636e\u65f6\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u6765\u63d0\u9ad8\u62df\u5408\u7cbe\u5ea6\uff1f<\/strong><br \/>\u9762\u5bf9\u566a\u58f0\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6570\u636e\u5e73\u6ed1\u6280\u672f\uff0c\u4f8b\u5982\u79fb\u52a8\u5e73\u5747\u6216Savitzky-Golay\u6ee4\u6ce2\u5668\uff0c\u6765\u51cf\u5c11\u566a\u58f0\u5bf9\u62df\u5408\u7684\u5f71\u54cd\u3002\u6b64\u5916\uff0c\u9009\u62e9\u5408\u9002\u7684\u521d\u59cb\u53c2\u6570\u503c\u4e5f\u80fd\u663e\u8457\u63d0\u9ad8\u62df\u5408\u7684\u51c6\u786e\u6027\u3002\u53ef\u4ee5\u901a\u8fc7\u53ef\u89c6\u5316\u6570\u636e\u5206\u5e03\u6765\u5e2e\u52a9\u786e\u5b9a\u8fd9\u4e9b\u521d\u59cb\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7528Python\u62df\u5408\u53cc\u6307\u6570\u51fd\u6570 \u7528Python\u62df\u5408\u53cc\u6307\u6570\u51fd\u6570\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u5bfc\u5165\u6240\u9700\u5e93\u3001\u5b9a\u4e49\u53cc\u6307\u6570\u51fd\u6570\u3001\u751f\u6210\u6216\u5bfc [&hellip;]","protected":false},"author":3,"featured_media":1075330,"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\/1075318"}],"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=1075318"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075318\/revisions"}],"predecessor-version":[{"id":1075332,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1075318\/revisions\/1075332"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1075330"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1075318"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1075318"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1075318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}