{"id":1101942,"date":"2025-01-08T15:54:57","date_gmt":"2025-01-08T07:54:57","guid":{"rendered":""},"modified":"2025-01-08T15:55:03","modified_gmt":"2025-01-08T07:55:03","slug":"python%e5%a6%82%e4%bd%95%e6%b1%82%e4%b8%a4%e4%b8%aa%e7%9f%a9%e9%98%b5%e5%85%83%e7%b4%a0%e7%9b%b8%e9%99%a4-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1101942.html","title":{"rendered":"python\u5982\u4f55\u6c42\u4e24\u4e2a\u77e9\u9635\u5143\u7d20\u76f8\u9664"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064458\/23925e30-f074-4a6a-84a1-be2fcd17af42.webp\" alt=\"python\u5982\u4f55\u6c42\u4e24\u4e2a\u77e9\u9635\u5143\u7d20\u76f8\u9664\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u6c42\u4e24\u4e2a\u77e9\u9635\u5143\u7d20\u76f8\u9664\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Numpy\u5e93\u3001\u901a\u8fc7\u5217\u8868\u89e3\u6790\u4ee5\u53ca\u624b\u52a8\u5b9e\u73b0\u7b49\u591a\u79cd\u65b9\u5f0f\u3002<\/strong> \u5176\u4e2d\uff0c\u6700\u5e38\u7528\u548c\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Numpy\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u548c\u5e7f\u64ad\u673a\u5236\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Numpy\u5e93<\/h3>\n<\/p>\n<p><p>Numpy\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u3002\u4f7f\u7528Numpy\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u9635\u64cd\u4f5c\uff0c\u5305\u62ec\u5143\u7d20\u7ea7\u522b\u7684\u9664\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>1. \u5b89\u88c5Numpy\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Numpy\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528Numpy\u8fdb\u884c\u77e9\u9635\u5143\u7d20\u76f8\u9664<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix_a = np.array([[4, 9], [16, 25]])<\/p>\n<p>matrix_b = np.array([[2, 3], [4, 5]])<\/p>\n<h2><strong>\u5143\u7d20\u76f8\u9664<\/strong><\/h2>\n<p>result = np.divide(matrix_a, matrix_b)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>np.divide<\/code>\u51fd\u6570\u7528\u4e8e\u5bf9\u4e24\u4e2a\u77e9\u9635\u7684\u6bcf\u4e2a\u5bf9\u5e94\u5143\u7d20\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u3002Numpy\u4f1a\u81ea\u52a8\u5904\u7406\u77e9\u9635\u5f62\u72b6\u7684\u5339\u914d\u548c\u5e7f\u64ad\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u89e3\u6790<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u4e0d\u60f3\u4f7f\u7528\u5916\u90e8\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u89e3\u6790\u6765\u5b9e\u73b0\u77e9\u9635\u5143\u7d20\u7ea7\u522b\u7684\u9664\u6cd5\u3002\u867d\u7136\u8fd9\u79cd\u65b9\u6cd5\u7684\u6548\u7387\u4e0d\u5982Numpy\u9ad8\uff0c\u4f46\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u77e9\u9635\u7684\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e24\u4e2a\u77e9\u9635<\/p>\n<p>matrix_a = [[4, 9], [16, 25]]<\/p>\n<p>matrix_b = [[2, 3], [4, 5]]<\/p>\n<h2><strong>\u5143\u7d20\u76f8\u9664<\/strong><\/h2>\n<p>result = [[matrix_a[i][j] \/ matrix_b[i][j] for j in range(len(matrix_a[0]))] for i in range(len(matrix_a))]<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u4e86\u5d4c\u5957\u7684\u5217\u8868\u89e3\u6790\u6765\u904d\u5386\u4e24\u4e2a\u77e9\u9635\uff0c\u5e76\u5bf9\u6bcf\u4e2a\u5bf9\u5e94\u5143\u7d20\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u624b\u52a8\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u53ef\u4ee5\u624b\u52a8\u5b9e\u73b0\u77e9\u9635\u7684\u5143\u7d20\u7ea7\u522b\u9664\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u5408\u9700\u8981\u5bf9\u9664\u6cd5\u8fc7\u7a0b\u8fdb\u884c\u66f4\u591a\u63a7\u5236\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e24\u4e2a\u77e9\u9635<\/p>\n<p>matrix_a = [[4, 9], [16, 25]]<\/p>\n<p>matrix_b = [[2, 3], [4, 5]]<\/p>\n<h2><strong>\u5143\u7d20\u76f8\u9664<\/strong><\/h2>\n<p>result = []<\/p>\n<p>for i in range(len(matrix_a)):<\/p>\n<p>    row = []<\/p>\n<p>    for j in range(len(matrix_a[0])):<\/p>\n<p>        row.append(matrix_a[i][j] \/ matrix_b[i][j])<\/p>\n<p>    result.append(row)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u5d4c\u5957\u7684\u5faa\u73af\u904d\u5386\u77e9\u9635\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u3002\u6700\u7ec8\u5c06\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2a\u65b0\u7684\u77e9\u9635\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u7279\u6b8a\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u77e9\u9635\u5143\u7d20\u9664\u6cd5\u65f6\uff0c\u8fd8\u9700\u8981\u8003\u8651\u4e00\u4e9b\u7279\u6b8a\u60c5\u51b5\uff0c\u4f8b\u5982\u77e9\u9635\u5f62\u72b6\u4e0d\u5339\u914d\u3001\u9664\u6570\u4e3a\u96f6\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. \u77e9\u9635\u5f62\u72b6\u4e0d\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4e24\u4e2a\u77e9\u9635\u7684\u5f62\u72b6\u4e0d\u5339\u914d\uff0c\u8fdb\u884c\u5143\u7d20\u7ea7\u522b\u9664\u6cd5\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u53ef\u4ee5\u5728\u64cd\u4f5c\u524d\u68c0\u67e5\u77e9\u9635\u7684\u5f62\u72b6\u662f\u5426\u4e00\u81f4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_divide(matrix_a, matrix_b):<\/p>\n<p>    if matrix_a.shape != matrix_b.shape:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u77e9\u9635\u5f62\u72b6\u4e0d\u5339\u914d&quot;)<\/p>\n<p>    return np.divide(matrix_a, matrix_b)<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix_a = np.array([[4, 9], [16, 25]])<\/p>\n<p>matrix_b = np.array([[2, 3], [4, 5]])<\/p>\n<p>result = safe_divide(matrix_a, matrix_b)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u9664\u6570\u4e3a\u96f6<\/h4>\n<\/p>\n<p><p>\u5728\u9664\u6cd5\u8fd0\u7b97\u4e2d\uff0c\u5982\u679c\u9664\u6570\u4e3a\u96f6\u4f1a\u5bfc\u81f4\u9664\u96f6\u9519\u8bef\u3002\u53ef\u4ee5\u5728\u8fdb\u884c\u9664\u6cd5\u524d\u68c0\u67e5\u9664\u6570\u77e9\u9635\uff0c\u5e76\u66ff\u6362\u4e3a\u4e00\u4e2a\u975e\u5e38\u5c0f\u7684\u6570\u503c\u6765\u907f\u514d\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def divide_with_zero_check(matrix_a, matrix_b):<\/p>\n<p>    matrix_b = np.where(matrix_b == 0, 1e-10, matrix_b)<\/p>\n<p>    return np.divide(matrix_a, matrix_b)<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix_a = np.array([[4, 9], [16, 25]])<\/p>\n<p>matrix_b = np.array([[2, 0], [4, 5]])<\/p>\n<p>result = divide_with_zero_check(matrix_a, matrix_b)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>np.where<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u9664\u6570\u77e9\u9635\u4e2d\u7684\u96f6\u503c\u66ff\u6362\u4e3a\u4e00\u4e2a\u975e\u5e38\u5c0f\u7684\u6570\u503c<code>1e-10<\/code>\uff0c\u4ee5\u907f\u514d\u9664\u96f6\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u4f7f\u7528Numpy\u5e93\u8fdb\u884c\u77e9\u9635\u5143\u7d20\u76f8\u9664\u662f\u6700\u63a8\u8350\u7684\u65b9\u6cd5<\/strong>\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7b80\u6d01\u3001\u9ad8\u6548\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u573a\u666f\u3002\u5217\u8868\u89e3\u6790\u548c\u624b\u52a8\u5b9e\u73b0\u9002\u7528\u4e8e\u5c0f\u89c4\u6a21\u77e9\u9635\u64cd\u4f5c\u6216\u4e0d\u4f9d\u8d56\u5916\u90e8\u5e93\u7684\u60c5\u51b5\u3002\u5728\u8fdb\u884c\u77e9\u9635\u5143\u7d20\u9664\u6cd5\u65f6\uff0c\u8fd8\u9700\u8981\u5904\u7406\u77e9\u9635\u5f62\u72b6\u4e0d\u5339\u914d\u548c\u9664\u6570\u4e3a\u96f6\u7b49\u7279\u6b8a\u60c5\u51b5\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u9c81\u68d2\u6027\u3002\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u77e9\u9635\u5143\u7d20\u7ea7\u522b\u7684\u9664\u6cd5\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>1. \u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u5143\u7d20\u76f8\u9664\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u9635\u64cd\u4f5c\u3002\u8981\u5b9e\u73b0\u4e24\u4e2a\u77e9\u9635\u5143\u7d20\u9010\u4e00\u76f8\u9664\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy<\/code>\u7684\u6570\u7ec4\u529f\u80fd\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u7136\u540e\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635\nmatrix_a = np.array([[1, 2, 3], [4, 5, 6]])\nmatrix_b = np.array([[1, 1, 1], [2, 2, 2]])\n\n# \u8fdb\u884c\u5143\u7d20\u9010\u4e00\u76f8\u9664\nresult = matrix_a \/ matrix_b\nprint(result)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u77e9\u9635\uff0c\u91cc\u9762\u7684\u5143\u7d20\u662f\u5bf9\u5e94\u4f4d\u7f6e\u7684\u5143\u7d20\u76f8\u9664\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>2. \u5982\u679c\u77e9\u9635\u4e2d\u5305\u542b\u96f6\uff0c\u5982\u4f55\u5904\u7406\u9664\u96f6\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u77e9\u9635\u5143\u7d20\u76f8\u9664\u65f6\uff0c\u5982\u679c\u5206\u6bcd\u77e9\u9635\u4e2d\u5305\u542b\u96f6\uff0c\u4f1a\u5f15\u53d1\u9664\u96f6\u9519\u8bef\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>np.errstate()<\/code>\u51fd\u6570\u6765\u5ffd\u7565\u8b66\u544a\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635\uff0c\u5305\u542b\u96f6\nmatrix_a = np.array([[1, 2, 3], [4, 5, 6]])\nmatrix_b = np.array([[1, 0, 1], [2, 2, 0]])\n\n# \u4f7f\u7528np.errstate\u6765\u5904\u7406\u9664\u96f6\nwith np.errstate(divide=&#39;ignore&#39;, invalid=&#39;ignore&#39;):\n    result = matrix_a \/ matrix_b\n    result = np.nan_to_num(result)  # \u5c06nan\u66ff\u6362\u4e3a0\nprint(result)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u4ee3\u7801\u5728\u8fd0\u884c\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u5e76\u4e14\u53ef\u4ee5\u5904\u7406\u9664\u96f6\u60c5\u51b5\u3002<\/p>\n<p><strong>3. \u6709\u6ca1\u6709\u529e\u6cd5\u4f7f\u7528\u5176\u4ed6\u5e93\u8fdb\u884c\u77e9\u9635\u7684\u5143\u7d20\u76f8\u9664\uff1f<\/strong><br \/>\u9664\u4e86NumPy\uff0cPython\u4e2d\u8fd8\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u5904\u7406\u77e9\u9635\u8fd0\u7b97\uff0c\u6bd4\u5982Pandas\u548cTensorFlow\u3002\u4f7f\u7528Pandas\u5e93\u8fdb\u884c\u5143\u7d20\u76f8\u9664\u7684\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\n# \u5b9a\u4e49\u4e24\u4e2aDataFrame\ndf_a = pd.DataFrame([[1, 2, 3], [4, 5, 6]])\ndf_b = pd.DataFrame([[1, 1, 1], [2, 2, 2]])\n\n# \u8fdb\u884c\u5143\u7d20\u9010\u4e00\u76f8\u9664\nresult = df_a \/ df_b\nprint(result)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u540c\u6837\u652f\u6301\u5143\u7d20\u9010\u4e00\u76f8\u9664\uff0c\u5e76\u4e14\u5728\u5904\u7406\u6570\u636e\u65f6\uff0cPandas\u63d0\u4f9b\u4e86\u66f4\u5f3a\u5927\u7684\u6570\u636e\u7ba1\u7406\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u6c42\u4e24\u4e2a\u77e9\u9635\u5143\u7d20\u76f8\u9664\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528Numpy\u5e93\u3001\u901a\u8fc7\u5217\u8868\u89e3\u6790\u4ee5\u53ca\u624b\u52a8\u5b9e\u73b0\u7b49\u591a\u79cd\u65b9\u5f0f\u3002 \u5176\u4e2d\uff0c\u6700\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1101961,"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\/1101942"}],"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=1101942"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101942\/revisions"}],"predecessor-version":[{"id":1101963,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1101942\/revisions\/1101963"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1101961"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1101942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1101942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1101942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}