{"id":1069630,"date":"2025-01-08T10:50:45","date_gmt":"2025-01-08T02:50:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1069630.html"},"modified":"2025-01-08T10:50:47","modified_gmt":"2025-01-08T02:50:47","slug":"python%e5%a6%82%e4%bd%95%e5%81%9a%e9%9d%9e%e7%ba%bf%e6%80%a7%e5%9b%9e%e5%bd%92-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1069630.html","title":{"rendered":"python\u5982\u4f55\u505a\u975e\u7ebf\u6027\u56de\u5f52"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25100609\/3127b976-2cb8-44ea-988f-919cddb68e5d.webp\" alt=\"python\u5982\u4f55\u505a\u975e\u7ebf\u6027\u56de\u5f52\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528scipy\u5e93\u3001statsmodels\u5e93\u3001\u4ee5\u53ca<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e93\u5982scikit-learn\u7b49\u3002\u4f7f\u7528scipy.optimize.curve_fit\u51fd\u6570\u3001statsmodels\u4e2d\u7684OLS\u6216GLM\uff0c\u4ee5\u53cascikit-learn\u4e2d\u7684\u975e\u7ebf\u6027\u6a21\u578b\u662f\u5e38\u89c1\u7684\u9009\u62e9\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528scipy.optimize.curve_fit\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52<\/h3>\n<\/p>\n<p><h4>1\u3001\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p><code>scipy.optimize.curve_fit<\/code>\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u62df\u5408\u975e\u7ebf\u6027\u51fd\u6570\u3002\u5b83\u4f7f\u7528\u6700\u5c0f\u4e8c\u4e58\u6cd5\u6765\u62df\u5408\u6a21\u578b\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>curve_fit<\/code>\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52\u7684\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>\u5b9a\u4e49\u975e\u7ebf\u6027\u51fd\u6570<\/strong><\/h2>\n<p>def func(x, a, b, c):<\/p>\n<p>    return a * np.exp(-b * x) + c<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>xdata = np.linspace(0, 4, 50)<\/p>\n<p>ydata = func(xdata, 2.5, 1.3, 0.5) + 0.2 * np.random.normal(size=len(xdata))<\/p>\n<h2><strong>\u4f7f\u7528curve_fit\u62df\u5408\u6570\u636e<\/strong><\/h2>\n<p>popt, pcov = curve_fit(func, xdata, ydata)<\/p>\n<h2><strong>\u6253\u5370\u62df\u5408\u53c2\u6570<\/strong><\/h2>\n<p>print(&quot;\u62df\u5408\u53c2\u6570:&quot;, popt)<\/p>\n<h2><strong>\u7ed8\u5236\u7ed3\u679c<\/strong><\/h2>\n<p>plt.plot(xdata, ydata, &#39;b-&#39;, label=&#39;data&#39;)<\/p>\n<p>plt.plot(xdata, func(xdata, *popt), &#39;r-&#39;, label=&#39;fit: a=%5.3f, b=%5.3f, c=%5.3f&#39; % tuple(popt))<\/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><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u975e\u7ebf\u6027\u51fd\u6570<code>func<\/code>\uff0c\u7136\u540e\u751f\u6210\u4e00\u4e9b\u793a\u4f8b\u6570\u636e\u5e76\u52a0\u5165\u566a\u58f0\u3002\u63a5\u7740\u4f7f\u7528<code>curve_fit<\/code>\u51fd\u6570\u62df\u5408\u8fd9\u4e9b\u6570\u636e\uff0c\u5e76\u7ed8\u5236\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528statsmodels\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52<\/h3>\n<\/p>\n<p><h4>1\u3001\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p><code>statsmodels<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u4f30\u8ba1\u548c\u63a8\u65ad\u7edf\u8ba1\u6a21\u578b\u7684Python\u6a21\u5757\u3002\u5b83\u63d0\u4f9b\u4e86\u5305\u62ec\u7ebf\u6027\u548c\u975e\u7ebf\u6027\u56de\u5f52\u5728\u5185\u7684\u591a\u79cd\u7edf\u8ba1\u6a21\u578b\u3002\u867d\u7136<code>statsmodels<\/code>\u4e3b\u8981\u7528\u4e8e\u7ebf\u6027\u6a21\u578b\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u975e\u7ebf\u6027\u56de\u5f52\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>statsmodels<\/code>\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import statsmodels.api as sm<\/p>\n<p>from statsmodels.formula.api import ols<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>np.random.seed(0)<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = 2 * np.sin(x) + np.random.normal(size=100)<\/p>\n<h2><strong>\u6784\u5efa\u8bbe\u8ba1\u77e9\u9635<\/strong><\/h2>\n<p>X = np.column_stack((np.sin(x), np.cos(x)))<\/p>\n<p>X = sm.add_constant(X)<\/p>\n<h2><strong>\u62df\u5408\u975e\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = sm.OLS(y, X).fit()<\/p>\n<h2><strong>\u6253\u5370\u6a21\u578b\u6458\u8981<\/strong><\/h2>\n<p>print(model.summary())<\/p>\n<h2><strong>\u9884\u6d4b\u548c\u7ed8\u56fe<\/strong><\/h2>\n<p>y_pred = model.predict(X)<\/p>\n<p>plt.scatter(x, y, label=&#39;data&#39;)<\/p>\n<p>plt.plot(x, y_pred, label=&#39;fit&#39;, color=&#39;red&#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><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u751f\u6210\u4e86\u4e00\u4e9b\u793a\u4f8b\u6570\u636e\u5e76\u6784\u5efa\u8bbe\u8ba1\u77e9\u9635\uff0c\u7136\u540e\u4f7f\u7528<code>OLS<\/code>\u51fd\u6570\u62df\u5408\u975e\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528scikit-learn\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52<\/h3>\n<\/p>\n<p><h4>1\u3001\u7b80\u4ecb<\/h4>\n<\/p>\n<p><p><code>scikit-learn<\/code>\u662f\u4e00\u4e2a\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u5927\u91cf\u7684\u6a21\u578b\u548c\u5de5\u5177\u3002\u5bf9\u4e8e\u975e\u7ebf\u6027\u56de\u5f52\uff0c\u53ef\u4ee5\u4f7f\u7528\u8bf8\u5982<code>PolynomialFeatures<\/code>\u548c<code>LinearRegression<\/code>\u7b49\u5de5\u5177\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>scikit-learn<\/code>\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52\u7684\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 sklearn.preprocessing import PolynomialFeatures<\/p>\n<p>from sklearn.linear_model import LinearRegression<\/p>\n<p>from sklearn.pipeline import make_pipeline<\/p>\n<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>np.random.seed(0)<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = 2 * np.sin(x) + np.random.normal(size=100)<\/p>\n<h2><strong>\u8f6c\u6362\u6570\u636e\u5f62\u72b6<\/strong><\/h2>\n<p>x = x[:, np.newaxis]<\/p>\n<h2><strong>\u521b\u5efa\u591a\u9879\u5f0f\u7279\u5f81\u548c\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>degree = 5<\/p>\n<p>model = make_pipeline(PolynomialFeatures(degree), LinearRegression())<\/p>\n<h2><strong>\u62df\u5408\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(x, y)<\/p>\n<h2><strong>\u9884\u6d4b\u548c\u7ed8\u56fe<\/strong><\/h2>\n<p>y_pred = model.predict(x)<\/p>\n<p>plt.scatter(x, y, label=&#39;data&#39;)<\/p>\n<p>plt.plot(x, y_pred, label=&#39;fit&#39;, color=&#39;red&#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><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>PolynomialFeatures<\/code>\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u591a\u9879\u5f0f\u7279\u5f81\uff0c\u5e76\u4f7f\u7528<code>LinearRegression<\/code>\u62df\u5408\u975e\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\u3002<code>scipy.optimize.curve_fit<\/code>\u9002\u7528\u4e8e\u7b80\u5355\u7684\u975e\u7ebf\u6027\u6a21\u578b\uff0c<code>statsmodels<\/code>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7edf\u8ba1\u5206\u6790\u5de5\u5177\uff0c\u800c<code>scikit-learn<\/code>\u5219\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u673a\u5668\u5b66\u4e60\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u9009\u62e9\u5408\u9002\u7684\u5e93<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u95ee\u9898\u548c\u6570\u636e\u3002\u5982\u679c\u60a8\u7684\u6570\u636e\u8f83\u4e3a\u7b80\u5355\uff0c\u5e76\u4e14\u60a8\u9700\u8981\u5bf9\u6a21\u578b\u8fdb\u884c\u8be6\u7ec6\u7684\u7edf\u8ba1\u5206\u6790\uff0c<code>statsmodels<\/code>\u53ef\u80fd\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\u3002\u5982\u679c\u60a8\u9700\u8981\u66f4\u590d\u6742\u7684\u6a21\u578b\u548c\u66f4\u591a\u7684\u63a7\u5236\u9009\u9879\uff0c<code>scikit-learn<\/code>\u53ef\u80fd\u66f4\u9002\u5408\u60a8\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u6570\u636e\u9884\u5904\u7406\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u6b65\u3002\u786e\u4fdd\u60a8\u7684\u6570\u636e\u662f\u6e05\u6d01\u7684\uff0c\u5e76\u4e14\u9002\u5408\u6240\u9009\u7684\u6a21\u578b\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e<code>scikit-learn<\/code>\u4e2d\u7684\u591a\u9879\u5f0f\u56de\u5f52\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u6570\u636e\u7684\u5f62\u72b6\u6b63\u786e\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u6a21\u578b\u8bc4\u4f30<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52\u540e\uff0c\u8bc4\u4f30\u6a21\u578b\u7684\u6027\u80fd\u4e5f\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u53ef\u4ee5\u4f7f\u7528\u8bf8\u5982\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u3001\u51b3\u5b9a\u7cfb\u6570\uff08R^2\uff09\u7b49\u6307\u6807\u6765\u8bc4\u4f30\u6a21\u578b\u7684\u62df\u5408\u6548\u679c\u3002\u8fd9\u4e9b\u6307\u6807\u53ef\u4ee5\u5e2e\u52a9\u60a8\u4e86\u89e3\u6a21\u578b\u7684\u9884\u6d4b\u80fd\u529b\uff0c\u5e76\u8fdb\u884c\u5fc5\u8981\u7684\u8c03\u6574\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5b9e\u6218\u6848\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u975e\u7ebf\u6027\u56de\u5f52\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u6848\u4f8b\u6765\u5c55\u793a\u5982\u4f55\u4f7f\u7528Python\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6848\u4f8b\u80cc\u666f<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5173\u4e8e\u67d0\u79cd\u5316\u5b66\u53cd\u5e94\u7684\u6570\u636e\u96c6\uff0c\u5176\u4e2d\u5305\u542b\u53cd\u5e94\u65f6\u95f4\u548c\u53cd\u5e94\u901f\u7387\u7684\u6570\u636e\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u5efa\u7acb\u4e00\u4e2a\u975e\u7ebf\u6027\u56de\u5f52\u6a21\u578b\uff0c\u6765\u9884\u6d4b\u4e0d\u540c\u53cd\u5e94\u65f6\u95f4\u4e0b\u7684\u53cd\u5e94\u901f\u7387\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u51c6\u5907<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u751f\u6210\u4e00\u4e9b\u793a\u4f8b\u6570\u636e\u6765\u6a21\u62df\u5316\u5b66\u53cd\u5e94\u7684\u8fc7\u7a0b\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<h2><strong>\u751f\u6210\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>np.random.seed(42)<\/p>\n<p>time = np.linspace(0, 10, 100)<\/p>\n<p>rate = 3 * np.exp(-0.5 * time) + np.random.normal(0, 0.2, 100)<\/p>\n<h2><strong>\u7ed8\u5236\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>plt.scatter(time, rate, label=&#39;Data&#39;)<\/p>\n<p>plt.xlabel(&#39;Time&#39;)<\/p>\n<p>plt.ylabel(&#39;Reaction Rate&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53cd\u5e94\u901f\u7387\u968f\u7740\u65f6\u95f4\u7684\u589e\u52a0\u800c\u51cf\u5c0f\uff0c\u5e76\u4e14\u6570\u636e\u4e2d\u5b58\u5728\u4e00\u5b9a\u7684\u566a\u58f0\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528scipy.optimize.curve_fit\u8fdb\u884c\u62df\u5408<\/h4>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528<code>scipy.optimize.curve_fit<\/code>\u6765\u62df\u5408\u4e00\u4e2a\u6307\u6570\u8870\u51cf\u6a21\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.optimize import curve_fit<\/p>\n<h2><strong>\u5b9a\u4e49\u6307\u6570\u8870\u51cf\u6a21\u578b<\/strong><\/h2>\n<p>def model_func(t, a, b):<\/p>\n<p>    return a * np.exp(-b * t)<\/p>\n<h2><strong>\u4f7f\u7528curve_fit\u62df\u5408\u6570\u636e<\/strong><\/h2>\n<p>popt, pcov = curve_fit(model_func, time, rate)<\/p>\n<h2><strong>\u6253\u5370\u62df\u5408\u53c2\u6570<\/strong><\/h2>\n<p>print(&quot;\u62df\u5408\u53c2\u6570:&quot;, popt)<\/p>\n<h2><strong>\u9884\u6d4b\u548c\u7ed8\u56fe<\/strong><\/h2>\n<p>rate_pred = model_func(time, *popt)<\/p>\n<p>plt.scatter(time, rate, label=&#39;Data&#39;)<\/p>\n<p>plt.plot(time, rate_pred, label=&#39;Fit&#39;, color=&#39;red&#39;)<\/p>\n<p>plt.xlabel(&#39;Time&#39;)<\/p>\n<p>plt.ylabel(&#39;Reaction Rate&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6307\u6570\u8870\u51cf\u6a21\u578b\uff0c\u5e76\u4f7f\u7528<code>curve_fit<\/code>\u51fd\u6570\u8fdb\u884c\u62df\u5408\u3002\u62df\u5408\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u6a21\u578b\u4e0e\u6570\u636e\u7684\u62df\u5408\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u6a21\u578b\u8bc4\u4f30<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u6211\u4eec\u8bc4\u4f30\u6a21\u578b\u7684\u62df\u5408\u6548\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import mean_squared_error, r2_score<\/p>\n<h2><strong>\u8ba1\u7b97MSE\u548cR^2<\/strong><\/h2>\n<p>mse = mean_squared_error(rate, rate_pred)<\/p>\n<p>r2 = r2_score(rate, rate_pred)<\/p>\n<p>print(&quot;\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09:&quot;, mse)<\/p>\n<p>print(&quot;\u51b3\u5b9a\u7cfb\u6570\uff08R^2\uff09:&quot;, r2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8ba1\u7b97\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u548c\u51b3\u5b9a\u7cfb\u6570\uff08R^2\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u91cf\u5316\u6a21\u578b\u7684\u62df\u5408\u6548\u679c\u3002\u8f83\u4f4e\u7684MSE\u548c\u8f83\u9ad8\u7684R^2\u503c\u8868\u793a\u6a21\u578b\u5177\u6709\u8f83\u597d\u7684\u62df\u5408\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684<code>scipy.optimize.curve_fit<\/code>\u3001<code>statsmodels<\/code>\u548c<code>scikit-learn<\/code>\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u9002\u7528\u573a\u666f\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u95ee\u9898\u548c\u6570\u636e\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6570\u636e\u9884\u5904\u7406\u548c\u6a21\u578b\u8bc4\u4f30\u662f\u81f3\u5173\u91cd\u8981\u7684\u6b65\u9aa4\uff0c\u6709\u52a9\u4e8e\u63d0\u9ad8\u6a21\u578b\u7684\u9884\u6d4b\u80fd\u529b\u548c\u51c6\u786e\u6027\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u60a8\u80fd\u591f\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u975e\u7ebf\u6027\u56de\u5f52\uff0c\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u975e\u7ebf\u6027\u56de\u5f52\u7684\u57fa\u672c\u6982\u5ff5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u975e\u7ebf\u6027\u56de\u5f52\u662f\u4e00\u79cd\u7528\u4e8e\u5efa\u6a21\u53d8\u91cf\u4e4b\u95f4\u975e\u7ebf\u6027\u5173\u7cfb\u7684\u7edf\u8ba1\u6280\u672f\u3002\u4e0e\u7ebf\u6027\u56de\u5f52\u4e0d\u540c\uff0c\u975e\u7ebf\u6027\u56de\u5f52\u4e0d\u5047\u8bbe\u56e0\u53d8\u91cf\u4e0e\u81ea\u53d8\u91cf\u4e4b\u95f4\u662f\u7ebf\u6027\u5173\u7cfb\u3002\u5b83\u901a\u5e38\u6d89\u53ca\u4f7f\u7528\u590d\u6742\u7684\u6570\u5b66\u51fd\u6570\uff0c\u5982\u591a\u9879\u5f0f\u3001\u6307\u6570\u6216\u5bf9\u6570\u51fd\u6570\uff0c\u6765\u6700\u4f18\u5730\u63cf\u8ff0\u6570\u636e\u70b9\u7684\u5206\u5e03\u3002\u901a\u8fc7\u62df\u5408\u6a21\u578b\uff0c\u7814\u7a76\u4eba\u5458\u53ef\u4ee5\u66f4\u51c6\u786e\u5730\u9884\u6d4b\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5b9e\u73b0\u975e\u7ebf\u6027\u56de\u5f52\u65f6\uff0c\u5e38\u7528\u7684\u5e93\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u51e0\u4e2a\u5f3a\u5927\u7684\u5e93\u53ef\u4ee5\u7528\u4e8e\u975e\u7ebf\u6027\u56de\u5f52\u3002\u6700\u5e38\u7528\u7684\u5305\u62ecNumPy\u548cSciPy\uff0c\u5b83\u4eec\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u5b66\u548c\u7edf\u8ba1\u51fd\u6570\u3002\u7279\u522b\u662fSciPy\u4e2d\u7684<code>curve_fit<\/code>\u51fd\u6570\uff0c\u80fd\u591f\u6839\u636e\u7ed9\u5b9a\u7684\u6a21\u578b\u62df\u5408\u6570\u636e\u3002\u6b64\u5916\uff0cpandas\u5e93\u7528\u4e8e\u6570\u636e\u5904\u7406\u548c\u6e05\u7406\uff0cMatplotlib\u548cSeaborn\u5219\u53ef\u4ee5\u5e2e\u52a9\u53ef\u89c6\u5316\u56de\u5f52\u7ed3\u679c\u3002<\/p>\n<p><strong>\u5982\u4f55\u9009\u62e9\u5408\u9002\u7684\u975e\u7ebf\u6027\u56de\u5f52\u6a21\u578b\uff1f<\/strong><br \/>\u9009\u62e9\u5408\u9002\u7684\u975e\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u901a\u5e38\u9700\u8981\u8003\u8651\u6570\u636e\u7684\u7279\u6027\u548c\u80cc\u666f\u77e5\u8bc6\u3002\u53ef\u4ee5\u901a\u8fc7\u7ed8\u5236\u6563\u70b9\u56fe\u6765\u521d\u6b65\u89c2\u5bdf\u81ea\u53d8\u91cf\u4e0e\u56e0\u53d8\u91cf\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u57fa\u4e8e\u8fd9\u79cd\u89c2\u5bdf\uff0c\u9009\u62e9\u5408\u9002\u7684\u975e\u7ebf\u6027\u51fd\u6570\u5f62\u5f0f\uff08\u5982\u6307\u6570\u51fd\u6570\u3001\u5bf9\u6570\u51fd\u6570\u6216\u591a\u9879\u5f0f\u51fd\u6570\uff09\u8fdb\u884c\u62df\u5408\u3002\u540c\u65f6\uff0c\u4f7f\u7528\u62df\u5408\u4f18\u5ea6\u6307\u6807\uff08\u5982R\u00b2\u503c\uff09\u548c\u6b8b\u5dee\u5206\u6790\u6765\u8bc4\u4f30\u6a21\u578b\u7684\u8868\u73b0\uff0c\u4ee5\u786e\u4fdd\u6240\u9009\u6a21\u578b\u80fd\u591f\u6709\u6548\u6355\u6349\u6570\u636e\u7684\u7279\u5f81\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u8fdb\u884c\u975e\u7ebf\u6027\u56de\u5f52\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528scipy\u5e93\u3001statsmodels\u5e93\u3001\u4ee5\u53ca\u673a\u5668\u5b66\u4e60\u5e93\u5982sciki [&hellip;]","protected":false},"author":3,"featured_media":1069640,"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\/1069630"}],"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=1069630"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069630\/revisions"}],"predecessor-version":[{"id":1069641,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1069630\/revisions\/1069641"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1069640"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1069630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1069630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1069630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}