{"id":1159066,"date":"2025-01-13T18:48:28","date_gmt":"2025-01-13T10:48:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1159066.html"},"modified":"2025-01-13T18:48:31","modified_gmt":"2025-01-13T10:48:31","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%85%a5%e8%87%aa%e7%84%b6%e5%af%b9%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1159066.html","title":{"rendered":"Python\u5982\u4f55\u8f93\u5165\u81ea\u7136\u5bf9\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25200859\/0c8727a4-3085-4a5d-a351-3aa4954cfb08.webp\" alt=\"Python\u5982\u4f55\u8f93\u5165\u81ea\u7136\u5bf9\u6570\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u8f93\u5165\u81ea\u7136\u5bf9\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528math\u6a21\u5757\u3001numpy\u6a21\u5757\u3001scipy\u6a21\u5757\u7b49\u3002\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528math\u6a21\u5757\u3001\u4f7f\u7528numpy\u6a21\u5757\u3001\u4f7f\u7528scipy\u6a21\u5757\u3002<\/strong> <\/p>\n<\/p>\n<p><p>\u5176\u4e2d<strong>\u4f7f\u7528math\u6a21\u5757<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3amath\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>import math<\/code>\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>math.log<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>x = 10<\/p>\n<p>natural_log = math.log(x)<\/p>\n<p>print(natural_log)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u51fd\u6570\u9ed8\u8ba4\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\uff0c\u5982\u679c\u4f60\u9700\u8981\u8ba1\u7b97\u5176\u4ed6\u5e95\u6570\u7684\u5bf9\u6570\uff0c\u53ef\u4ee5\u4f20\u5165\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528MATH\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>1. math.log\u51fd\u6570<\/strong><\/p>\n<p><code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>log<\/code>\u51fd\u6570\u662f\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u7684\u57fa\u672c\u65b9\u6cd5\u3002<code>math.log(x)<\/code>\u8fd4\u56dex\u7684\u81ea\u7136\u5bf9\u6570\uff0c\u5e95\u4e3ae\u3002\u5982\u679c\u9700\u8981\u8ba1\u7b97\u5176\u4ed6\u5e95\u6570\u7684\u5bf9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math.log(x, base)<\/code>\u5f62\u5f0f\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>x = 10<\/p>\n<p>natural_log = math.log(x)<\/p>\n<p>print(f&quot;10\u7684\u81ea\u7136\u5bf9\u6570\u662f\uff1a{natural_log}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u4ee3\u7801\u7247\u6bb5\u4f1a\u8f93\u51fa10\u7684\u81ea\u7136\u5bf9\u6570\u3002<code>math.log<\/code>\u51fd\u6570\u8fd8\u53ef\u4ee5\u63a5\u53d7\u7b2c\u4e8c\u4e2a\u53c2\u6570<code>base<\/code>\uff0c\u7528\u4e8e\u8ba1\u7b97\u4efb\u610f\u5e95\u6570\u7684\u5bf9\u6570\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u4ee52\u4e3a\u5e95\u768410\u7684\u5bf9\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>x = 10<\/p>\n<p>base = 2<\/p>\n<p>log_base_2 = math.log(x, base)<\/p>\n<p>print(f&quot;\u4ee52\u4e3a\u5e95\u768410\u7684\u5bf9\u6570\u662f\uff1a{log_base_2}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. math.exp\u51fd\u6570<\/strong><\/p>\n<p><code>math.exp<\/code>\u51fd\u6570\u4e0e<code>math.log<\/code>\u662f\u4e92\u9006\u8fd0\u7b97\uff0c\u7528\u4e8e\u8ba1\u7b97e\u7684x\u6b21\u5e42\u3002\u5b83\u7684\u5e38\u89c1\u7528\u9014\u662f\u5728\u77e5\u9053\u5bf9\u6570\u503c\u7684\u60c5\u51b5\u4e0b\u8fd8\u539f\u539f\u59cb\u6570\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>x = 2<\/p>\n<p>exp_x = math.exp(x)<\/p>\n<p>print(f&quot;e\u76842\u6b21\u5e42\u662f\uff1a{exp_x}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528NUMPY\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>1. numpy.log\u51fd\u6570<\/strong><\/p>\n<p><code>numpy<\/code>\u6a21\u5757\u662f\u79d1\u5b66\u8ba1\u7b97\u7684\u91cd\u8981\u5e93\uff0c\u5176\u4e2d\u7684<code>log<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u3002\u4e0e<code>math.log<\/code>\u7c7b\u4f3c\uff0c<code>numpy.log<\/code>\u51fd\u6570\u8fd4\u56de\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u81ea\u7136\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>x = np.array([1, 10, 100])<\/p>\n<p>log_x = np.log(x)<\/p>\n<p>print(f&quot;\u6570\u7ec4\u5143\u7d20\u7684\u81ea\u7136\u5bf9\u6570\u662f\uff1a{log_x}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>numpy.log<\/code>\u5c06\u8fd4\u56de\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u81ea\u7136\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>2. numpy.log10\u548cnumpy.log2\u51fd\u6570<\/strong><\/p>\n<p>\u9664\u4e86\u81ea\u7136\u5bf9\u6570\uff0c<code>numpy<\/code>\u8fd8\u63d0\u4f9b\u4e86\u8ba1\u7b97\u4ee510\u548c2\u4e3a\u5e95\u7684\u5bf9\u6570\u7684\u51fd\u6570\uff0c\u5206\u522b\u662f<code>numpy.log10<\/code>\u548c<code>numpy.log2<\/code>\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>x = np.array([1, 10, 100])<\/p>\n<p>log10_x = np.log10(x)<\/p>\n<p>log2_x = np.log2(x)<\/p>\n<p>print(f&quot;\u6570\u7ec4\u5143\u7d20\u7684\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\u662f\uff1a{log10_x}&quot;)<\/p>\n<p>print(f&quot;\u6570\u7ec4\u5143\u7d20\u7684\u4ee52\u4e3a\u5e95\u7684\u5bf9\u6570\u662f\uff1a{log2_x}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528SCIPY\u6a21\u5757<\/h3>\n<\/p>\n<p><p><strong>1. scipy.special\u6a21\u5757<\/strong><\/p>\n<p><code>scipy<\/code>\u6a21\u5757\u4e2d\u7684<code>special<\/code>\u5b50\u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u7279\u6b8a\u51fd\u6570\uff0c\u5305\u62ec\u81ea\u7136\u5bf9\u6570\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy import special<\/p>\n<p>x = 10<\/p>\n<p>log_x = special.log1p(x-1)<\/p>\n<p>print(f&quot;10\u7684\u81ea\u7136\u5bf9\u6570\u662f\uff1a{log_x}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>special.log1p(x)<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97<code>log(1 + x)<\/code>\uff0c\u5728<code>x<\/code>\u975e\u5e38\u63a5\u8fd1\u4e8e0\u65f6\uff0c\u8fd9\u4e2a\u51fd\u6570\u6bd4<code>log(1 + x)<\/code>\u66f4\u52a0\u7cbe\u786e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p><strong>1. \u8ba1\u7b97\u591a\u4e2a\u6570\u503c\u7684\u81ea\u7136\u5bf9\u6570<\/strong><\/p>\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u8ba1\u7b97\u591a\u4e2a\u6570\u503c\u7684\u81ea\u7136\u5bf9\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u548c<code>numpy<\/code>\u6a21\u5757\u5206\u522b\u8ba1\u7b97\u591a\u4e2a\u6570\u503c\u7684\u81ea\u7136\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528math\u6a21\u5757\u8ba1\u7b97\u591a\u4e2a\u6570\u503c\u7684\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>values = [1, 10, 100]<\/p>\n<p>log_values = [math.log(value) for value in values]<\/p>\n<p>print(f&quot;\u4f7f\u7528math\u6a21\u5757\u8ba1\u7b97\u7684\u81ea\u7136\u5bf9\u6570\uff1a{log_values}&quot;)<\/p>\n<h2><strong>\u4f7f\u7528numpy\u6a21\u5757\u8ba1\u7b97\u591a\u4e2a\u6570\u503c\u7684\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>values = np.array([1, 10, 100])<\/p>\n<p>log_values = np.log(values)<\/p>\n<p>print(f&quot;\u4f7f\u7528numpy\u6a21\u5757\u8ba1\u7b97\u7684\u81ea\u7136\u5bf9\u6570\uff1a{log_values}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u7ed8\u5236\u5bf9\u6570\u51fd\u6570\u56fe\u50cf<\/strong><\/p>\n<p>\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\uff0c\u7ed8\u5236\u5bf9\u6570\u51fd\u6570\u56fe\u50cf\u662f\u5e38\u89c1\u7684\u9700\u6c42\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528<code>matplotlib<\/code>\u5e93\u7ed8\u5236\u81ea\u7136\u5bf9\u6570\u51fd\u6570\u56fe\u50cf\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>x = np.linspace(0.1, 10, 100)<\/p>\n<p>y = np.log(x)<\/p>\n<p>plt.plot(x, y, label=&#39;y=log(x)&#39;)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;y&#39;)<\/p>\n<p>plt.title(&#39;Natural Logarithm Function&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.grid(True)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u81ea\u7136\u5bf9\u6570\u7684\u5e94\u7528\u9886\u57df<\/h3>\n<\/p>\n<p><p><strong>1. \u79d1\u5b66\u8ba1\u7b97<\/strong><\/p>\n<p>\u81ea\u7136\u5bf9\u6570\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u4f8b\u5982\u5728\u5316\u5b66\u4e2d\u7684\u53cd\u5e94\u901f\u7387\u8ba1\u7b97\u3001\u751f\u7269\u5b66\u4e2d\u7684\u79cd\u7fa4\u589e\u957f\u6a21\u578b\u3001\u7269\u7406\u5b66\u4e2d\u7684\u653e\u5c04\u6027\u8870\u53d8\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u7ecf\u6d4e\u5b66\u4e0e\u91d1\u878d\u5b66<\/strong><\/p>\n<p>\u5728\u7ecf\u6d4e\u5b66\u548c\u91d1\u878d\u5b66\u4e2d\uff0c\u81ea\u7136\u5bf9\u6570\u7528\u4e8e\u8ba1\u7b97\u590d\u5229\u3001\u6295\u8d44\u56de\u62a5\u7387\u3001\u7ecf\u6d4e\u589e\u957f\u6a21\u578b\u7b49\u3002\u4f8b\u5982\uff0c\u8ba1\u7b97\u8fde\u7eed\u590d\u5229\u7684\u516c\u5f0f\u4e3a<code>A = P * exp(rt)<\/code>\uff0c\u5176\u4e2d<code>P<\/code>\u662f\u672c\u91d1\uff0c<code>r<\/code>\u662f\u5e74\u5229\u7387\uff0c<code>t<\/code>\u662f\u65f6\u95f4\uff0c<code>exp<\/code>\u8868\u793a\u4ee5e\u4e3a\u5e95\u7684\u6307\u6570\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u4fe1\u606f\u8bba<\/strong><\/p>\n<p>\u5728\u4fe1\u606f\u8bba\u4e2d\uff0c\u81ea\u7136\u5bf9\u6570\u7528\u4e8e\u8ba1\u7b97\u71b5\u548c\u4fe1\u606f\u91cf\u3002\u4f8b\u5982\uff0c\u9999\u519c\u71b5\u7684\u8ba1\u7b97\u516c\u5f0f\u4e3a<code>H = -sum(p(x) * log(p(x)))<\/code>\uff0c\u5176\u4e2d<code>p(x)<\/code>\u662f\u4e8b\u4ef6x\u53d1\u751f\u7684\u6982\u7387\u3002<\/p>\n<\/p>\n<p><p><strong>4. <a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e0e\u6570\u636e\u79d1\u5b66<\/strong><\/p>\n<p>\u5728\u673a\u5668\u5b66\u4e60\u548c\u6570\u636e\u79d1\u5b66\u4e2d\uff0c\u81ea\u7136\u5bf9\u6570\u7528\u4e8e\u7279\u5f81\u7f29\u653e\u3001\u5bf9\u6570\u51e0\u7387\u56de\u5f52\u3001\u635f\u5931\u51fd\u6570\u7b49\u3002\u4f8b\u5982\uff0c\u5bf9\u6570\u51e0\u7387\u56de\u5f52\u7684\u635f\u5931\u51fd\u6570\u4e3a<code>L = -sum(y * log(p) + (1 - y) * log(1 - p))<\/code>\uff0c\u5176\u4e2d<code>y<\/code>\u662f\u5b9e\u9645\u6807\u7b7e\uff0c<code>p<\/code>\u662f\u9884\u6d4b\u6982\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5bf9\u6570\u7684\u5e95\u6570\u8f6c\u6362<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u5c06\u4e00\u79cd\u5e95\u6570\u7684\u5bf9\u6570\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u5e95\u6570\u7684\u5bf9\u6570\u3002\u5e95\u6570\u8f6c\u6362\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p><code>log_b(x) = log_k(x) \/ log_k(b)<\/code><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>log_b(x)<\/code>\u8868\u793a\u4ee5b\u4e3a\u5e95\u7684x\u7684\u5bf9\u6570\uff0c<code>log_k(x)<\/code>\u548c<code>log_k(b)<\/code>\u8868\u793a\u4ee5k\u4e3a\u5e95\u7684x\u548cb\u7684\u5bf9\u6570\u3002\u4f8b\u5982\uff0c\u5c06\u81ea\u7136\u5bf9\u6570\u8f6c\u6362\u4e3a\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>x = 10<\/p>\n<p>log10_x = math.log(x) \/ math.log(10)<\/p>\n<p>print(f&quot;10\u7684\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\u662f\uff1a{log10_x}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5bf9\u6570\u7684\u6027\u8d28<\/h3>\n<\/p>\n<p><p><strong>1. \u5bf9\u6570\u7684\u57fa\u672c\u6027\u8d28<\/strong><\/p>\n<p>\u5bf9\u6570\u5177\u6709\u4ee5\u4e0b\u57fa\u672c\u6027\u8d28\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u5bf9\u6570\u7684\u4e58\u79ef\u6027\u8d28\uff1a<code>log_b(x * y) = log_b(x) + log_b(y)<\/code><\/li>\n<li>\u5bf9\u6570\u7684\u5546\u6027\u8d28\uff1a<code>log_b(x \/ y) = log_b(x) - log_b(y)<\/code><\/li>\n<li>\u5bf9\u6570\u7684\u5e42\u6027\u8d28\uff1a<code>log_b(x^y) = y * log_b(x)<\/code><\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>x = 10<\/p>\n<p>y = 2<\/p>\n<p>log_prod = math.log(x * y)<\/p>\n<p>log_sum = math.log(x) + math.log(y)<\/p>\n<p>print(f&quot;log(x * y) = {log_prod}, log(x) + log(y) = {log_sum}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u5bf9\u6570\u7684\u6362\u5e95\u516c\u5f0f<\/strong><\/p>\n<p>\u5bf9\u6570\u7684\u6362\u5e95\u516c\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p><code>log_b(x) = log_k(x) \/ log_k(b)<\/code><\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>log_b(x)<\/code>\u8868\u793a\u4ee5b\u4e3a\u5e95\u7684x\u7684\u5bf9\u6570\uff0c<code>log_k(x)<\/code>\u548c<code>log_k(b)<\/code>\u8868\u793a\u4ee5k\u4e3a\u5e95\u7684x\u548cb\u7684\u5bf9\u6570\u3002\u4f8b\u5982\uff0c\u5c06\u81ea\u7136\u5bf9\u6570\u8f6c\u6362\u4e3a\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>x = 10<\/p>\n<p>log10_x = math.log(x) \/ math.log(10)<\/p>\n<p>print(f&quot;10\u7684\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\u662f\uff1a{log10_x}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5bf9\u6570\u51fd\u6570\u7684\u6570\u503c\u7a33\u5b9a\u6027<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u503c\u8ba1\u7b97\u4e2d\uff0c\u5bf9\u6570\u51fd\u6570\u7684\u6570\u503c\u7a33\u5b9a\u6027\u662f\u4e00\u4e2a\u91cd\u8981\u95ee\u9898\u3002\u5bf9\u4e8e\u63a5\u8fd1\u4e8e0\u7684\u6570\u503c\uff0c\u76f4\u63a5\u8ba1\u7b97<code>log(1 + x)<\/code>\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u503c\u4e0d\u7a33\u5b9a\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>log1p(x)<\/code>\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u8ba1\u7b97<code>log(1 + x)<\/code>\uff0c\u5728<code>x<\/code>\u63a5\u8fd1\u4e8e0\u65f6\u66f4\u52a0\u7cbe\u786e\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>x = 1e-10<\/p>\n<p>log1p_x = math.log1p(x)<\/p>\n<p>log_x = math.log(1 + x)<\/p>\n<p>print(f&quot;log1p(x) = {log1p_x}, log(1 + x) = {log_x}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5bf9\u6570\u51fd\u6570\u7684\u590d\u6742\u5ea6\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5bf9\u6570\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3aO(1)\uff0c\u56e0\u4e3a\u8ba1\u7b97\u5bf9\u6570\u7684\u65f6\u95f4\u4e0e\u8f93\u5165\u6570\u503c\u7684\u5927\u5c0f\u65e0\u5173\u3002\u7136\u800c\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u96c6\u65f6\uff0c\u8ba1\u7b97\u5bf9\u6570\u7684\u603b\u65f6\u95f4\u53ef\u80fd\u4f1a\u53d8\u5f97\u663e\u8457\u3002\u5728\u8fd9\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528\u77e2\u91cf\u5316\u64cd\u4f5c\u6216\u5e76\u884c\u8ba1\u7b97\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy<\/code>\u6a21\u5757\u7684\u77e2\u91cf\u5316\u64cd\u4f5c\u8ba1\u7b97\u5927\u89c4\u6a21\u6570\u636e\u96c6\u7684\u81ea\u7136\u5bf9\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>x = np.random.rand(1000000)<\/p>\n<p>log_x = np.log(x)<\/p>\n<p>print(log_x[:10])  # \u6253\u5370\u524d10\u4e2a\u7ed3\u679c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u3001<code>numpy<\/code>\u6a21\u5757\u548c<code>scipy<\/code>\u6a21\u5757\u3002<code>math<\/code>\u6a21\u5757\u9002\u7528\u4e8e\u5355\u4e2a\u6570\u503c\u7684\u5bf9\u6570\u8ba1\u7b97\uff0c\u800c<code>numpy<\/code>\u6a21\u5757\u548c<code>scipy<\/code>\u6a21\u5757\u5219\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u96c6\u548c\u79d1\u5b66\u8ba1\u7b97\u3002\u6b64\u5916\uff0c\u81ea\u7136\u5bf9\u6570\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u7ecf\u6d4e\u5b66\u3001\u4fe1\u606f\u8bba\u3001\u673a\u5668\u5b66\u4e60\u7b49\u9886\u57df\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u4e0d\u540c\u6a21\u5757\u7684\u4f7f\u7528\u65b9\u6cd5\u4ee5\u53ca\u5bf9\u6570\u51fd\u6570\u7684\u6027\u8d28\u548c\u5e94\u7528\uff0c\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u548c\u5de5\u5177\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u4f18\u5316\uff0c\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u548c\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>math<\/code>\u6a21\u5757\u6765\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u3002\u5177\u4f53\u65b9\u6cd5\u662f\u4f7f\u7528<code>math.log()<\/code>\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e00\u4e2a\u6570\u5b57\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u8be5\u6570\u5b57\u7684\u81ea\u7136\u5bf9\u6570\u3002\u4f8b\u5982\uff0c<code>import math<\/code>\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7<code>math.log(10)<\/code>\u6765\u8ba1\u7b9710\u7684\u81ea\u7136\u5bf9\u6570\u3002<\/p>\n<p><strong>2. Python\u4e2d\u662f\u5426\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\uff1f<\/strong><br \/>\u9664\u4e86<code>math<\/code>\u6a21\u5757\uff0cPython\u7684<code>numpy<\/code>\u5e93\u4e5f\u63d0\u4f9b\u4e86\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u7684\u529f\u80fd\u3002\u901a\u8fc7\u4f7f\u7528<code>numpy.log()<\/code>\u51fd\u6570\uff0c\u60a8\u53ef\u4ee5\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\uff0c\u8fd9\u5bf9\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u7279\u522b\u6709\u7528\u3002\u4f8b\u5982\uff0c<code>import numpy as np<\/code>\u540e\uff0c\u901a\u8fc7<code>np.log([1, 2, 3])<\/code>\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5305\u542b\u6bcf\u4e2a\u5143\u7d20\u81ea\u7136\u5bf9\u6570\u7684\u6570\u7ec4\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5904\u7406Python\u4e2d\u81ea\u7136\u5bf9\u6570\u7684\u8d1f\u503c\u6216\u96f6\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5728\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u65f6\uff0c\u8d1f\u503c\u548c\u96f6\u4f1a\u5bfc\u81f4\u6570\u5b66\u4e0a\u7684\u9519\u8bef\u3002\u5728Python\u4e2d\uff0c\u5982\u679c\u5c1d\u8bd5\u5bf9\u8d1f\u6570\u6216\u96f6\u4f7f\u7528<code>math.log()<\/code>\u6216<code>numpy.log()<\/code>\uff0c\u5c06\u629b\u51fa<code>ValueError<\/code>\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5728\u8ba1\u7b97\u4e4b\u524d\u8fdb\u884c\u68c0\u67e5\uff0c\u786e\u4fdd\u8f93\u5165\u503c\u5927\u4e8e\u96f6\uff0c\u6216\u8005\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u5904\u7406\u8fd9\u4e9b\u8fb9\u754c\u60c5\u51b5\uff0c\u4f8b\u5982<code>if x &gt; 0: log_value = math.log(x)<\/code>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u8f93\u5165\u81ea\u7136\u5bf9\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528math\u6a21\u5757\u3001numpy\u6a21\u5757\u3001scipy\u6a21\u5757\u7b49\u3002\u4e3b\u8981\u65b9\u6cd5\u6709\uff1a\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1159077,"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\/1159066"}],"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=1159066"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159066\/revisions"}],"predecessor-version":[{"id":1159080,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1159066\/revisions\/1159080"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1159077"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1159066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1159066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1159066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}