{"id":1188045,"date":"2025-01-15T20:12:29","date_gmt":"2025-01-15T12:12:29","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1188045.html"},"modified":"2025-01-15T20:12:32","modified_gmt":"2025-01-15T12:12:32","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e5%ba%8f%e5%88%97%e5%8f%96%e5%af%b9%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1188045.html","title":{"rendered":"python\u5982\u4f55\u5bf9\u5e8f\u5217\u53d6\u5bf9\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140444\/f13ad524-b753-432b-a1d4-2bb572dfc693.webp\" alt=\"python\u5982\u4f55\u5bf9\u5e8f\u5217\u53d6\u5bf9\u6570\" \/><\/p>\n<p><p> <strong>Python\u5bf9\u5e8f\u5217\u53d6\u5bf9\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001Pandas\u5e93\u3001\u4ee5\u53camath\u5e93\u3002NumPy\u5e93\u63d0\u4f9b\u7684\u5411\u91cf\u5316\u64cd\u4f5c\u6548\u7387\u9ad8\u3001Pandas\u5e93\u9002\u7528\u4e8e\u5904\u7406\u6570\u636e\u6846\u3001math\u5e93\u5219\u9002\u5408\u5904\u7406\u5355\u4e2a\u6570\u503c\u3002<\/strong>\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u51e0\u79cd\u65b9\u6cd5\u5bf9\u5e8f\u5217\u53d6\u5bf9\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001NumPy\u5e93\u5bf9\u5e8f\u5217\u53d6\u5bf9\u6570<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9\u6570\u7ec4\u8fdb\u884c\u9ad8\u6548\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002\u4f7f\u7528NumPy\u5bf9\u5e8f\u5217\u53d6\u5bf9\u6570\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u5bf9\u6570\u7ec4\u53d6\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>log_arr = np.log(arr)<\/p>\n<p>print(&quot;\u81ea\u7136\u5bf9\u6570:&quot;, log_arr)<\/p>\n<h2><strong>\u5bf9\u6570\u7ec4\u53d6\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570<\/strong><\/h2>\n<p>log10_arr = np.log10(arr)<\/p>\n<p>print(&quot;\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570:&quot;, log10_arr)<\/p>\n<h2><strong>\u5bf9\u6570\u7ec4\u53d6\u4ee52\u4e3a\u5e95\u7684\u5bf9\u6570<\/strong><\/h2>\n<p>log2_arr = np.log2(arr)<\/p>\n<p>print(&quot;\u4ee52\u4e3a\u5e95\u7684\u5bf9\u6570:&quot;, log2_arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u7684\u4f18\u52bf\u5728\u4e8e\u80fd\u591f\u5bf9\u6574\u4e2a\u6570\u7ec4\u8fdb\u884c\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u975e\u5e38\u5927\u7684\u6570\u7ec4\uff0c\u9700\u8981\u5bf9\u6bcf\u4e2a\u5143\u7d20\u53d6\u5bf9\u6570\uff0cNumPy\u7684\u5411\u91cf\u5316\u64cd\u4f5c\u80fd\u591f\u6781\u5927\u5730\u7f29\u77ed\u8ba1\u7b97\u65f6\u95f4\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001Pandas\u5e93\u5bf9\u5e8f\u5217\u53d6\u5bf9\u6570<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u5e38\u7528\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u9002\u5408\u5904\u7406\u5305\u542b\u591a\u79cd\u7c7b\u578b\u6570\u636e\u7684\u5927\u578b\u6570\u636e\u96c6\u3002\u4f7f\u7528Pandas\u5bf9\u5e8f\u5217\u53d6\u5bf9\u6570\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;values&#39;: [1, 2, 3, 4, 5]})<\/p>\n<h2><strong>\u5bf9\u6570\u636e\u6846\u4e2d\u7684\u5e8f\u5217\u53d6\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>df[&#39;log_values&#39;] = np.log(df[&#39;values&#39;])<\/p>\n<p>print(&quot;\u81ea\u7136\u5bf9\u6570:\\n&quot;, df)<\/p>\n<h2><strong>\u5bf9\u6570\u636e\u6846\u4e2d\u7684\u5e8f\u5217\u53d6\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570<\/strong><\/h2>\n<p>df[&#39;log10_values&#39;] = np.log10(df[&#39;values&#39;])<\/p>\n<p>print(&quot;\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570:\\n&quot;, df)<\/p>\n<h2><strong>\u5bf9\u6570\u636e\u6846\u4e2d\u7684\u5e8f\u5217\u53d6\u4ee52\u4e3a\u5e95\u7684\u5bf9\u6570<\/strong><\/h2>\n<p>df[&#39;log2_values&#39;] = np.log2(df[&#39;values&#39;])<\/p>\n<p>print(&quot;\u4ee52\u4e3a\u5e95\u7684\u5bf9\u6570:\\n&quot;, df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>Pandas\u7684\u4f18\u52bf\u5728\u4e8e\u80fd\u591f\u5f88\u65b9\u4fbf\u5730\u5904\u7406\u6570\u636e\u6846\u4e2d\u7684\u6570\u636e\uff0c\u9002\u5408\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u9884\u5904\u7406\u3002\u901a\u8fc7\u4e0eNumPy\u7ed3\u5408\u4f7f\u7528\uff0cPandas\u80fd\u591f\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u64cd\u4f5c\u548c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001math\u5e93\u5bf9\u5355\u4e2a\u6570\u503c\u53d6\u5bf9\u6570<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5904\u7406\u5355\u4e2a\u6570\u503c\u6216\u8f83\u5c0f\u7684\u5e8f\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684math\u5e93\u3002math\u5e93\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u6570\u5b66\u51fd\u6570\uff0c\u5305\u62ec\u5bf9\u6570\u51fd\u6570\u3002\u4f7f\u7528math\u5e93\u5bf9\u5e8f\u5217\u53d6\u5bf9\u6570\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u5217\u8868<\/strong><\/h2>\n<p>values = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u53d6\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>log_values = [math.log(x) for x in values]<\/p>\n<p>print(&quot;\u81ea\u7136\u5bf9\u6570:&quot;, log_values)<\/p>\n<h2><strong>\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u53d6\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570<\/strong><\/h2>\n<p>log10_values = [math.log10(x) for x in values]<\/p>\n<p>print(&quot;\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570:&quot;, log10_values)<\/p>\n<h2><strong>\u5bf9\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u53d6\u4ee52\u4e3a\u5e95\u7684\u5bf9\u6570<\/strong><\/h2>\n<p>log2_values = [math.log2(x) for x in values]<\/p>\n<p>print(&quot;\u4ee52\u4e3a\u5e95\u7684\u5bf9\u6570:&quot;, log2_values)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>math\u5e93\u7684\u4f18\u52bf\u5728\u4e8e\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u5904\u7406\u5355\u4e2a\u6570\u503c\u6216\u8f83\u5c0f\u7684\u5e8f\u5217\u3002\u867d\u7136\u6548\u7387\u4e0d\u5982NumPy\u9ad8\uff0c\u4f46\u5728\u5904\u7406\u5c0f\u89c4\u6a21\u6570\u636e\u65f6\u4ecd\u7136\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5bf9\u6570\u53d8\u6362\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5bf9\u6570\u53d8\u6362\u5728\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u5b83\u53ef\u4ee5\u7528\u6765\u5904\u7406\u6570\u636e\u4e2d\u7684\u6781\u503c\uff0c\u5e73\u6ed1\u6570\u636e\u5206\u5e03\uff0c\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u3002\u4f8b\u5982\uff0c\u5728\u5904\u7406\u53f3\u504f\u6570\u636e\u65f6\uff0c\u5bf9\u6570\u636e\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\u53ef\u4ee5\u4f7f\u6570\u636e\u66f4\u52a0\u7b26\u5408\u6b63\u6001\u5206\u5e03\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u62df\u5408\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u8d1f\u6570\u548c\u96f6\u503c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u4f1a\u9047\u5230\u5305\u542b\u8d1f\u6570\u548c\u96f6\u503c\u7684\u5e8f\u5217\u3002\u7531\u4e8e\u5bf9\u6570\u51fd\u6570\u5728\u8d1f\u6570\u548c\u96f6\u503c\u5904\u6ca1\u6709\u5b9a\u4e49\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u6570\u636e\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e0a\u4e00\u4e2a\u5e38\u6570\uff0c\u4f7f\u5f97\u6240\u6709\u5143\u7d20\u90fd\u4e3a\u6b63\u6570\uff0c\u7136\u540e\u518d\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u8d1f\u6570\u548c\u96f6\u503c\u7684\u793a\u4f8b\u6570\u7ec4<\/strong><\/h2>\n<p>arr = np.array([-3, 0, 1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u52a0\u4e0a\u4e00\u4e2a\u5e38\u6570\uff0c\u4f7f\u6240\u6709\u5143\u7d20\u90fd\u4e3a\u6b63\u6570<\/strong><\/h2>\n<p>shifted_arr = arr + 4<\/p>\n<h2><strong>\u5bf9\u5904\u7406\u540e\u7684\u6570\u7ec4\u53d6\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>log_arr = np.log(shifted_arr)<\/p>\n<p>print(&quot;\u81ea\u7136\u5bf9\u6570:&quot;, log_arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\uff0c\u53ef\u4ee5\u907f\u514d\u5bf9\u6570\u51fd\u6570\u5728\u8d1f\u6570\u548c\u96f6\u503c\u5904\u7684\u8ba1\u7b97\u95ee\u9898\uff0c\u4ece\u800c\u4fdd\u8bc1\u8ba1\u7b97\u7ed3\u679c\u7684\u6b63\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5bf9\u6570\u53d8\u6362\u5728\u673a\u5668\u5b66\u4e60\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u5bf9\u6570\u53d8\u6362\u53ef\u4ee5\u7528\u4e8e\u7279\u5f81\u5de5\u7a0b\u548c\u6570\u636e\u9884\u5904\u7406\u3002\u4f8b\u5982\uff0c\u5728\u56de\u5f52\u5206\u6790\u4e2d\uff0c\u5bf9\u6570\u53d8\u6362\u53ef\u4ee5\u7528\u6765\u5904\u7406\u975e\u7ebf\u6027\u5173\u7cfb\uff0c\u4f7f\u5176\u8f6c\u5316\u4e3a\u7ebf\u6027\u5173\u7cfb\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u62df\u5408\u6548\u679c\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<p>from sklearn.linear_model import LinearRegression<\/p>\n<p>from sklearn.model_selection import tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_test_split<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;X&#39;: [1, 2, 3, 4, 5],<\/p>\n<p>                   &#39;y&#39;: [2, 3, 5, 7, 11]})<\/p>\n<h2><strong>\u5bf9\u7279\u5f81X\u8fdb\u884c\u5bf9\u6570\u53d8\u6362<\/strong><\/h2>\n<p>df[&#39;log_X&#39;] = np.log(df[&#39;X&#39;])<\/p>\n<h2><strong>\u5206\u5272\u6570\u636e\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(df[[&#39;log_X&#39;]], df[&#39;y&#39;], test_size=0.2, random_state=42)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u8bad\u7ec3\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<h2><strong>\u9884\u6d4b\u7ed3\u679c<\/strong><\/h2>\n<p>y_pred = model.predict(X_test)<\/p>\n<p>print(&quot;\u9884\u6d4b\u7ed3\u679c:&quot;, y_pred)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u7279\u5f81\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6a21\u578b\u5bf9\u6570\u636e\u7684\u62df\u5408\u6548\u679c\uff0c\u4ece\u800c\u63d0\u9ad8\u9884\u6d4b\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5bf9\u6570\u53d8\u6362\u5728\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u4e2d\uff0c\u5bf9\u6570\u53d8\u6362\u53ef\u4ee5\u7528\u6765\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u4e2d\u7684\u8d8b\u52bf\u548c\u5b63\u8282\u6027\u3002\u4f8b\u5982\uff0c\u5bf9\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\uff0c\u53ef\u4ee5\u51cf\u5f31\u6570\u636e\u4e2d\u7684\u8d8b\u52bf\u548c\u5b63\u8282\u6027\uff0c\u4ece\u800c\u4f7f\u5f97\u65f6\u95f4\u5e8f\u5217\u66f4\u52a0\u5e73\u7a33\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u65f6\u95f4\u5e8f\u5217\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;date&#39;: pd.date_range(start=&#39;1\/1\/2020&#39;, periods=10, freq=&#39;M&#39;),<\/p>\n<p>        &#39;value&#39;: [10, 12, 15, 20, 25, 30, 40, 50, 60, 80]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5bf9\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u8fdb\u884c\u5bf9\u6570\u53d8\u6362<\/strong><\/h2>\n<p>df[&#39;log_value&#39;] = np.log(df[&#39;value&#39;])<\/p>\n<h2><strong>\u7ed8\u5236\u539f\u59cb\u6570\u636e\u548c\u5bf9\u6570\u53d8\u6362\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 5))<\/p>\n<p>plt.plot(df[&#39;date&#39;], df[&#39;value&#39;], label=&#39;\u539f\u59cb\u6570\u636e&#39;)<\/p>\n<p>plt.plot(df[&#39;date&#39;], df[&#39;log_value&#39;], label=&#39;\u5bf9\u6570\u53d8\u6362\u540e\u7684\u6570\u636e&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\uff0c\u53ef\u4ee5\u51cf\u5f31\u6570\u636e\u4e2d\u7684\u8d8b\u52bf\u548c\u5b63\u8282\u6027\uff0c\u4ece\u800c\u4f7f\u5f97\u65f6\u95f4\u5e8f\u5217\u66f4\u52a0\u5e73\u7a33\uff0c\u6709\u52a9\u4e8e\u540e\u7eed\u7684\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u548c\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u5bf9\u6570\u53d8\u6362\u5728\u56fe\u50cf\u5904\u7406\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u5bf9\u6570\u53d8\u6362\u53ef\u4ee5\u7528\u6765\u589e\u5f3a\u56fe\u50cf\u7684\u5bf9\u6bd4\u5ea6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u56fe\u50cf\u7684\u50cf\u7d20\u503c\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\uff0c\u4f7f\u5f97\u56fe\u50cf\u4e2d\u6697\u90e8\u7684\u7ec6\u8282\u66f4\u52a0\u660e\u663e\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cv2<\/p>\n<p>import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u8bfb\u53d6\u793a\u4f8b\u56fe\u50cf<\/strong><\/h2>\n<p>image = cv2.imread(&#39;example.jpg&#39;, cv2.IMREAD_GRAYSCALE)<\/p>\n<h2><strong>\u5bf9\u56fe\u50cf\u7684\u50cf\u7d20\u503c\u8fdb\u884c\u5bf9\u6570\u53d8\u6362<\/strong><\/h2>\n<p>log_image = np.log1p(image)<\/p>\n<h2><strong>\u5f52\u4e00\u5316\u52300-255\u8303\u56f4<\/strong><\/h2>\n<p>log_image = cv2.normalize(log_image, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)<\/p>\n<h2><strong>\u663e\u793a\u539f\u59cb\u56fe\u50cf\u548c\u5bf9\u6570\u53d8\u6362\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 5))<\/p>\n<p>plt.subplot(1, 2, 1)<\/p>\n<p>plt.title(&#39;\u539f\u59cb\u56fe\u50cf&#39;)<\/p>\n<p>plt.imshow(image, cmap=&#39;gray&#39;)<\/p>\n<p>plt.subplot(1, 2, 2)<\/p>\n<p>plt.title(&#39;\u5bf9\u6570\u53d8\u6362\u540e\u7684\u56fe\u50cf&#39;)<\/p>\n<p>plt.imshow(log_image, cmap=&#39;gray&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u56fe\u50cf\u7684\u50cf\u7d20\u503c\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\uff0c\u53ef\u4ee5\u589e\u5f3a\u56fe\u50cf\u7684\u5bf9\u6bd4\u5ea6\uff0c\u4f7f\u5f97\u56fe\u50cf\u4e2d\u6697\u90e8\u7684\u7ec6\u8282\u66f4\u52a0\u660e\u663e\uff0c\u4ece\u800c\u63d0\u9ad8\u56fe\u50cf\u7684\u89c6\u89c9\u6548\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5bf9\u6570\u53d8\u6362\u5728\u91d1\u878d\u6570\u636e\u5206\u6790\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u91d1\u878d\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5bf9\u6570\u53d8\u6362\u53ef\u4ee5\u7528\u6765\u5904\u7406\u91d1\u878d\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u5bf9\u80a1\u7968\u4ef7\u683c\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\uff0c\u4f7f\u5f97\u4ef7\u683c\u53d8\u5316\u66f4\u52a0\u5e73\u7a33\uff0c\u4ece\u800c\u66f4\u597d\u5730\u8fdb\u884c\u5206\u6790\u548c\u9884\u6d4b\u3002\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u80a1\u7968\u4ef7\u683c\u6570\u636e<\/strong><\/h2>\n<p>data = {&#39;date&#39;: pd.date_range(start=&#39;1\/1\/2020&#39;, periods=10, freq=&#39;D&#39;),<\/p>\n<p>        &#39;price&#39;: [100, 102, 105, 110, 120, 125, 130, 140, 150, 160]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5bf9\u80a1\u7968\u4ef7\u683c\u8fdb\u884c\u5bf9\u6570\u53d8\u6362<\/strong><\/h2>\n<p>df[&#39;log_price&#39;] = np.log(df[&#39;price&#39;])<\/p>\n<h2><strong>\u7ed8\u5236\u539f\u59cb\u4ef7\u683c\u548c\u5bf9\u6570\u53d8\u6362\u540e\u7684\u4ef7\u683c<\/strong><\/h2>\n<p>plt.figure(figsize=(10, 5))<\/p>\n<p>plt.plot(df[&#39;date&#39;], df[&#39;price&#39;], label=&#39;\u539f\u59cb\u4ef7\u683c&#39;)<\/p>\n<p>plt.plot(df[&#39;date&#39;], df[&#39;log_price&#39;], label=&#39;\u5bf9\u6570\u53d8\u6362\u540e\u7684\u4ef7\u683c&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u5bf9\u80a1\u7968\u4ef7\u683c\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\uff0c\u53ef\u4ee5\u51cf\u5f31\u4ef7\u683c\u53d8\u5316\u7684\u6ce2\u52a8\uff0c\u4f7f\u5f97\u4ef7\u683c\u53d8\u5316\u66f4\u52a0\u5e73\u7a33\uff0c\u4ece\u800c\u66f4\u597d\u5730\u8fdb\u884c\u5206\u6790\u548c\u9884\u6d4b\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5bf9\u5e8f\u5217\u53d6\u5bf9\u6570\uff0c\u5305\u62ecNumPy\u5e93\u3001Pandas\u5e93\u3001\u4ee5\u53camath\u5e93\u3002NumPy\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u7ec4\uff0cPandas\u9002\u7528\u4e8e\u5904\u7406\u6570\u636e\u6846\uff0cmath\u5e93\u9002\u5408\u5904\u7406\u5355\u4e2a\u6570\u503c\u3002\u5bf9\u6570\u53d8\u6362\u5728\u6570\u636e\u5206\u6790\u3001\u673a\u5668\u5b66\u4e60\u3001\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u3001\u56fe\u50cf\u5904\u7406\u3001\u91d1\u878d\u6570\u636e\u5206\u6790\u7b49\u9886\u57df\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u901a\u8fc7\u5bf9\u6570\u636e\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\uff0c\u53ef\u4ee5\u5e73\u6ed1\u6570\u636e\u5206\u5e03\uff0c\u51cf\u5f31\u6781\u503c\u7684\u5f71\u54cd\uff0c\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u6570\u636e\u7684\u7279\u70b9\u548c\u5206\u6790\u7684\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u5bf9\u6570\u53d8\u6362\u65b9\u6cd5\u548c\u5e93\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u63d0\u9ad8\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u7684\u6548\u7387\u548c\u6548\u679c\u3002\u65e0\u8bba\u662f\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u6570\u636e\u5206\u6790\u8fd8\u662f\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u5bf9\u6570\u53d8\u6362\u90fd\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u548c\u5b9e\u7528\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u5e8f\u5217\u8fdb\u884c\u5bf9\u6570\u8fd0\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5bf9\u5e8f\u5217\u8fdb\u884c\u5bf9\u6570\u8fd0\u7b97\u3002NumPy\u63d0\u4f9b\u4e86<code>numpy.log()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u81ea\u7136\u5bf9\u6570\u8ba1\u7b97\u3002\u5982\u679c\u9700\u8981\u8ba1\u7b97\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.log10()<\/code>\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5NumPy\u5e93\uff0c\u7136\u540e\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u65b9\u5f0f\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u5e8f\u5217\nsequence = np.array([1, 10, 100, 1000])\n\n# \u8ba1\u7b97\u81ea\u7136\u5bf9\u6570\nnatural_log = np.log(sequence)\n\n# \u8ba1\u7b97\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570\nlog10 = np.log10(sequence)\n\nprint(&quot;\u81ea\u7136\u5bf9\u6570:&quot;, natural_log)\nprint(&quot;\u4ee510\u4e3a\u5e95\u7684\u5bf9\u6570:&quot;, log10)\n<\/code><\/pre>\n<p><strong>\u5bf9\u6570\u8fd0\u7b97\u4e2d\u5982\u679c\u9047\u5230\u8d1f\u6570\u6216\u96f6\uff0c\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5bf9\u6570\u8fd0\u7b97\u4e2d\uff0c\u8d1f\u6570\u548c\u96f6\u662f undefined \u7684\uff0c\u56e0\u6b64\u5728\u8fdb\u884c\u5bf9\u6570\u8ba1\u7b97\u4e4b\u524d\uff0c\u5efa\u8bae\u68c0\u67e5\u5e8f\u5217\u4e2d\u7684\u503c\u3002\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u8bed\u53e5\u6765\u8fc7\u6ee4\u6389\u4e0d\u7b26\u5408\u8981\u6c42\u7684\u503c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 NumPy \u7684\u5e03\u5c14\u7d22\u5f15\u6765\u4ec5\u9009\u62e9\u6b63\u6570\u8fdb\u884c\u5bf9\u6570\u8fd0\u7b97\uff1a<\/p>\n<pre><code class=\"language-python\">positive_sequence = sequence[sequence &gt; 0]\nlog_values = np.log(positive_sequence)\n<\/code><\/pre>\n<p><strong>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u4f7f\u7528\u5bf9\u6570\u53d8\u6362\u7684\u76ee\u7684\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5bf9\u6570\u53d8\u6362\u5728\u6570\u636e\u5206\u6790\u4e2d\u5e38\u7528\u6765\u5904\u7406\u6570\u636e\u7684\u504f\u6001\u5206\u5e03\uff0c\u51cf\u5c11\u5f02\u5e38\u503c\u7684\u5f71\u54cd\uff0c\u5e76\u4f7f\u6570\u636e\u66f4\u7b26\u5408\u6b63\u6001\u5206\u5e03\u7684\u5047\u8bbe\u3002\u8fd9\u5bf9\u4e8e\u8bb8\u591a\u7edf\u8ba1\u6a21\u578b\u548c\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u90fd\u975e\u5e38\u91cd\u8981\uff0c\u80fd\u591f\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u548c\u51c6\u786e\u6027\u3002\u6b64\u5916\uff0c\u5bf9\u6570\u53d8\u6362\u8fd8\u53ef\u4ee5\u5e2e\u52a9\u66f4\u597d\u5730\u7406\u89e3\u6570\u636e\u7684\u76f8\u5bf9\u53d8\u5316\uff0c\u6bd4\u5982\u767e\u5206\u6bd4\u53d8\u5316\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u8fdb\u884c\u5bf9\u6570\u8fd0\u7b97\uff1f<\/strong><br \/>\u9664\u4e86NumPy\uff0cPython\u7684\u5176\u4ed6\u5e93\u5982Pandas\u548cSciPy\u4e5f\u53ef\u4ee5\u8fdb\u884c\u5bf9\u6570\u8fd0\u7b97\u3002\u7279\u522b\u662f\u5728\u5904\u7406DataFrame\u65f6\uff0cPandas\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u5bf9\u6570\u636e\u5217\u8fdb\u884c\u5bf9\u6570\u53d8\u6362\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>DataFrame.apply()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5bf9\u5217\u8fdb\u884c\u5bf9\u6570\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u521b\u5efa\u4e00\u4e2aDataFrame\ndf = pd.DataFrame({&#39;values&#39;: [1, 10, 100, 1000]})\n\n# \u8ba1\u7b97\u5bf9\u6570\ndf[&#39;log_values&#39;] = df[&#39;values&#39;].apply(np.log)\nprint(df)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Python\u5bf9\u5e8f\u5217\u53d6\u5bf9\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001Pandas\u5e93\u3001\u4ee5\u53camath\u5e93\u3002NumP [&hellip;]","protected":false},"author":3,"featured_media":1188049,"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\/1188045"}],"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=1188045"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188045\/revisions"}],"predecessor-version":[{"id":1188051,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1188045\/revisions\/1188051"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1188049"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1188045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1188045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1188045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}