{"id":1097706,"date":"2025-01-08T15:14:27","date_gmt":"2025-01-08T07:14:27","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1097706.html"},"modified":"2025-01-08T15:14:30","modified_gmt":"2025-01-08T07:14:30","slug":"python%e9%87%8c%e9%9d%a2%e5%af%b9%e6%95%b0%e5%87%bd%e6%95%b0%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1097706.html","title":{"rendered":"python\u91cc\u9762\u5bf9\u6570\u51fd\u6570\u51fd\u6570\u5982\u4f55"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24212334\/6eeede79-e934-46fc-8019-fd799b7d1e9e.webp\" alt=\"python\u91cc\u9762\u5bf9\u6570\u51fd\u6570\u51fd\u6570\u5982\u4f55\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5e38\u7528\u7684\u5bf9\u6570\u51fd\u6570\u5305\u62ec<code>math.log()<\/code>\u3001<code>math.log10()<\/code>\u3001<code>math.log2()<\/code>\u7b49\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u8fdb\u884c\u6570\u5b66\u8ba1\u7b97\u65f6\u6c42\u53d6\u4e0d\u540c\u5e95\u6570\u7684\u5bf9\u6570\u3002<\/strong> <code>math.log()<\/code>\u51fd\u6570\u53ef\u4ee5\u6c42\u4efb\u610f\u5e95\u6570\u7684\u5bf9\u6570\uff0c\u800c<code>math.log10()<\/code>\u548c<code>math.log2()<\/code>\u5206\u522b\u7528\u4e8e\u6c42\u4ee510\u4e3a\u5e95\u548c\u4ee52\u4e3a\u5e95\u7684\u5bf9\u6570\u3002\u8fd9\u91cc\u91cd\u70b9\u4ecb\u7ecd\u4e00\u4e0b<code>math.log()<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u7684\u7075\u6d3b\u6027\u4f7f\u5176\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u975e\u5e38\u5e7f\u6cdb\u3002<\/p>\n<\/p>\n<p><p><code>math.log()<\/code>\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u4e24\u4e2a\u53c2\u6570\u6765\u6c42\u4e0d\u540c\u5e95\u6570\u7684\u5bf9\u6570\uff1a\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u9700\u8981\u6c42\u5bf9\u6570\u7684\u6570\u503c\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5bf9\u6570\u7684\u5e95\u6570\uff08\u5982\u679c\u4e0d\u63d0\u4f9b\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u9ed8\u8ba4\u5e95\u6570\u4e3a\u81ea\u7136\u5bf9\u6570e\uff09\u3002\u4f8b\u5982\uff0c\u8981\u8ba1\u7b97\u4ee52\u4e3a\u5e95\u76848\u7684\u5bf9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math.log(8, 2)<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u57fa\u672c\u5bf9\u6570\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1\u3001math.log()<\/h4>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c<code>math.log()<\/code>\u51fd\u6570\u662f\u6700\u57fa\u672c\u7684\u5bf9\u6570\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u4efb\u610f\u5e95\u6570\u7684\u5bf9\u6570\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5b83\u8ba1\u7b97\u7684\u662f\u81ea\u7136\u5bf9\u6570\uff08\u4ee5e\u4e3a\u5e95\u7684\u5bf9\u6570\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>result = math.log(10)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a2.302585092994046<\/p>\n<h2><strong>\u8ba1\u7b97\u4ee52\u4e3a\u5e95\u7684\u5bf9\u6570<\/strong><\/h2>\n<p>result = math.log(8, 2)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a3.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528<code>math.log()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u4e0d\u540c\u5e95\u6570\u7684\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><h4>2\u3001math.log10()<\/h4>\n<\/p>\n<p><p><code>math.log10()<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\uff0c\u8fd9\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u5de5\u7a0b\u8ba1\u7b97\u4e2d\u975e\u5e38\u5e38\u89c1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u8ba1\u7b97\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570<\/strong><\/h2>\n<p>result = math.log10(100)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a2.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001math.log2()<\/h4>\n<\/p>\n<p><p><code>math.log2()<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u4ee52\u4e3a\u5e95\u7684\u5bf9\u6570\uff0c\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u4e8c\u8fdb\u5236\u7cfb\u7edf\u4ee52\u4e3a\u57fa\u7840\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u8ba1\u7b97\u4ee52\u4e3a\u5e95\u7684\u5bf9\u6570<\/strong><\/h2>\n<p>result = math.log2(8)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a3.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bf9\u6570\u51fd\u6570\u7684\u5b9e\u9645\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u4fe1\u606f\u71b5\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u4fe1\u606f\u71b5\u662f\u4fe1\u606f\u8bba\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u7528\u4e8e\u5ea6\u91cf\u4fe1\u606f\u7684\u4e0d\u786e\u5b9a\u6027\u3002\u5728\u8ba1\u7b97\u4fe1\u606f\u71b5\u65f6\uff0c\u5bf9\u6570\u51fd\u6570\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def entropy(probabilities):<\/p>\n<p>    return -sum(p * math.log2(p) for p in probabilities if p &gt; 0)<\/p>\n<h2><strong>\u793a\u4f8b\u6982\u7387\u5206\u5e03<\/strong><\/h2>\n<p>probabilities = [0.25, 0.25, 0.25, 0.25]<\/p>\n<p>result = entropy(probabilities)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a2.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u7684\u5bf9\u6570\u635f\u5931\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u5bf9\u6570\u635f\u5931\u51fd\u6570\uff08Log Loss\uff09\u662f\u8bc4\u4f30\u5206\u7c7b\u6a21\u578b\u6027\u80fd\u7684\u4e00\u79cd\u5e38\u7528\u65b9\u6cd5\uff0c\u7279\u522b\u662f\u5728\u4e8c\u5206\u7c7b\u95ee\u9898\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def log_loss(y_true, y_pred):<\/p>\n<p>    return -sum(y * math.log(p) + (1 - y) * math.log(1 - p) for y, p in zip(y_true, y_pred)) \/ len(y_true)<\/p>\n<h2><strong>\u793a\u4f8b\u771f\u5b9e\u6807\u7b7e\u548c\u9884\u6d4b\u6982\u7387<\/strong><\/h2>\n<p>y_true = [1, 0, 1, 0]<\/p>\n<p>y_pred = [0.9, 0.1, 0.8, 0.2]<\/p>\n<p>result = log_loss(y_true, y_pred)<\/p>\n<p>print(result)  # \u8f93\u51fa\uff1a0.164252033486018<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5bf9\u6570\u51fd\u6570\u5728\u6570\u636e\u5904\u7406\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u6807\u51c6\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5bf9\u6570\u53d8\u6362\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6570\u636e\u6807\u51c6\u5316\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4f7f\u6570\u636e\u66f4\u52a0\u7b26\u5408\u6b63\u6001\u5206\u5e03\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = [1, 10, 100, 1000, 10000]<\/p>\n<p>log_transformed_data = np.log(data)<\/p>\n<p>print(log_transformed_data)  # \u8f93\u51fa\uff1a[0.         2.30258509 4.60517019 6.90775528 9.21034037]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u6307\u6570\u589e\u957f\u7684\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5728\u91d1\u878d\u3001\u4eba\u53e3\u7edf\u8ba1\u7b49\u9886\u57df\uff0c\u6570\u636e\u5e38\u5e38\u5448\u6307\u6570\u589e\u957f\u8d8b\u52bf\u3002\u901a\u8fc7\u5bf9\u6570\u53d8\u6362\uff0c\u53ef\u4ee5\u5c06\u6307\u6570\u589e\u957f\u6570\u636e\u8f6c\u6362\u4e3a\u7ebf\u6027\u6570\u636e\uff0c\u4fbf\u4e8e\u5206\u6790\u548c\u5efa\u6a21\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>years = np.array([1, 2, 3, 4, 5])<\/p>\n<p>population = np.array([100, 200, 400, 800, 1600])<\/p>\n<p>log_population = np.log(population)<\/p>\n<h2><strong>\u4f7f\u7528\u7ebf\u6027\u56de\u5f52\u62df\u5408\u5bf9\u6570\u53d8\u6362\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>from sklearn.linear_model import LinearRegression<\/p>\n<p>model = LinearRegression()<\/p>\n<p>model.fit(years.reshape(-1, 1), log_population)<\/p>\n<p>print(&quot;Slope:&quot;, model.coef_[0])  # \u8f93\u51fa\uff1a0.6931471805599453<\/p>\n<p>print(&quot;Intercept:&quot;, model.intercept_)  # \u8f93\u51fa\uff1a4.605170185988091<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5bf9\u6570\u51fd\u6570\u5728\u7edf\u8ba1\u5b66\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u5bf9\u6570\u6b63\u6001\u5206\u5e03<\/h4>\n<\/p>\n<p><p>\u5bf9\u6570\u6b63\u6001\u5206\u5e03\u662f\u4e00\u79cd\u6982\u7387\u5206\u5e03\uff0c\u5e7f\u6cdb\u7528\u4e8e\u91d1\u878d\u3001\u73af\u5883\u79d1\u5b66\u7b49\u9886\u57df\u3002\u5728\u8fd9\u79cd\u5206\u5e03\u4e0b\uff0c\u6570\u636e\u7684\u5bf9\u6570\u503c\u670d\u4ece\u6b63\u6001\u5206\u5e03\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<h2><strong>\u751f\u6210\u5bf9\u6570\u6b63\u6001\u5206\u5e03\u6570\u636e<\/strong><\/h2>\n<p>mu, sigma = 0, 0.1  # \u5747\u503c\u548c\u6807\u51c6\u5dee<\/p>\n<p>log_normal_data = np.random.lognormal(mu, sigma, 1000)<\/p>\n<h2><strong>\u7ed8\u5236\u76f4\u65b9\u56fe<\/strong><\/h2>\n<p>plt.hist(log_normal_data, bins=50, density=True)<\/p>\n<p>plt.title(&quot;Log-normal distribution&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bf9\u6570\u56de\u5f52\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u5bf9\u6570\u56de\u5f52\u6a21\u578b\u662f\u4e00\u79cd\u5e7f\u6cdb\u5e94\u7528\u4e8e\u7ecf\u6d4e\u5b66\u548c\u751f\u7269\u7edf\u8ba1\u5b66\u7684\u56de\u5f52\u6a21\u578b\uff0c\u7528\u4e8e\u5904\u7406\u56e0\u53d8\u91cf\u4e3a\u5bf9\u6570\u53d8\u6362\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import statsmodels.api as sm<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>x = np.array([1, 2, 3, 4, 5])<\/p>\n<p>y = np.array([2.3, 2.8, 3.6, 4.5, 5.1])<\/p>\n<h2><strong>\u5bf9y\u8fdb\u884c\u5bf9\u6570\u53d8\u6362<\/strong><\/h2>\n<p>log_y = np.log(y)<\/p>\n<h2><strong>\u6dfb\u52a0\u5e38\u6570\u9879<\/strong><\/h2>\n<p>x = sm.add_constant(x)<\/p>\n<h2><strong>\u62df\u5408\u5bf9\u6570\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = sm.OLS(log_y, x).fit()<\/p>\n<p>print(model.summary())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5bf9\u6570\u51fd\u6570\u5728\u4fe1\u53f7\u5904\u7406\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u5bf9\u6570\u5e45\u5ea6\u8c31<\/h4>\n<\/p>\n<p><p>\u5728\u4fe1\u53f7\u5904\u7406\u9886\u57df\uff0c\u9891\u8c31\u5206\u6790\u662f\u4e00\u4e2a\u91cd\u8981\u4efb\u52a1\u3002\u5bf9\u6570\u5e45\u5ea6\u8c31\uff08Logarithmic Amplitude Spectrum\uff09\u53ef\u4ee5\u66f4\u597d\u5730\u5c55\u793a\u4fe1\u53f7\u5728\u9891\u57df\u4e2d\u7684\u7279\u6027\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<h2><strong>\u751f\u6210\u793a\u4f8b\u4fe1\u53f7<\/strong><\/h2>\n<p>t = np.linspace(0, 1, 500)<\/p>\n<p>signal = np.sin(2 * np.pi * 50 * t) + np.sin(2 * np.pi * 120 * t)<\/p>\n<h2><strong>\u8ba1\u7b97\u5085\u91cc\u53f6\u53d8\u6362<\/strong><\/h2>\n<p>fft_result = np.fft.fft(signal)<\/p>\n<h2><strong>\u8ba1\u7b97\u5bf9\u6570\u5e45\u5ea6\u8c31<\/strong><\/h2>\n<p>log_amplitude_spectrum = np.log(np.abs(fft_result))<\/p>\n<h2><strong>\u7ed8\u5236\u5bf9\u6570\u5e45\u5ea6\u8c31<\/strong><\/h2>\n<p>plt.plot(log_amplitude_spectrum)<\/p>\n<p>plt.title(&quot;Logarithmic Amplitude Spectrum&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bf9\u6570\u538b\u7f29<\/h4>\n<\/p>\n<p><p>\u5728\u97f3\u9891\u5904\u7406\u548c\u56fe\u50cf\u5904\u7406\u7b49\u9886\u57df\uff0c\u5bf9\u6570\u538b\u7f29\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6280\u672f\uff0c\u7528\u4e8e\u589e\u5f3a\u4fe1\u53f7\u7684\u52a8\u6001\u8303\u56f4\uff0c\u4f7f\u5176\u66f4\u6613\u4e8e\u5904\u7406\u548c\u5206\u6790\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<h2><strong>\u751f\u6210\u793a\u4f8b\u4fe1\u53f7<\/strong><\/h2>\n<p>t = np.linspace(0, 1, 500)<\/p>\n<p>signal = np.sin(2 * np.pi * 50 * t) + np.sin(2 * np.pi * 120 * t)<\/p>\n<h2><strong>\u5bf9\u4fe1\u53f7\u8fdb\u884c\u5bf9\u6570\u538b\u7f29<\/strong><\/h2>\n<p>log_compressed_signal = np.log1p(signal)<\/p>\n<h2><strong>\u7ed8\u5236\u539f\u59cb\u4fe1\u53f7\u548c\u5bf9\u6570\u538b\u7f29\u540e\u7684\u4fe1\u53f7<\/strong><\/h2>\n<p>plt.subplot(2, 1, 1)<\/p>\n<p>plt.plot(signal)<\/p>\n<p>plt.title(&quot;Original Signal&quot;)<\/p>\n<p>plt.subplot(2, 1, 2)<\/p>\n<p>plt.plot(log_compressed_signal)<\/p>\n<p>plt.title(&quot;Log Compressed Signal&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5bf9\u6570\u51fd\u6570\u5728\u91d1\u878d\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>1\u3001\u5bf9\u6570\u6536\u76ca\u7387<\/h4>\n<\/p>\n<p><p>\u5728\u91d1\u878d\u9886\u57df\uff0c\u5bf9\u6570\u6536\u76ca\u7387\uff08Log Returns\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6536\u76ca\u7387\u8ba1\u7b97\u65b9\u6cd5\uff0c\u80fd\u591f\u66f4\u597d\u5730\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u80a1\u7968\u4ef7\u683c<\/strong><\/h2>\n<p>prices = np.array([100, 105, 110, 120, 115])<\/p>\n<h2><strong>\u8ba1\u7b97\u5bf9\u6570\u6536\u76ca\u7387<\/strong><\/h2>\n<p>log_returns = np.diff(np.log(prices))<\/p>\n<p>print(log_returns)  # \u8f93\u51fa\uff1a[0.04879  0.04652  0.09097 -0.04256]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5bf9\u6570\u6b63\u6001\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u5bf9\u6570\u6b63\u6001\u6a21\u578b\u662f\u4e00\u79cd\u91d1\u878d\u6a21\u578b\uff0c\u5047\u8bbe\u8d44\u4ea7\u4ef7\u683c\u670d\u4ece\u5bf9\u6570\u6b63\u6001\u5206\u5e03\uff0c\u7528\u4e8e\u671f\u6743\u5b9a\u4ef7\u548c\u98ce\u9669\u7ba1\u7406\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<h2><strong>\u751f\u6210\u5bf9\u6570\u6b63\u6001\u5206\u5e03\u7684\u8d44\u4ea7\u4ef7\u683c<\/strong><\/h2>\n<p>mu, sigma = 0.1, 0.2  # \u5e74\u5316\u5747\u503c\u548c\u6ce2\u52a8\u7387<\/p>\n<p>S0 = 100  # \u521d\u59cb\u4ef7\u683c<\/p>\n<p>T = 1  # \u65f6\u95f4\u957f\u5ea6<\/p>\n<p>dt = 1\/252  # \u65f6\u95f4\u6b65\u957f<\/p>\n<p>N = int(T\/dt)  # \u6b65\u6570<\/p>\n<h2><strong>\u6a21\u62df\u8d44\u4ea7\u4ef7\u683c\u8def\u5f84<\/strong><\/h2>\n<p>prices = S0 * np.exp(np.cumsum((mu - 0.5 * sigma2) * dt + sigma * np.sqrt(dt) * np.random.randn(N)))<\/p>\n<h2><strong>\u7ed8\u5236\u8d44\u4ea7\u4ef7\u683c\u8def\u5f84<\/strong><\/h2>\n<p>plt.plot(prices)<\/p>\n<p>plt.title(&quot;Asset Price Path&quot;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5bf9\u6570\u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><h4>1\u3001NumPy\u4e2d\u7684\u5bf9\u6570\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0cNumPy\u63d0\u4f9b\u4e86\u9ad8\u6027\u80fd\u7684\u5bf9\u6570\u51fd\u6570\uff0c\u5982<code>np.log()<\/code>\u3001<code>np.log10()<\/code>\u548c<code>np.log2()<\/code>\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u5927\u89c4\u6a21\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.rand(1000000)<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u7684\u5bf9\u6570\u51fd\u6570<\/strong><\/h2>\n<p>log_data = np.log(data)<\/p>\n<p>print(log_data[:5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5e76\u884c\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406\u8d85\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5e76\u884c\u8ba1\u7b97\u6280\u672f\uff0c\u5982\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\uff0c\u52a0\u901f\u5bf9\u6570\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from multiprocessing import Pool<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u5927\u89c4\u6a21\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.rand(1000000)<\/p>\n<h2><strong>\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u5757<\/strong><\/h2>\n<p>chunks = np.array_split(data, 4)<\/p>\n<h2><strong>\u5b9a\u4e49\u8ba1\u7b97\u5bf9\u6570\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def compute_log(chunk):<\/p>\n<p>    return np.log(chunk)<\/p>\n<h2><strong>\u4f7f\u7528\u591a\u8fdb\u7a0b\u5e76\u884c\u8ba1\u7b97<\/strong><\/h2>\n<p>with Pool(4) as pool:<\/p>\n<p>    result = pool.map(compute_log, chunks)<\/p>\n<h2><strong>\u5408\u5e76\u7ed3\u679c<\/strong><\/h2>\n<p>log_data = np.concatenate(result)<\/p>\n<p>print(log_data[:5])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5bf9\u6570\u51fd\u6570\u7684\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u8d1f\u6570\u548c\u96f6<\/h4>\n<\/p>\n<p><p>\u5bf9\u6570\u51fd\u6570\u5728\u5904\u7406\u8d1f\u6570\u548c\u96f6\u65f6\u4f1a\u51fa\u73b0\u6570\u5b66\u9519\u8bef\uff0c\u56e0\u6b64\u5728\u8ba1\u7b97\u524d\u9700\u8981\u8fdb\u884c\u6570\u636e\u6e05\u7406\u548c\u9884\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = np.array([1, 10, 100, -10, 0])<\/p>\n<h2><strong>\u6e05\u7406\u8d1f\u6570\u548c\u96f6<\/strong><\/h2>\n<p>cleaned_data = data[data &gt; 0]<\/p>\n<h2><strong>\u8ba1\u7b97\u5bf9\u6570<\/strong><\/h2>\n<p>log_data = np.log(cleaned_data)<\/p>\n<p>print(log_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u5c3a\u5ea6\u95ee\u9898<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e0d\u540c\u5c3a\u5ea6\u7684\u6570\u636e\u53ef\u80fd\u9700\u8981\u4e0d\u540c\u7684\u5bf9\u6570\u53d8\u6362\u3002\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u9002\u5f53\u7684\u5bf9\u6570\u5e95\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u793a\u4f8b\u6570\u636e<\/strong><\/h2>\n<p>data = np.array([1, 10, 100, 1000, 10000])<\/p>\n<h2><strong>\u9009\u62e9\u4e0d\u540c\u7684\u5bf9\u6570\u5e95\u6570<\/strong><\/h2>\n<p>log_data_e = np.log(data)<\/p>\n<p>log_data_10 = np.log10(data)<\/p>\n<p>log_data_2 = np.log2(data)<\/p>\n<p>print(&quot;Natural log:&quot;, log_data_e)<\/p>\n<p>print(&quot;Log base 10:&quot;, log_data_10)<\/p>\n<p>print(&quot;Log base 2:&quot;, log_data_2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5bf9\u6570\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5904\u7406\u3001\u673a\u5668\u5b66\u4e60\u3001\u7edf\u8ba1\u5b66\u3001\u4fe1\u53f7\u5904\u7406\u548c\u91d1\u878d\u7b49\u9886\u57df\u3002\u901a\u8fc7\u7075\u6d3b\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u548c<code>NumPy<\/code>\u5e93\u4e2d\u7684\u5bf9\u6570\u51fd\u6570\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u89e3\u51b3\u5404\u79cd\u5b9e\u9645\u95ee\u9898\u3002\u5728\u5e94\u7528\u5bf9\u6570\u51fd\u6570\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6570\u636e\u7684\u9884\u5904\u7406\u548c\u5c3a\u5ea6\u9009\u62e9\uff0c\u4ee5\u786e\u4fdd\u8ba1\u7b97\u7ed3\u679c\u7684\u51c6\u786e\u6027\u548c\u5408\u7406\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u4e2d\u5982\u4f55\u4f7f\u7528\u5bf9\u6570\u51fd\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>math<\/code>\u6a21\u5757\u6216<code>numpy<\/code>\u5e93\u6765\u8ba1\u7b97\u5bf9\u6570\u3002<code>math.log(x, base)<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u4ee5<code>base<\/code>\u4e3a\u5e95\u7684<code>x<\/code>\u7684\u5bf9\u6570\u3002\u5982\u679c\u4e0d\u6307\u5b9a<code>base<\/code>\uff0c\u9ed8\u8ba4\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\uff08\u4ee5e\u4e3a\u5e95\uff09\u3002\u4f8b\u5982\uff0c<code>math.log(10)<\/code>\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\uff0c\u800c<code>math.log(10, 10)<\/code>\u5219\u8ba1\u7b97\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\u3002<\/p>\n<p><strong>Python\u7684\u5bf9\u6570\u51fd\u6570\u652f\u6301\u54ea\u4e9b\u5e95\u6570\uff1f<\/strong><br \/>Python\u7684\u5bf9\u6570\u51fd\u6570\u652f\u6301\u591a\u79cd\u5e95\u6570\uff0c\u5305\u62ec\u81ea\u7136\u5bf9\u6570\uff08\u5e95\u6570\u4e3ae\uff09\u3001\u5e38\u7528\u5bf9\u6570\uff08\u5e95\u6570\u4e3a10\uff09\u4ee5\u53ca\u4efb\u610f\u6b63\u6570\u5e95\u6570\u3002\u4f7f\u7528<code>math.log(x, base)<\/code>\u65f6\uff0c<code>base<\/code>\u53ef\u4ee5\u662f\u4efb\u610f\u6b63\u6570\uff0c\u53ea\u8981<code>x<\/code>\u4e5f\u4e3a\u6b63\u6570\u3002<\/p>\n<p><strong>\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u5982\u4f55\u5e94\u7528\u5bf9\u6570\u51fd\u6570\uff1f<\/strong><br \/>\u5728\u6570\u636e\u79d1\u5b66\u4e2d\uff0c\u5bf9\u6570\u51fd\u6570\u5e38\u7528\u4e8e\u6570\u636e\u9884\u5904\u7406\u548c\u7279\u5f81\u5de5\u7a0b\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5177\u6709\u6307\u6570\u589e\u957f\u7279\u5f81\u7684\u6570\u636e\u65f6\u3002\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\uff0c\u53ef\u4ee5\u4f7f\u6570\u636e\u66f4\u7b26\u5408\u6b63\u6001\u5206\u5e03\uff0c\u63d0\u9ad8\u6a21\u578b\u7684\u7a33\u5b9a\u6027\u548c\u9884\u6d4b\u80fd\u529b\u3002\u6b64\u5916\uff0c\u5bf9\u6570\u51fd\u6570\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u5904\u7406\u5927\u8303\u56f4\u6570\u503c\uff0c\u4ece\u800c\u51cf\u5c11\u6570\u636e\u7684\u504f\u6001\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5e38\u7528\u7684\u5bf9\u6570\u51fd\u6570\u5305\u62ecmath.log()\u3001math.log10()\u3001math.log2()\u7b49\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1097712,"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\/1097706"}],"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=1097706"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097706\/revisions"}],"predecessor-version":[{"id":1097715,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1097706\/revisions\/1097715"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1097712"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1097706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1097706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1097706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}