{"id":1170477,"date":"2025-01-15T16:23:04","date_gmt":"2025-01-15T08:23:04","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170477.html"},"modified":"2025-01-15T16:23:07","modified_gmt":"2025-01-15T08:23:07","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e7%9f%a9%e9%98%b5%e7%9b%b8%e4%b9%98","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1170477.html","title":{"rendered":"python\u5982\u4f55\u5199\u77e9\u9635\u76f8\u4e58"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26073106\/730890c6-a24e-4111-a675-6dccb8e8a386.webp\" alt=\"python\u5982\u4f55\u5199\u77e9\u9635\u76f8\u4e58\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u77e9\u9635\u76f8\u4e58\u7684\u6838\u5fc3\u5728\u4e8e<strong>\u77e9\u9635\u5143\u7d20\u76f8\u4e58\u5e76\u7d2f\u52a0\u3001\u4f7f\u7528NumPy\u5e93\u5b9e\u73b0\u3001\u8003\u8651\u77e9\u9635\u7ef4\u5ea6\u4e00\u81f4\u6027<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528NumPy\u5e93\u5b9e\u73b0<\/strong>\u662f\u6700\u4e3a\u91cd\u8981\u7684\u4e00\u70b9\u3002NumPy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5f3a\u5927\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u591a\u79cd\u7528\u4e8e\u64cd\u4f5c\u6570\u7ec4\u548c\u77e9\u9635\u7684\u5de5\u5177\uff0c\u4f7f\u5f97\u77e9\u9635\u76f8\u4e58\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u4e14\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528NumPy\u5e93\u5b9e\u73b0\uff1a<\/strong><\/p>\n<p>NumPy\u5e93\u5185\u7f6e\u4e86\u5f3a\u5927\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u5176<code>dot<\/code>\u51fd\u6570\u548c<code>matmul<\/code>\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u77e9\u9635\u76f8\u4e58\u3002<code>dot<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u4e00\u7ef4\u6570\u7ec4\u7684\u70b9\u79ef\u8fd0\u7b97\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u4e8c\u7ef4\u6570\u7ec4\uff08\u5373\u77e9\u9635\uff09\u7684\u77e9\u9635\u4e58\u6cd5\u3002\u800c<code>matmul<\/code>\u51fd\u6570\u4e13\u95e8\u7528\u4e8e\u77e9\u9635\u76f8\u4e58\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>A = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p>B = np.array([[7, 8], [9, 10], [11, 12]])<\/p>\n<h2><strong>\u4f7f\u7528dot\u51fd\u6570\u8fdb\u884c\u77e9\u9635\u76f8\u4e58<\/strong><\/h2>\n<p>C = np.dot(A, B)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c\u77e9\u9635<\/strong><\/h2>\n<p>print(C)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u9996\u5148\u5bfc\u5165\u4e86NumPy\u5e93\uff0c\u5b9a\u4e49\u4e86\u4e24\u4e2a\u77e9\u9635A\u548cB\uff0c\u7136\u540e\u4f7f\u7528<code>np.dot<\/code>\u51fd\u6570\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\uff0c\u6700\u540e\u8f93\u51fa\u7ed3\u679c\u77e9\u9635C\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u77e9\u9635\u76f8\u4e58\u7684\u5404\u4e2a\u65b9\u9762\uff0c\u5305\u62ec\u77e9\u9635\u76f8\u4e58\u7684\u57fa\u672c\u539f\u7406\u3001\u624b\u52a8\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58\u3001NumPy\u7684\u4f18\u52bf\u3001\u4ee5\u53ca\u4e00\u4e9b\u8fdb\u9636\u7684\u77e9\u9635\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u77e9\u9635\u76f8\u4e58\u7684\u57fa\u672c\u539f\u7406<\/h2>\n<\/p>\n<p><p>\u77e9\u9635\u76f8\u4e58\uff08\u4e5f\u79f0\u4e3a\u77e9\u9635\u4e58\u6cd5\uff09\u662f\u7ebf\u6027\u4ee3\u6570\u4e2d\u7684\u4e00\u79cd\u57fa\u672c\u8fd0\u7b97\uff0c\u5b83\u6d89\u53ca\u4e24\u4e2a\u77e9\u9635\u7684\u4e58\u79ef\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e24\u4e2a\u77e9\u9635A\u548cB\uff0c\u5176\u4e2dA\u662fm\u00d7n\u77e9\u9635\uff0cB\u662fn\u00d7p\u77e9\u9635\uff0c\u90a3\u4e48\u5b83\u4eec\u7684\u4e58\u79efC\u662f\u4e00\u4e2am\u00d7p\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p>\u77e9\u9635C\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20<code>c[i][j]<\/code>\u662f\u77e9\u9635A\u7684\u7b2ci\u884c\u4e0e\u77e9\u9635B\u7684\u7b2cj\u5217\u7684\u5143\u7d20\u7684\u4e58\u79ef\u4e4b\u548c\u3002\u5177\u4f53\u5730\uff0c<code>c[i][j]<\/code>\u7684\u8ba1\u7b97\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><p>[ c[i][j] = \\sum_{k=1}^{n} a[i][k] \\cdot b[k][j] ]<\/p>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>a[i][k]<\/code>\u662f\u77e9\u9635A\u7684\u7b2ci\u884c\u7b2ck\u5217\u7684\u5143\u7d20\uff0c<code>b[k][j]<\/code>\u662f\u77e9\u9635B\u7684\u7b2ck\u884c\u7b2cj\u5217\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><p>\u4e3e\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff0c\u5047\u8bbe\u6709\u5982\u4e0b\u4e24\u4e2a\u77e9\u9635A\u548cB\uff1a<\/p>\n<\/p>\n<p><p>[ A = \\begin{bmatrix} 1 &amp; 2 &amp; 3 \\ 4 &amp; 5 &amp; 6 \\end{bmatrix} ]<\/p>\n<\/p>\n<p><p>[ B = \\begin{bmatrix} 7 &amp; 8 \\ 9 &amp; 10 \\ 11 &amp; 12 \\end{bmatrix} ]<\/p>\n<\/p>\n<p><p>\u5b83\u4eec\u7684\u4e58\u79efC\u662f\uff1a<\/p>\n<\/p>\n<p><p>[ C = \\begin{bmatrix} (1 \\cdot 7 + 2 \\cdot 9 + 3 \\cdot 11) &amp; (1 \\cdot 8 + 2 \\cdot 10 + 3 \\cdot 12) \\ (4 \\cdot 7 + 5 \\cdot 9 + 6 \\cdot 11) &amp; (4 \\cdot 8 + 5 \\cdot 10 + 6 \\cdot 12) \\end{bmatrix} ]<\/p>\n<\/p>\n<p><p>\u8ba1\u7b97\u7ed3\u679c\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ C = \\begin{bmatrix} 58 &amp; 64 \\ 139 &amp; 154 \\end{bmatrix} ]<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u624b\u52a8\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58<\/h2>\n<\/p>\n<p><p>\u5728\u4e86\u89e3\u4e86\u77e9\u9635\u76f8\u4e58\u7684\u57fa\u672c\u539f\u7406\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c1d\u8bd5\u624b\u52a8\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58\u3002\u5c3d\u7ba1NumPy\u5e93\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u51fd\u6570\uff0c\u6211\u4eec\u4ecd\u7136\u6709\u5fc5\u8981\u4e86\u89e3\u5176\u80cc\u540e\u7684\u5b9e\u73b0\u539f\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528Python\u4ee3\u7801\u624b\u52a8\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def matrix_multiply(A, B):<\/p>\n<p>    # \u83b7\u53d6\u77e9\u9635\u7684\u7ef4\u5ea6<\/p>\n<p>    rows_A, cols_A = len(A), len(A[0])<\/p>\n<p>    rows_B, cols_B = len(B), len(B[0])<\/p>\n<p>    # \u68c0\u67e5\u77e9\u9635A\u7684\u5217\u6570\u662f\u5426\u7b49\u4e8e\u77e9\u9635B\u7684\u884c\u6570<\/p>\n<p>    if cols_A != rows_B:<\/p>\n<p>        r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se ValueError(&quot;\u65e0\u6cd5\u76f8\u4e58\uff1a\u77e9\u9635A\u7684\u5217\u6570\u5e94\u7b49\u4e8e\u77e9\u9635B\u7684\u884c\u6570&quot;)<\/p>\n<p>    # \u521d\u59cb\u5316\u7ed3\u679c\u77e9\u9635C\uff0c\u6240\u6709\u5143\u7d20\u521d\u59cb\u4e3a0<\/p>\n<p>    C = [[0 for _ in range(cols_B)] for _ in range(rows_A)]<\/p>\n<p>    # \u8fdb\u884c\u77e9\u9635\u76f8\u4e58<\/p>\n<p>    for i in range(rows_A):<\/p>\n<p>        for j in range(cols_B):<\/p>\n<p>            for k in range(cols_A):<\/p>\n<p>                C[i][j] += A[i][k] * B[k][j]<\/p>\n<p>    return C<\/p>\n<h2><strong>\u5b9a\u4e49\u4e24\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>A = [[1, 2, 3], [4, 5, 6]]<\/p>\n<p>B = [[7, 8], [9, 10], [11, 12]]<\/p>\n<h2><strong>\u8fdb\u884c\u77e9\u9635\u76f8\u4e58<\/strong><\/h2>\n<p>C = matrix_multiply(A, B)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c\u77e9\u9635<\/strong><\/h2>\n<p>print(C)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>matrix_multiply<\/code>\u7528\u4e8e\u77e9\u9635\u76f8\u4e58\u3002\u8be5\u51fd\u6570\u9996\u5148\u68c0\u67e5\u77e9\u9635A\u7684\u5217\u6570\u662f\u5426\u7b49\u4e8e\u77e9\u9635B\u7684\u884c\u6570\uff0c\u5982\u679c\u4e0d\u76f8\u7b49\u5219\u629b\u51fa\u4e00\u4e2a\u9519\u8bef\u3002\u7136\u540e\uff0c\u5b83\u521d\u59cb\u5316\u7ed3\u679c\u77e9\u9635C\uff0c\u6240\u6709\u5143\u7d20\u521d\u59cb\u4e3a0\u3002\u63a5\u4e0b\u6765\uff0c\u5b83\u901a\u8fc7\u4e09\u91cd\u5faa\u73af\u904d\u5386\u77e9\u9635A\u548c\u77e9\u9635B\u7684\u5143\u7d20\uff0c\u8fdb\u884c\u76f8\u4e58\u5e76\u7d2f\u52a0\uff0c\u6700\u7ec8\u8fd4\u56de\u7ed3\u679c\u77e9\u9635C\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001NumPy\u7684\u4f18\u52bf<\/h2>\n<\/p>\n<p><p>\u867d\u7136\u624b\u52a8\u5b9e\u73b0\u77e9\u9635\u76f8\u4e58\u6709\u52a9\u4e8e\u6211\u4eec\u7406\u89e3\u77e9\u9635\u4e58\u6cd5\u7684\u57fa\u672c\u539f\u7406\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528NumPy\u5e93\u6765\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\u3002NumPy\u5e93\u63d0\u4f9b\u4e86\u8bb8\u591a\u4f18\u52bf\uff0c\u4f7f\u5f97\u77e9\u9635\u76f8\u4e58\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u548c\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h3>1. \u9ad8\u6548\u6027<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u662f\u7528C\u8bed\u8a00\u7f16\u5199\u7684\uff0c\u5e95\u5c42\u8fdb\u884c\u4e86\u8bb8\u591a\u4f18\u5316\uff0c\u4f7f\u5f97\u77e9\u9635\u76f8\u4e58\u7684\u6548\u7387\u975e\u5e38\u9ad8\u3002\u76f8\u6bd4\u4e8e\u7eafPython\u5b9e\u73b0\uff0cNumPy\u7684\u77e9\u9635\u76f8\u4e58\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u8ba1\u7b97\u65f6\u95f4\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u77e9\u9635\u65f6\u3002<\/p>\n<\/p>\n<p><h3>2. \u6613\u7528\u6027<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u7528\u4e8e\u77e9\u9635\u64cd\u4f5c\u7684\u51fd\u6570\uff0c\u4f8b\u5982<code>dot<\/code>\u51fd\u6570\u548c<code>matmul<\/code>\u51fd\u6570\uff0c\u4f7f\u5f97\u77e9\u9635\u76f8\u4e58\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u548c\u76f4\u89c2\u3002\u53ea\u9700\u4e00\u884c\u4ee3\u7801\u5373\u53ef\u5b8c\u6210\u77e9\u9635\u76f8\u4e58\u7684\u64cd\u4f5c\uff0c\u8fd9\u6781\u5927\u5730\u7b80\u5316\u4e86\u4ee3\u7801\u7f16\u5199\u548c\u7ef4\u62a4\u7684\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>3. \u5e7f\u6cdb\u7684\u529f\u80fd<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u77e9\u9635\u76f8\u4e58\uff0cNumPy\u5e93\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u4f8b\u5982\u77e9\u9635\u8f6c\u7f6e\u3001\u77e9\u9635\u6c42\u9006\u3001\u77e9\u9635\u5206\u89e3\u7b49\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\u3002\u6b64\u5916\uff0cNumPy\u8fd8\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\u548c\u9ad8\u7ef4\u6570\u7ec4\uff0c\u4f7f\u5f97\u5b83\u5728\u6570\u636e\u5206\u6790\u548c\u79d1\u5b66\u8ba1\u7b97\u4e2d\u5f97\u5230\u4e86\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>4. \u8de8\u5e73\u53f0\u652f\u6301<\/h3>\n<\/p>\n<p><p>NumPy\u5e93\u662f\u8de8\u5e73\u53f0\u7684\uff0c\u53ef\u4ee5\u5728Windows\u3001Linux\u3001macOS\u7b49\u591a\u4e2a\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u8fd0\u884c\uff0c\u8fd9\u4f7f\u5f97\u5b83\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u7684\u5e94\u7528\u53d8\u5f97\u975e\u5e38\u65b9\u4fbf\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u8fdb\u9636\u7684\u77e9\u9635\u64cd\u4f5c<\/h2>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u77e9\u9635\u76f8\u4e58\uff0cNumPy\u5e93\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u8fdb\u9636\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u590d\u6742\u7684\u77e9\u9635\u8fd0\u7b97\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u8fdb\u9636\u77e9\u9635\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><h3>1. \u77e9\u9635\u8f6c\u7f6e<\/h3>\n<\/p>\n<p><p>\u77e9\u9635\u8f6c\u7f6e\u662f\u5c06\u77e9\u9635\u7684\u884c\u548c\u5217\u4e92\u6362\uff0c\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u77e9\u9635\u3002NumPy\u5e93\u63d0\u4f9b\u4e86<code>transpose<\/code>\u51fd\u6570\u7528\u4e8e\u77e9\u9635\u8f6c\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>A = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u8fdb\u884c\u77e9\u9635\u8f6c\u7f6e<\/strong><\/h2>\n<p>A_T = np.transpose(A)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c\u77e9\u9635<\/strong><\/h2>\n<p>print(A_T)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u77e9\u9635\u6c42\u9006<\/h3>\n<\/p>\n<p><p>\u77e9\u9635\u6c42\u9006\u662f\u6c42\u4e00\u4e2a\u77e9\u9635\u7684\u9006\u77e9\u9635\uff0c\u4f7f\u5f97\u8be5\u77e9\u9635\u4e0e\u5176\u9006\u77e9\u9635\u76f8\u4e58\u5f97\u5230\u5355\u4f4d\u77e9\u9635\u3002NumPy\u5e93\u63d0\u4f9b\u4e86<code>linalg.inv<\/code>\u51fd\u6570\u7528\u4e8e\u77e9\u9635\u6c42\u9006\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u65b9\u9635<\/strong><\/h2>\n<p>A = np.array([[1, 2], [3, 4]])<\/p>\n<h2><strong>\u8fdb\u884c\u77e9\u9635\u6c42\u9006<\/strong><\/h2>\n<p>A_inv = np.linalg.inv(A)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c\u77e9\u9635<\/strong><\/h2>\n<p>print(A_inv)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3. \u77e9\u9635\u5206\u89e3<\/h3>\n<\/p>\n<p><p>\u77e9\u9635\u5206\u89e3\u662f\u5c06\u4e00\u4e2a\u77e9\u9635\u5206\u89e3\u6210\u591a\u4e2a\u77e9\u9635\u7684\u4e58\u79ef\uff0c\u5e38\u89c1\u7684\u77e9\u9635\u5206\u89e3\u65b9\u6cd5\u5305\u62ecLU\u5206\u89e3\u3001QR\u5206\u89e3\u548c\u5947\u5f02\u503c\u5206\u89e3\uff08SVD\uff09\u3002NumPy\u5e93\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u51fd\u6570\u7528\u4e8e\u77e9\u9635\u5206\u89e3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>A = np.array([[1, 2], [3, 4]])<\/p>\n<h2><strong>\u8fdb\u884cLU\u5206\u89e3<\/strong><\/h2>\n<p>P, L, U = np.linalg.lu(A)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c\u77e9\u9635<\/strong><\/h2>\n<p>print(&quot;P:&quot;, P)<\/p>\n<p>print(&quot;L:&quot;, L)<\/p>\n<p>print(&quot;U:&quot;, U)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4. \u77e9\u9635\u884c\u5217\u5f0f<\/h3>\n<\/p>\n<p><p>\u77e9\u9635\u884c\u5217\u5f0f\u662f\u4e00\u4e2a\u6807\u91cf\uff0c\u7528\u4e8e\u63cf\u8ff0\u77e9\u9635\u7684\u67d0\u4e9b\u6027\u8d28\uff0c\u5982\u53ef\u9006\u6027\u3002NumPy\u5e93\u63d0\u4f9b\u4e86<code>linalg.det<\/code>\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u77e9\u9635\u884c\u5217\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u65b9\u9635<\/strong><\/h2>\n<p>A = np.array([[1, 2], [3, 4]])<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u884c\u5217\u5f0f<\/strong><\/h2>\n<p>det_A = np.linalg.det(A)<\/p>\n<h2><strong>\u8f93\u51fa\u7ed3\u679c<\/strong><\/h2>\n<p>print(det_A)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u8fdb\u9636\u7684\u77e9\u9635\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u590d\u6742\u7684\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\uff0c\u4ece\u800c\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u77e9\u9635\u76f8\u4e58\u7684\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><p>\u77e9\u9635\u76f8\u4e58\u5728\u8bb8\u591a\u9886\u57df\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h3>1. \u8ba1\u7b97\u673a\u56fe\u5f62\u5b66<\/h3>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66\u4e2d\uff0c\u77e9\u9635\u76f8\u4e58\u7528\u4e8e\u63cf\u8ff0\u548c\u5b9e\u73b0\u51e0\u4f55\u53d8\u6362\uff0c\u4f8b\u5982\u5e73\u79fb\u3001\u65cb\u8f6c\u548c\u7f29\u653e\u3002\u901a\u8fc7\u5c06\u70b9\u6216\u5411\u91cf\u4e0e\u53d8\u6362\u77e9\u9635\u76f8\u4e58\uff0c\u53ef\u4ee5\u5b9e\u73b0\u51e0\u4f55\u56fe\u5f62\u7684\u5404\u79cd\u53d8\u6362\u3002<\/p>\n<\/p>\n<p><h3>2. <a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/h3>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u77e9\u9635\u76f8\u4e58\u7528\u4e8e\u63cf\u8ff0\u548c\u5b9e\u73b0\u7ebf\u6027\u53d8\u6362\u3002\u4f8b\u5982\uff0c\u5728\u7ebf\u6027\u56de\u5f52\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u77e9\u9635\u4e0e\u6743\u91cd\u77e9\u9635\u76f8\u4e58\u5f97\u5230\u9884\u6d4b\u503c\u3002\u5728\u795e\u7ecf\u7f51\u7edc\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u77e9\u9635\u4e0e\u6743\u91cd\u77e9\u9635\u76f8\u4e58\u5f97\u5230\u6fc0\u6d3b\u503c\u3002<\/p>\n<\/p>\n<p><h3>3. \u6570\u636e\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u77e9\u9635\u76f8\u4e58\u7528\u4e8e\u63cf\u8ff0\u548c\u5b9e\u73b0\u6570\u636e\u7684\u7ebf\u6027\u53d8\u6362\u548c\u964d\u7ef4\u3002\u4f8b\u5982\uff0c\u5728\u4e3b\u6210\u5206\u5206\u6790\uff08PCA\uff09\u4e2d\uff0c\u8f93\u5165\u6570\u636e\u77e9\u9635\u4e0e\u7279\u5f81\u5411\u91cf\u77e9\u9635\u76f8\u4e58\u5f97\u5230\u964d\u7ef4\u540e\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>4. \u7269\u7406\u5b66<\/h3>\n<\/p>\n<p><p>\u5728\u7269\u7406\u5b66\u4e2d\uff0c\u77e9\u9635\u76f8\u4e58\u7528\u4e8e\u63cf\u8ff0\u548c\u5b9e\u73b0\u5404\u79cd\u7269\u7406\u53d8\u6362\u3002\u4f8b\u5982\uff0c\u5728\u91cf\u5b50\u529b\u5b66\u4e2d\uff0c\u72b6\u6001\u5411\u91cf\u4e0e\u7b97\u7b26\u77e9\u9635\u76f8\u4e58\u5f97\u5230\u65b0\u7684\u72b6\u6001\u5411\u91cf\u3002\u5728\u7ecf\u5178\u529b\u5b66\u4e2d\uff0c\u4f4d\u7f6e\u5411\u91cf\u4e0e\u53d8\u6362\u77e9\u9635\u76f8\u4e58\u5f97\u5230\u65b0\u7684\u4f4d\u7f6e\u5411\u91cf\u3002<\/p>\n<\/p>\n<p><h3>5. \u7ecf\u6d4e\u5b66<\/h3>\n<\/p>\n<p><p>\u5728\u7ecf\u6d4e\u5b66\u4e2d\uff0c\u77e9\u9635\u76f8\u4e58\u7528\u4e8e\u63cf\u8ff0\u548c\u5b9e\u73b0\u7ecf\u6d4e\u6a21\u578b\u3002\u4f8b\u5982\uff0c\u5728\u6295\u5165\u4ea7\u51fa\u6a21\u578b\u4e2d\uff0c\u6295\u5165\u77e9\u9635\u4e0e\u4ea7\u51fa\u77e9\u9635\u76f8\u4e58\u5f97\u5230\u7ecf\u6d4e\u7cfb\u7edf\u7684\u603b\u4ea7\u51fa\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001\u77e9\u9635\u76f8\u4e58\u7684\u4f18\u5316\u6280\u5de7<\/h2>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4e3a\u4e86\u63d0\u9ad8\u77e9\u9635\u76f8\u4e58\u7684\u6548\u7387\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4e00\u4e9b\u4f18\u5316\u6280\u5de7\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u4f18\u5316\u6280\u5de7\uff1a<\/p>\n<\/p>\n<p><h3>1. \u77e9\u9635\u5206\u5757<\/h3>\n<\/p>\n<p><p>\u5c06\u5927\u77e9\u9635\u5206\u89e3\u4e3a\u591a\u4e2a\u5c0f\u77e9\u9635\u5757\uff0c\u5206\u522b\u8fdb\u884c\u76f8\u4e58\uff0c\u7136\u540e\u5408\u5e76\u7ed3\u679c\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5730\u5229\u7528\u7f13\u5b58\uff0c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>2. \u5e76\u884c\u8ba1\u7b97<\/h3>\n<\/p>\n<p><p>\u5229\u7528\u591a\u7ebf\u7a0b\u6216\u591a\u8fdb\u7a0b\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\u3002\u901a\u8fc7\u5c06\u77e9\u9635\u76f8\u4e58\u7684\u4efb\u52a1\u5206\u914d\u7ed9\u591a\u4e2a\u7ebf\u7a0b\u6216\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>3. \u77e9\u9635\u7a00\u758f\u6027<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u77e9\u9635\u662f\u7a00\u758f\u77e9\u9635\uff08\u5373\u5927\u90e8\u5206\u5143\u7d20\u4e3a\u96f6\uff09\uff0c\u53ef\u4ee5\u5229\u7528\u7a00\u758f\u77e9\u9635\u5b58\u50a8\u683c\u5f0f\u548c\u7a00\u758f\u77e9\u9635\u8fd0\u7b97\u7b97\u6cd5\uff0c\u51cf\u5c11\u8ba1\u7b97\u91cf\u548c\u5b58\u50a8\u7a7a\u95f4\u3002<\/p>\n<\/p>\n<p><h3>4. \u77e9\u9635\u4f18\u5316\u5e93<\/h3>\n<\/p>\n<p><p>\u5229\u7528\u4e13\u95e8\u7684\u77e9\u9635\u4f18\u5316\u5e93\uff0c\u4f8b\u5982BLAS\uff08Basic Linear Algebra Subprograms\uff09\u548cLAPACK\uff08Linear Algebra PACKage\uff09\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u77e9\u9635\u76f8\u4e58\u7684\u6548\u7387\u3002\u8fd9\u4e9b\u5e93\u901a\u5e38\u5728\u5e95\u5c42\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u5e76\u4e14\u53ef\u4ee5\u5229\u7528\u786c\u4ef6\u52a0\u901f\u3002<\/p>\n<\/p>\n<p><h3>5. \u77e9\u9635\u7ef4\u5ea6\u7684\u9009\u62e9<\/h3>\n<\/p>\n<p><p>\u5728\u77e9\u9635\u76f8\u4e58\u65f6\uff0c\u5408\u7406\u9009\u62e9\u77e9\u9635\u7684\u7ef4\u5ea6\u53ef\u4ee5\u51cf\u5c11\u8ba1\u7b97\u91cf\u3002\u4f8b\u5982\uff0c\u5728\u8fdb\u884c\u591a\u6b21\u77e9\u9635\u76f8\u4e58\u65f6\uff0c\u53ef\u4ee5\u5148\u8ba1\u7b97\u8f83\u5c0f\u7ef4\u5ea6\u7684\u77e9\u9635\u4e58\u79ef\uff0c\u7136\u540e\u518d\u8ba1\u7b97\u8f83\u5927\u7ef4\u5ea6\u7684\u77e9\u9635\u4e58\u79ef\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u4f18\u5316\u6280\u5de7\uff0c\u6211\u4eec\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u77e9\u9635\u76f8\u4e58\u7684\u6548\u7387\uff0c\u4ece\u800c\u89e3\u51b3\u5927\u89c4\u6a21\u77e9\u9635\u8fd0\u7b97\u4e2d\u7684\u6027\u80fd\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h2>\u4e03\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u4e2d\u77e9\u9635\u76f8\u4e58\u7684\u57fa\u672c\u539f\u7406\u3001\u624b\u52a8\u5b9e\u73b0\u3001NumPy\u5e93\u7684\u4f18\u52bf\u3001\u8fdb\u9636\u77e9\u9635\u64cd\u4f5c\u3001\u5e94\u7528\u573a\u666f\u4ee5\u53ca\u4f18\u5316\u6280\u5de7\u3002\u901a\u8fc7\u5b66\u4e60\u8fd9\u4e9b\u5185\u5bb9\uff0c\u6211\u4eec\u53ef\u4ee5\u638c\u63e1\u77e9\u9635\u76f8\u4e58\u7684\u57fa\u672c\u65b9\u6cd5\u548c\u9ad8\u7ea7\u6280\u5de7\uff0c\u4ece\u800c\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u9ad8\u6548\u3001\u6613\u7528\u548c\u529f\u80fd\u4e30\u5bcc\u7684\u77e9\u9635\u64cd\u4f5c\u5de5\u5177\u3002\u540c\u65f6\uff0c\u6211\u4eec\u4e5f\u9700\u8981\u4e86\u89e3\u77e9\u9635\u76f8\u4e58\u7684\u57fa\u672c\u539f\u7406\u548c\u624b\u52a8\u5b9e\u73b0\u65b9\u6cd5\uff0c\u4ee5\u4fbf\u5728\u9700\u8981\u65f6\u8fdb\u884c\u81ea\u5b9a\u4e49\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u7406\u89e3\u548c\u638c\u63e1Python\u4e2d\u77e9\u9635\u76f8\u4e58\u6709\u6240\u5e2e\u52a9\u3002\u901a\u8fc7\u4e0d\u65ad\u5b9e\u8df5\u548c\u63a2\u7d22\uff0c\u60a8\u5c06\u80fd\u591f\u66f4\u52a0\u719f\u7ec3\u5730\u8fd0\u7528\u8fd9\u4e9b\u77e5\u8bc6\uff0c\u89e3\u51b3\u5404\u79cd\u7ebf\u6027\u4ee3\u6570\u548c\u6570\u636e\u5206\u6790\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u77e9\u9635\u4e58\u6cd5\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5d4c\u5957\u7684\u5217\u8868\u6765\u8868\u793a\u77e9\u9635\u5e76\u624b\u52a8\u5b9e\u73b0\u77e9\u9635\u4e58\u6cd5\u3002\u901a\u8fc7\u4e24\u4e2afor\u5faa\u73af\u904d\u5386\u884c\u548c\u5217\uff0c\u53ef\u4ee5\u9010\u4e2a\u8ba1\u7b97\u5143\u7d20\u7684\u4e58\u79ef\u5e76\u7d2f\u52a0\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">def matrix_multiply(A, B):\n    result = [[0 for _ in range(len(B[0]))] for _ in range(len(A))]\n    for i in range(len(A)):\n        for j in range(len(B[0])):\n            for k in range(len(B)):\n                result[i][j] += A[i][k] * B[k][j]\n    return result\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5b9e\u73b0\u4e86\u4e24\u4e2a\u77e9\u9635\u7684\u4e58\u6cd5\uff0c\u5176\u4e2d<code>A<\/code>\u548c<code>B<\/code>\u662f\u8f93\u5165\u7684\u77e9\u9635\u3002<\/p>\n<p><strong>\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u3002\u4f7f\u7528NumPy\u8fdb\u884c\u77e9\u9635\u76f8\u4e58\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\uff0c\u8fd8\u53ef\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002NumPy\u63d0\u4f9b\u4e86<code>numpy.dot()<\/code>\u548c<code>numpy.matmul()<\/code>\u51fd\u6570\uff0c\u65b9\u4fbf\u7528\u6237\u5feb\u901f\u5b9e\u73b0\u77e9\u9635\u4e58\u6cd5\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nA = np.array([[1, 2], [3, 4]])\nB = np.array([[5, 6], [7, 8]])\nresult = np.dot(A, B)\n<\/code><\/pre>\n<p>\u6b64\u65b9\u6cd5\u80fd\u591f\u5904\u7406\u66f4\u9ad8\u7ef4\u5ea6\u7684\u6570\u7ec4\uff0c\u9002\u5408\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<p><strong>\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u9700\u8981\u4f7f\u7528\u77e9\u9635\u4e58\u6cd5\uff1f<\/strong><br \/>\u77e9\u9635\u4e58\u6cd5\u5728\u8bb8\u591a\u9886\u57df\u4e2d\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5305\u62ec\u8ba1\u7b97\u673a\u56fe\u5f62\u5b66\u3001\u673a\u5668\u5b66\u4e60\u3001\u7269\u7406\u6a21\u62df\u548c\u5de5\u7a0b\u7b49\u3002\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u77e9\u9635\u4e58\u6cd5\u5e38\u7528\u4e8e\u5904\u7406\u7279\u5f81\u5411\u91cf\u548c\u6743\u91cd\u77e9\u9635\u7684\u8fd0\u7b97\uff0c\u4ee5\u5b9e\u73b0\u6a21\u578b\u7684\u8bad\u7ec3\u548c\u63a8\u7406\u3002\u5728\u56fe\u50cf\u5904\u7406\u548c\u4fe1\u53f7\u5904\u7406\u7b49\u9886\u57df\uff0c\u77e9\u9635\u4e58\u6cd5\u4e5f\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u77e9\u9635\u76f8\u4e58\u7684\u6838\u5fc3\u5728\u4e8e\u77e9\u9635\u5143\u7d20\u76f8\u4e58\u5e76\u7d2f\u52a0\u3001\u4f7f\u7528NumPy\u5e93\u5b9e\u73b0\u3001\u8003\u8651\u77e9\u9635\u7ef4\u5ea6\u4e00\u81f4\u6027\u3002\u5176\u4e2d\uff0c\u4f7f\u7528N [&hellip;]","protected":false},"author":3,"featured_media":1170485,"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\/1170477"}],"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=1170477"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170477\/revisions"}],"predecessor-version":[{"id":1170488,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170477\/revisions\/1170488"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170485"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}