{"id":1001843,"date":"2024-12-27T10:03:45","date_gmt":"2024-12-27T02:03:45","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1001843.html"},"modified":"2024-12-27T10:03:47","modified_gmt":"2024-12-27T02:03:47","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9a%e4%b9%89%e5%af%b9%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1001843.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5b9a\u4e49\u5bf9\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075842\/32b3c132-2350-48cd-95ff-9500fb656356.webp\" alt=\"python\u4e2d\u5982\u4f55\u5b9a\u4e49\u5bf9\u6570\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u5b9a\u4e49\u5bf9\u6570\u4e3b\u8981\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684\u6570\u5b66\u5e93<code>math<\/code>\u6216\u8005<code>numpy<\/code>\u5e93\u6765\u5b9e\u73b0\u3002<strong>\u901a\u8fc7\u4f7f\u7528<code>math.log()<\/code>\u51fd\u6570\u3001\u901a\u8fc7<code>numpy.log()<\/code>\u51fd\u6570\u3001\u5b9e\u73b0\u81ea\u5b9a\u4e49\u5bf9\u6570\u51fd\u6570<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u7528\u6765\u5b9a\u4e49\u5bf9\u6570\u3002\u5728Python\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>math.log()<\/code>\u51fd\u6570\uff0c\u5b83\u63d0\u4f9b\u4e86\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u548c\u5176\u4ed6\u5e95\u6570\u5bf9\u6570\u7684\u529f\u80fd\u3002<code>math.log(x, base)<\/code>\u51fd\u6570\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff0c\u5176\u4e2d<code>x<\/code>\u662f\u9700\u8981\u8ba1\u7b97\u5bf9\u6570\u7684\u6570\u503c\uff0c<code>base<\/code>\u662f\u5bf9\u6570\u7684\u5e95\u6570\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8ba1\u7b97\u4efb\u4f55\u5e95\u6570\u7684\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 <code>math<\/code> \u5e93\u5b9a\u4e49\u5bf9\u6570<\/h3>\n<\/p>\n<p><p>Python\u7684<code>math<\/code>\u5e93\u662f\u8ba1\u7b97\u5bf9\u6570\u7684\u9996\u9009\u5de5\u5177\u3002<code>math.log()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\u548c\u4efb\u610f\u5e95\u6570\u5bf9\u6570\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u81ea\u7136\u5bf9\u6570<\/h4>\n<\/p>\n<p><p>\u81ea\u7136\u5bf9\u6570\u662f\u4ee5\u6570\u5b66\u5e38\u6570e\u4e3a\u5e95\u7684\u5bf9\u6570\u3002Python\u4e2d\u7684<code>math.log(x)<\/code>\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97x\u7684\u81ea\u7136\u5bf9\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u53ea\u9700\u8981\u4e00\u4e2a\u53c2\u6570\uff0c\u5373\u5f85\u8ba1\u7b97\u5bf9\u6570\u7684\u6570\u503cx\u3002<\/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;The natural log of {x} is {natural_log}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5176\u4ed6\u5e95\u6570\u7684\u5bf9\u6570<\/h4>\n<\/p>\n<p><p><code>math.log()<\/code>\u51fd\u6570\u4e5f\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5176\u4ed6\u5e95\u6570\u7684\u5bf9\u6570\u3002\u901a\u8fc7\u4f20\u9012\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6765\u6307\u5b9a\u5e95\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>x = 100<\/p>\n<p>base = 10<\/p>\n<p>log_base_10 = math.log(x, base)<\/p>\n<p>print(f&quot;The log base {base} of {x} is {log_base_10}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 <code>numpy<\/code> \u5e93\u5b9a\u4e49\u5bf9\u6570<\/h3>\n<\/p>\n<p><p><code>numpy<\/code>\u5e93\u5728\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u65f6\u975e\u5e38\u6709\u7528\u3002\u5b83\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u5bf9\u6570\u51fd\u6570\uff0c\u9002\u7528\u4e8e\u6279\u91cf\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u81ea\u7136\u5bf9\u6570<\/h4>\n<\/p>\n<p><p><code>numpy.log()<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u81ea\u7136\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 10, 100])<\/p>\n<p>natural_logs = np.log(array)<\/p>\n<p>print(f&quot;The natural logs are {natural_logs}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5176\u4ed6\u5e95\u6570\u7684\u5bf9\u6570<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5176\u4ed6\u5e95\u6570\u7684\u5bf9\u6570\uff0c<code>numpy<\/code>\u6ca1\u6709\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u81ea\u7136\u5bf9\u6570\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 10, 100])<\/p>\n<p>base = 10<\/p>\n<p>logs_base_10 = np.log(array) \/ np.log(base)<\/p>\n<p>print(f&quot;The logs base {base} are {logs_base_10}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u81ea\u5b9a\u4e49\u5bf9\u6570\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5b9a\u4e49\u81ea\u5df1\u7684\u5bf9\u6570\u51fd\u6570\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u3002\u81ea\u5b9a\u4e49\u51fd\u6570\u53ef\u4ee5\u4e3a\u4f60\u63d0\u4f9b\u66f4\u9ad8\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u57fa\u672c\u5b9e\u73b0<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u5229\u7528\u81ea\u7136\u5bf9\u6570\u7684\u6027\u8d28\uff0c\u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5bf9\u6570\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def custom_log(x, base=math.e):<\/p>\n<p>    return math.log(x) \/ math.log(base)<\/p>\n<p>x = 1000<\/p>\n<p>base = 10<\/p>\n<p>log_base_10 = custom_log(x, base)<\/p>\n<p>print(f&quot;The custom log base {base} of {x} is {log_base_10}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u5f02\u5e38\u548c\u8fb9\u754c\u60c5\u51b5<\/h4>\n<\/p>\n<p><p>\u5f53\u5904\u7406\u5bf9\u6570\u65f6\uff0c\u9700\u8981\u8003\u8651\u8fb9\u754c\u60c5\u51b5\uff0c\u4f8b\u5982\u96f6\u6216\u8d1f\u6570\u8f93\u5165\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_log(x, base=math.e):<\/p>\n<p>    if x &lt;= 0:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;Logarithm is undefined for non-positive values.&quot;)<\/p>\n<p>    return math.log(x) \/ math.log(base)<\/p>\n<p>try:<\/p>\n<p>    x = 0<\/p>\n<p>    base = 10<\/p>\n<p>    log_base_10 = safe_log(x, base)<\/p>\n<p>    print(f&quot;The safe log base {base} of {x} is {log_base_10}&quot;)<\/p>\n<p>except ValueError as e:<\/p>\n<p>    print(e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5e94\u7528\u5bf9\u6570\u7684\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u5bf9\u6570\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u548c\u4f7f\u7528\u5bf9\u6570\u51fd\u6570\u5bf9\u4e8e\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u5f52\u4e00\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5bf9\u6570\u53d8\u6362\u5e38\u7528\u4e8e\u6570\u636e\u5f52\u4e00\u5316\uff0c\u4ee5\u51cf\u5c0f\u6570\u636e\u7684\u504f\u6001\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([1, 10, 100, 1000])<\/p>\n<p>log_transformed_data = np.log(data)<\/p>\n<p>print(f&quot;Log transformed data: {log_transformed_data}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6307\u6570\u589e\u957f\u7684\u5206\u6790<\/h4>\n<\/p>\n<p><p>\u5bf9\u6570\u5e38\u7528\u4e8e\u5206\u6790\u6307\u6570\u589e\u957f\u7684\u73b0\u8c61\uff0c\u4f8b\u5982\u4eba\u53e3\u589e\u957f\u3001\u75c5\u6bd2\u4f20\u64ad\u7b49\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>time = np.arange(1, 11)<\/p>\n<p>population = np.exp(time)<\/p>\n<p>log_population = np.log(population)<\/p>\n<p>plt.plot(time, population, label=&#39;Exponential Growth&#39;)<\/p>\n<p>plt.plot(time, log_population, label=&#39;Log of Growth&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5bf9\u6570\u51fd\u6570\u7684\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8ba1\u7b97\u5bf9\u6570\u53ef\u80fd\u4f1a\u6210\u4e3a\u6027\u80fd\u74f6\u9888\u3002\u7406\u89e3\u5982\u4f55\u4f18\u5316\u5bf9\u6570\u8ba1\u7b97\u662f\u63d0\u9ad8\u7a0b\u5e8f\u6548\u7387\u7684\u91cd\u8981\u65b9\u9762\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u6570\u7ec4\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u91cf\u6570\u636e\uff0c\u4f7f\u7528\u6570\u7ec4\u8fd0\u7b97\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.random.rand(1000000)<\/p>\n<p>log_data = np.log(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u9884\u8ba1\u7b97\u548c\u7f13\u5b58<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u91cd\u590d\u8ba1\u7b97\u7684\u5bf9\u6570\u503c\uff0c\u53ef\u4ee5\u8003\u8651\u9884\u8ba1\u7b97\u548c\u7f13\u5b58\u4ee5\u51cf\u5c11\u8ba1\u7b97\u5f00\u9500\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>from functools import lru_cache<\/p>\n<p>@lru_cache(maxsize=None)<\/p>\n<p>def cached_log(x, base=math.e):<\/p>\n<p>    return math.log(x) \/ math.log(base)<\/p>\n<p>x_values = [1, 10, 100, 1000]<\/p>\n<p>logs = [cached_log(x) for x in x_values]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u9ad8\u6548\u5730\u5b9a\u4e49\u548c\u4f7f\u7528\u5bf9\u6570\u51fd\u6570\uff0c\u5e76\u5728\u5404\u79cd\u5e94\u7528\u573a\u666f\u4e2d\u5229\u7528\u5bf9\u6570\u7684\u6027\u8d28\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u4f7f\u7528\u5bf9\u6570\u51fd\u6570\uff1f<\/strong><br \/>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u8ba1\u7b97\u5bf9\u6570\uff0c\u6700\u5e38\u7528\u7684\u662f\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u4e2d\u7684<code>log<\/code>\u51fd\u6570\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>math.log(x, base)<\/code>\u6765\u8ba1\u7b97x\u4ee5base\u4e3a\u5e95\u7684\u5bf9\u6570\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u5e95\u6570\uff0c\u9ed8\u8ba4\u662f\u4ee5e\u4e3a\u5e95\u7684\u81ea\u7136\u5bf9\u6570\u3002\u4f8b\u5982\uff0c<code>math.log(10)<\/code>\u8ba1\u7b97\u7684\u662f10\u7684\u81ea\u7136\u5bf9\u6570\uff0c\u800c<code>math.log(100, 10)<\/code>\u5219\u662f\u8ba1\u7b97100\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8d1f\u6570\u6216\u96f6\u7684\u5bf9\u6570\u8ba1\u7b97\uff1f<\/strong><br \/>\u5728\u6570\u5b66\u4e0a\uff0c\u8d1f\u6570\u548c\u96f6\u7684\u5bf9\u6570\u662f\u672a\u5b9a\u4e49\u7684\uff0c\u56e0\u6b64\u5728Python\u4e2d\u8ba1\u7b97\u8fd9\u4e9b\u503c\u65f6\u4f1a\u5f15\u53d1<code>ValueError<\/code>\u5f02\u5e38\u3002\u5728\u8fdb\u884c\u5bf9\u6570\u8fd0\u7b97\u4e4b\u524d\uff0c\u786e\u4fdd\u4f20\u5165\u7684\u53c2\u6570\u662f\u6b63\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u68c0\u67e5\u8f93\u5165\u503c\u7684\u6709\u6548\u6027\uff0c\u4f8b\u5982\uff1a<code>if x &gt; 0:<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u5f02\u5e38\u7684\u53d1\u751f\u3002<\/p>\n<p><strong>\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u5bf9\u6570\u7684\u5e94\u7528\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5bf9\u6570\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u6570\u636e\u5206\u5e03\u3001\u7279\u5f81\u7f29\u653e\u548c\u6a21\u578b\u8bad\u7ec3\u65f6\u3002\u5bf9\u6570\u53d8\u6362\u6709\u52a9\u4e8e\u7f29\u5c0f\u6570\u636e\u8303\u56f4\uff0c\u51cf\u5c11\u504f\u6001\uff0c\u4f7f\u6570\u636e\u66f4\u63a5\u8fd1\u6b63\u6001\u5206\u5e03\u3002\u8fd9\u5728\u8bb8\u591a\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u4e2d\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u548c\u7cbe\u5ea6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5b9a\u4e49\u5bf9\u6570\u4e3b\u8981\u901a\u8fc7\u4f7f\u7528\u5185\u7f6e\u7684\u6570\u5b66\u5e93math\u6216\u8005numpy\u5e93\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u4f7f\u7528math.log()\u51fd [&hellip;]","protected":false},"author":3,"featured_media":1001853,"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\/1001843"}],"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=1001843"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001843\/revisions"}],"predecessor-version":[{"id":1001857,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1001843\/revisions\/1001857"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1001853"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1001843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1001843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1001843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}