{"id":1149996,"date":"2025-01-13T16:56:26","date_gmt":"2025-01-13T08:56:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149996.html"},"modified":"2025-01-13T16:56:28","modified_gmt":"2025-01-13T08:56:28","slug":"python%e5%a6%82%e4%bd%95%e8%a1%a8%e7%a4%ba%e5%af%b9%e8%a7%92%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1149996.html","title":{"rendered":"python\u5982\u4f55\u8868\u793a\u5bf9\u89d2\u77e9\u9635"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25180553\/bd365bf3-9acd-4f8f-9032-3cfb181c2cd4.webp\" alt=\"python\u5982\u4f55\u8868\u793a\u5bf9\u89d2\u77e9\u9635\" \/><\/p>\n<p><p> Python\u4e2d\u53ef\u4ee5\u4f7f\u7528<code>NumPy<\/code>\u5e93\u6765\u8868\u793a\u5bf9\u89d2\u77e9\u9635\u3002<strong>\u901a\u8fc7<code>numpy.diag<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>numpy.eye<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>scipy.sparse.diags<\/code>\u51fd\u6570\u3001\u624b\u52a8\u521b\u5efa\u5bf9\u89d2\u77e9\u9635<\/strong>\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5b9e\u73b0\u5bf9\u89d2\u77e9\u9635\u7684\u8868\u793a\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u2014\u2014\u901a\u8fc7<code>numpy.diag<\/code>\u51fd\u6570\u6765\u521b\u5efa\u5bf9\u89d2\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p><code>numpy.diag<\/code>\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u7ed9\u5b9a\u7684\u5bf9\u89d2\u7ebf\u5143\u7d20\u751f\u6210\u5bf9\u89d2\u77e9\u9635\u3002\u4f7f\u7528\u8be5\u51fd\u6570\u65f6\uff0c\u60a8\u53ea\u9700\u63d0\u4f9b\u4e00\u4e2a\u5305\u542b\u5bf9\u89d2\u7ebf\u5143\u7d20\u7684\u6570\u7ec4\u6216\u5217\u8868\uff0c\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5bf9\u5e94\u7684\u5bf9\u89d2\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u5305\u542b\u5bf9\u89d2\u7ebf\u5143\u7d20\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>diagonal_elements = np.array([1, 2, 3])<\/p>\n<h2><strong>\u751f\u6210\u5bf9\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>diagonal_matrix = np.diag(diagonal_elements)<\/p>\n<p>print(diagonal_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u5bf9\u89d2\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1 0 0]<\/p>\n<p> [0 2 0]<\/p>\n<p> [0 0 3]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u4e2d\u8868\u793a\u5bf9\u89d2\u77e9\u9635\u7684\u5176\u4ed6\u65b9\u6cd5\u548c\u76f8\u5173\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>numpy.diag<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>numpy.diag<\/code>\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u751f\u6210\u5bf9\u89d2\u77e9\u9635\uff0c\u8fd8\u53ef\u4ee5\u63d0\u53d6\u77e9\u9635\u7684\u5bf9\u89d2\u7ebf\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u8be5\u51fd\u6570\u7684\u8be6\u7ec6\u7528\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u751f\u6210\u5bf9\u89d2\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u7ed9\u5b9a\u7684\u5bf9\u89d2\u7ebf\u5143\u7d20\u751f\u6210\u5bf9\u89d2\u77e9\u9635\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u7ef4\u6570\u7ec4\u6216\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u5305\u542b\u5bf9\u89d2\u7ebf\u5143\u7d20\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>diagonal_elements = np.array([4, 5, 6])<\/p>\n<h2><strong>\u751f\u6210\u5bf9\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>diagonal_matrix = np.diag(diagonal_elements)<\/p>\n<p>print(diagonal_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u5bf9\u89d2\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[4 0 0]<\/p>\n<p> [0 5 0]<\/p>\n<p> [0 0 6]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u63d0\u53d6\u5bf9\u89d2\u7ebf\u5143\u7d20<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5df2\u7ecf\u6709\u4e00\u4e2a\u77e9\u9635\uff0c\u5e76\u4e14\u5e0c\u671b\u63d0\u53d6\u8be5\u77e9\u9635\u7684\u5bf9\u89d2\u7ebf\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528<code>numpy.diag<\/code>\u51fd\u6570\u3002\u6b64\u65f6\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u65b9\u9635\u4f5c\u4e3a\u8f93\u5165\u53c2\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[7, 8, 9],<\/p>\n<p>                   [10, 11, 12],<\/p>\n<p>                   [13, 14, 15]])<\/p>\n<h2><strong>\u63d0\u53d6\u77e9\u9635\u7684\u5bf9\u89d2\u7ebf\u5143\u7d20<\/strong><\/h2>\n<p>diagonal_elements = np.diag(matrix)<\/p>\n<p>print(diagonal_elements)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u5bf9\u89d2\u7ebf\u5143\u7d20\uff1a<\/p>\n<\/p>\n<p><pre><code>[ 7 11 15]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u63a7\u5236\u5bf9\u89d2\u7ebf\u4f4d\u7f6e<\/h3>\n<\/p>\n<p><p><code>numpy.diag<\/code>\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u53ef\u4ee5\u63a7\u5236\u5bf9\u89d2\u7ebf\u7684\u4f4d\u7f6e\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a0\uff0c\u8868\u793a\u4e3b\u5bf9\u89d2\u7ebf\u3002\u6b63\u6570\u8868\u793a\u4e0a\u5bf9\u89d2\u7ebf\uff0c\u8d1f\u6570\u8868\u793a\u4e0b\u5bf9\u89d2\u7ebf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u5305\u542b\u5bf9\u89d2\u7ebf\u5143\u7d20\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>diagonal_elements = np.array([1, 2, 3])<\/p>\n<h2><strong>\u751f\u6210\u4e0a\u5bf9\u89d2\u7ebf\u77e9\u9635<\/strong><\/h2>\n<p>upper_diagonal_matrix = np.diag(diagonal_elements, k=1)<\/p>\n<p>print(upper_diagonal_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u4e0a\u5bf9\u89d2\u7ebf\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[0 1 0 0]<\/p>\n<p> [0 0 2 0]<\/p>\n<p> [0 0 0 3]<\/p>\n<p> [0 0 0 0]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>numpy.eye<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>numpy.eye<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u5355\u4f4d\u77e9\u9635\uff08\u5bf9\u89d2\u7ebf\u5143\u7d20\u4e3a1\uff0c\u5176\u4f59\u5143\u7d20\u4e3a0\uff09\uff0c\u5e76\u4e14\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u6307\u5b9a\u77e9\u9635\u7684\u5f62\u72b6\u548c\u5bf9\u89d2\u7ebf\u7684\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u62103x3\u7684\u5355\u4f4d\u77e9\u9635<\/strong><\/h2>\n<p>identity_matrix = np.eye(3)<\/p>\n<p>print(identity_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u5355\u4f4d\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1. 0. 0.]<\/p>\n<p> [0. 1. 0.]<\/p>\n<p> [0. 0. 1.]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u63a7\u5236\u5bf9\u89d2\u7ebf\u4f4d\u7f6e<\/h3>\n<\/p>\n<p><p><code>numpy.eye<\/code>\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u53ef\u4ee5\u63a7\u5236\u5bf9\u89d2\u7ebf\u7684\u4f4d\u7f6e\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e3a0\uff0c\u8868\u793a\u4e3b\u5bf9\u89d2\u7ebf\u3002\u6b63\u6570\u8868\u793a\u4e0a\u5bf9\u89d2\u7ebf\uff0c\u8d1f\u6570\u8868\u793a\u4e0b\u5bf9\u89d2\u7ebf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e0a\u5bf9\u89d2\u7ebf\u5355\u4f4d\u77e9\u9635<\/strong><\/h2>\n<p>upper_identity_matrix = np.eye(3, k=1)<\/p>\n<p>print(upper_identity_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u4e0a\u5bf9\u89d2\u7ebf\u5355\u4f4d\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[0. 1. 0.]<\/p>\n<p> [0. 0. 1.]<\/p>\n<p> [0. 0. 0.]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528<code>scipy.sparse.diags<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>scipy.sparse.diags<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u7a00\u758f\u5bf9\u89d2\u77e9\u9635\uff0c\u5bf9\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u975e\u5e38\u6709\u6548\u3002\u8be5\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u591a\u4e2a\u5bf9\u89d2\u7ebf\u7684\u7a00\u758f\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.sparse import diags<\/p>\n<h2><strong>\u521b\u5efa\u5305\u542b\u5bf9\u89d2\u7ebf\u5143\u7d20\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>diagonal_elements = [1, 2, 3]<\/p>\n<h2><strong>\u751f\u6210\u7a00\u758f\u5bf9\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>sparse_diagonal_matrix = diags(diagonal_elements)<\/p>\n<p>print(sparse_diagonal_matrix.toarray())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u7a00\u758f\u5bf9\u89d2\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1. 0. 0.]<\/p>\n<p> [0. 2. 0.]<\/p>\n<p> [0. 0. 3.]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u591a\u5bf9\u89d2\u7ebf\u7a00\u758f\u77e9\u9635<\/h3>\n<\/p>\n<p><p><code>scipy.sparse.diags<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u5305\u542b\u591a\u4e2a\u5bf9\u89d2\u7ebf\u7684\u7a00\u758f\u77e9\u9635\u3002\u9700\u8981\u63d0\u4f9b\u591a\u4e2a\u5bf9\u89d2\u7ebf\u5143\u7d20\u7684\u6570\u7ec4\u548c\u5bf9\u5e94\u7684\u5bf9\u89d2\u7ebf\u4f4d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from scipy.sparse import diags<\/p>\n<h2><strong>\u521b\u5efa\u5305\u542b\u5bf9\u89d2\u7ebf\u5143\u7d20\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>diagonals = [[1, 2, 3], [4, 5]]<\/p>\n<h2><strong>\u5bf9\u5e94\u7684\u5bf9\u89d2\u7ebf\u4f4d\u7f6e<\/strong><\/h2>\n<p>positions = [0, 1]<\/p>\n<h2><strong>\u751f\u6210\u7a00\u758f\u5bf9\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>sparse_diagonal_matrix = diags(diagonals, positions)<\/p>\n<p>print(sparse_diagonal_matrix.toarray())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u591a\u5bf9\u89d2\u7ebf\u7a00\u758f\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1. 4. 0.]<\/p>\n<p> [0. 2. 5.]<\/p>\n<p> [0. 0. 3.]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u624b\u52a8\u521b\u5efa\u5bf9\u89d2\u77e9\u9635<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u624b\u52a8\u521b\u5efa\u5bf9\u89d2\u77e9\u9635\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u5d4c\u5957\u7684\u5faa\u73af\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u5305\u542b\u5bf9\u89d2\u7ebf\u5143\u7d20\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>diagonal_elements = np.array([1, 2, 3])<\/p>\n<h2><strong>\u83b7\u53d6\u77e9\u9635\u7684\u7ef4\u5ea6<\/strong><\/h2>\n<p>n = len(diagonal_elements)<\/p>\n<h2><strong>\u521b\u5efa\u96f6\u77e9\u9635<\/strong><\/h2>\n<p>diagonal_matrix = np.zeros((n, n))<\/p>\n<h2><strong>\u8bbe\u7f6e\u5bf9\u89d2\u7ebf\u5143\u7d20<\/strong><\/h2>\n<p>for i in range(n):<\/p>\n<p>    diagonal_matrix[i, i] = diagonal_elements[i]<\/p>\n<p>print(diagonal_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u5bf9\u89d2\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1. 0. 0.]<\/p>\n<p> [0. 2. 0.]<\/p>\n<p> [0. 0. 3.]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u624b\u52a8\u521b\u5efa\u4e0a\u5bf9\u89d2\u7ebf\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u60a8\u53ef\u4ee5\u624b\u52a8\u521b\u5efa\u4e0a\u5bf9\u89d2\u7ebf\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u5305\u542b\u5bf9\u89d2\u7ebf\u5143\u7d20\u7684\u6570\u7ec4<\/strong><\/h2>\n<p>diagonal_elements = np.array([1, 2, 3])<\/p>\n<h2><strong>\u83b7\u53d6\u77e9\u9635\u7684\u7ef4\u5ea6<\/strong><\/h2>\n<p>n = len(diagonal_elements)<\/p>\n<h2><strong>\u521b\u5efa\u96f6\u77e9\u9635<\/strong><\/h2>\n<p>upper_diagonal_matrix = np.zeros((n + 1, n + 1))<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e0a\u5bf9\u89d2\u7ebf\u5143\u7d20<\/strong><\/h2>\n<p>for i in range(n):<\/p>\n<p>    upper_diagonal_matrix[i, i + 1] = diagonal_elements[i]<\/p>\n<p>print(upper_diagonal_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u4e0a\u5bf9\u89d2\u7ebf\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[0. 1. 0. 0.]<\/p>\n<p> [0. 0. 2. 0.]<\/p>\n<p> [0. 0. 0. 3.]<\/p>\n<p> [0. 0. 0. 0.]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5bf9\u89d2\u77e9\u9635\u7684\u57fa\u672c\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>\u5bf9\u89d2\u77e9\u9635\u7684\u57fa\u672c\u64cd\u4f5c\u5305\u62ec\u77e9\u9635\u7684\u52a0\u6cd5\u3001\u4e58\u6cd5\u3001\u6c42\u9006\u7b49\u3002\u7531\u4e8e\u5bf9\u89d2\u77e9\u9635\u5177\u6709\u7279\u6b8a\u7684\u7ed3\u6784\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u901a\u5e38\u6bd4\u4e00\u822c\u77e9\u9635\u66f4\u7b80\u5355\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h3>\u5bf9\u89d2\u77e9\u9635\u7684\u52a0\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u89d2\u77e9\u9635\u7684\u52a0\u6cd5\u662f\u5c06\u5bf9\u5e94\u4f4d\u7f6e\u7684\u5bf9\u89d2\u7ebf\u5143\u7d20\u76f8\u52a0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u5bf9\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>diagonal_matrix1 = np.diag([1, 2, 3])<\/p>\n<p>diagonal_matrix2 = np.diag([4, 5, 6])<\/p>\n<h2><strong>\u5bf9\u89d2\u77e9\u9635\u7684\u52a0\u6cd5<\/strong><\/h2>\n<p>result_matrix = diagonal_matrix1 + diagonal_matrix2<\/p>\n<p>print(result_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u7ed3\u679c\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[ 5  0  0]<\/p>\n<p> [ 0  7  0]<\/p>\n<p> [ 0  0  9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5bf9\u89d2\u77e9\u9635\u7684\u4e58\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u89d2\u77e9\u9635\u7684\u4e58\u6cd5\u662f\u5c06\u5bf9\u5e94\u4f4d\u7f6e\u7684\u5bf9\u89d2\u7ebf\u5143\u7d20\u76f8\u4e58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e24\u4e2a\u5bf9\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>diagonal_matrix1 = np.diag([1, 2, 3])<\/p>\n<p>diagonal_matrix2 = np.diag([4, 5, 6])<\/p>\n<h2><strong>\u5bf9\u89d2\u77e9\u9635\u7684\u4e58\u6cd5<\/strong><\/h2>\n<p>result_matrix = diagonal_matrix1 @ diagonal_matrix2<\/p>\n<p>print(result_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u7ed3\u679c\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[ 4  0  0]<\/p>\n<p> [ 0 10  0]<\/p>\n<p> [ 0  0 18]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5bf9\u89d2\u77e9\u9635\u7684\u6c42\u9006<\/h3>\n<\/p>\n<p><p>\u5bf9\u89d2\u77e9\u9635\u7684\u6c42\u9006\u662f\u5c06\u6bcf\u4e2a\u5bf9\u89d2\u7ebf\u5143\u7d20\u53d6\u5012\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>diagonal_matrix = np.diag([1, 2, 3])<\/p>\n<h2><strong>\u5bf9\u89d2\u77e9\u9635\u7684\u6c42\u9006<\/strong><\/h2>\n<p>inverse_matrix = np.diag(1 \/ np.diag(diagonal_matrix))<\/p>\n<p>print(inverse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u9006\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1.         0.         0.        ]<\/p>\n<p> [0.         0.5        0.        ]<\/p>\n<p> [0.         0.         0.33333333]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5bf9\u89d2\u77e9\u9635\u7684\u8ff9<\/h3>\n<\/p>\n<p><p>\u5bf9\u89d2\u77e9\u9635\u7684\u8ff9\u662f\u5bf9\u89d2\u7ebf\u5143\u7d20\u7684\u548c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u5bf9\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>diagonal_matrix = np.diag([1, 2, 3])<\/p>\n<h2><strong>\u5bf9\u89d2\u77e9\u9635\u7684\u8ff9<\/strong><\/h2>\n<p>trace = np.trace(diagonal_matrix)<\/p>\n<p>print(trace)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>6<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5bf9\u89d2\u77e9\u9635\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u5bf9\u89d2\u77e9\u9635\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u5de5\u7a0b\u5e94\u7528\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\u4f8b\u5982\uff0c\u5728\u7279\u5f81\u503c\u5206\u89e3\u3001\u4fe1\u53f7\u5904\u7406\u548c\u56fe\u50cf\u5904\u7406\u7b49\u9886\u57df\uff0c\u5bf9\u89d2\u77e9\u9635\u8d77\u7740\u91cd\u8981\u7684\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u7279\u5f81\u503c\u5206\u89e3<\/h3>\n<\/p>\n<p><p>\u5728\u7279\u5f81\u503c\u5206\u89e3\u4e2d\uff0c\u65b9\u9635\u53ef\u4ee5\u5206\u89e3\u4e3a\u5bf9\u89d2\u77e9\u9635\u548c\u7279\u5f81\u5411\u91cf\u77e9\u9635\u7684\u4e58\u79ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u65b9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[4, 1],<\/p>\n<p>                   [2, 3]])<\/p>\n<h2><strong>\u7279\u5f81\u503c\u5206\u89e3<\/strong><\/h2>\n<p>eigenvalues, eigenvectors = np.linalg.eig(matrix)<\/p>\n<h2><strong>\u751f\u6210\u5bf9\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>diagonal_matrix = np.diag(eigenvalues)<\/p>\n<p>print(diagonal_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u5bf9\u89d2\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[5. 0.]<\/p>\n<p> [0. 2.]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4fe1\u53f7\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u4fe1\u53f7\u5904\u7406\u7684\u5085\u91cc\u53f6\u53d8\u6362\u4e2d\uff0c\u9891\u57df\u7684\u5bf9\u89d2\u77e9\u9635\u8868\u793a\u9891\u7387\u5206\u91cf\u7684\u5e45\u5ea6\u548c\u76f8\u4f4d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u65f6\u95f4\u57df\u4fe1\u53f7<\/strong><\/h2>\n<p>time_signal = np.array([1, 2, 3, 4])<\/p>\n<h2><strong>\u5085\u91cc\u53f6\u53d8\u6362<\/strong><\/h2>\n<p>frequency_signal = np.fft.fft(time_signal)<\/p>\n<h2><strong>\u751f\u6210\u9891\u57df\u7684\u5bf9\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>diagonal_matrix = np.diag(frequency_signal)<\/p>\n<p>print(diagonal_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u9891\u57df\u7684\u5bf9\u89d2\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[10.+0.j  0.+0.j  0.+0.j  0.+0.j]<\/p>\n<p> [ 0.+0.j -2.+2.j  0.+0.j  0.+0.j]<\/p>\n<p> [ 0.+0.j  0.+0.j -2.+0.j  0.+0.j]<\/p>\n<p> [ 0.+0.j  0.+0.j  0.+0.j -2.-2.j]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56fe\u50cf\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u7684\u4e3b\u6210\u5206\u5206\u6790\uff08PCA\uff09\u4e2d\uff0c\u5bf9\u89d2\u77e9\u9635\u7528\u4e8e\u8868\u793a\u7279\u5f81\u503c\uff0c\u5176\u5bf9\u5e94\u7684\u7279\u5f81\u5411\u91cf\u7528\u4e8e\u56fe\u50cf\u964d\u7ef4\u548c\u7279\u5f81\u63d0\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from sklearn.decomposition import PCA<\/p>\n<h2><strong>\u521b\u5efa\u968f\u673a\u56fe\u50cf\u6570\u636e<\/strong><\/h2>\n<p>image_data = np.random.rand(100, 100)<\/p>\n<h2><strong>\u4e3b\u6210\u5206\u5206\u6790<\/strong><\/h2>\n<p>pca = PCA(n_components=2)<\/p>\n<p>pca.fit(image_data)<\/p>\n<h2><strong>\u751f\u6210\u5bf9\u89d2\u77e9\u9635<\/strong><\/h2>\n<p>diagonal_matrix = np.diag(pca.expl<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ned_variance_)<\/p>\n<p>print(diagonal_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u8f93\u51fa\u4ee5\u4e0b\u5bf9\u89d2\u77e9\u9635\uff1a<\/p>\n<\/p>\n<p><pre><code>[[8.68151227 0.        ]<\/p>\n<p> [0.         8.45526909]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7075\u6d3b\u5730\u4f7f\u7528Python\u6765\u8868\u793a\u548c\u64cd\u4f5c\u5bf9\u89d2\u77e9\u9635\u3002<strong>\u4e86\u89e3\u548c\u638c\u63e1\u5bf9\u89d2\u77e9\u9635\u7684\u8868\u793a\u65b9\u6cd5\u3001\u57fa\u672c\u64cd\u4f5c\u548c\u5e94\u7528<\/strong>\uff0c\u4e0d\u4ec5\u6709\u52a9\u4e8e\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\uff0c\u8fd8\u80fd\u4e3a\u79d1\u5b66\u8ba1\u7b97\u548c\u5de5\u7a0b\u5e94\u7528\u63d0\u4f9b\u5f3a\u6709\u529b\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5bf9\u89d2\u77e9\u9635\u662f\u4ec0\u4e48\uff1f\u5982\u4f55\u5728Python\u4e2d\u5b9a\u4e49\u5b83\uff1f<\/strong><br \/>\u5bf9\u89d2\u77e9\u9635\u662f\u6307\u9664\u4e86\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u7684\u5143\u7d20\u5916\uff0c\u5176\u4f59\u6240\u6709\u5143\u7d20\u5747\u4e3a\u96f6\u7684\u65b9\u9635\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5b9a\u4e49\u5bf9\u89d2\u77e9\u9635\u3002\u5177\u4f53\u65b9\u6cd5\u4e3a\u4f7f\u7528<code>numpy.diag()<\/code>\u51fd\u6570\uff0c\u5c06\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u4f5c\u4e3a\u8f93\u5165\uff0c\u5373\u53ef\u751f\u6210\u4e00\u4e2a\u5bf9\u5e94\u7684\u5bf9\u89d2\u77e9\u9635\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5feb\u901f\u521b\u5efa\u4e00\u4e2a\u5bf9\u89d2\u77e9\u9635\u7684\u5b9e\u4f8b\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u7247\u6bb5\u8f7b\u677e\u521b\u5efa\u4e00\u4e2a\u5bf9\u89d2\u77e9\u9635\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\n\n# \u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\nelements = np.array([1, 2, 3])\n# \u751f\u6210\u5bf9\u89d2\u77e9\u9635\ndiagonal_matrix = np.diag(elements)\nprint(diagonal_matrix)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u751f\u6210\u4e00\u4e2a3&#215;3\u7684\u5bf9\u89d2\u77e9\u9635\uff0c\u5176\u4e2d\u4e3b\u5bf9\u89d2\u7ebf\u4e0a\u7684\u5143\u7d20\u4e3a1\u30012\u30013\uff0c\u5176\u4f59\u5143\u7d20\u4e3a\u96f6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5bf9\u5bf9\u89d2\u77e9\u9635\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u5bf9\u89d2\u77e9\u9635\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\u3002\u53ef\u4ee5\u8fdb\u884c\u52a0\u6cd5\u3001\u4e58\u6cd5\u4ee5\u53ca\u6c42\u9006\u7b49\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u4e24\u4e2a\u5bf9\u89d2\u77e9\u9635\u7684\u52a0\u6cd5\u662f\u901a\u8fc7\u9010\u4e2a\u5bf9\u89d2\u5143\u7d20\u76f8\u52a0\u5b9e\u73b0\u7684\uff0c\u800c\u4e58\u6cd5\u5219\u662f\u901a\u8fc7\u5bf9\u89d2\u5143\u7d20\u7684\u4e58\u79ef\u6765\u5b8c\u6210\u7684\u3002\u4ee5\u4e0b\u662f\u52a0\u6cd5\u548c\u4e58\u6cd5\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\"># \u5b9a\u4e49\u4e24\u4e2a\u5bf9\u89d2\u77e9\u9635\nmatrix1 = np.diag([1, 2, 3])\nmatrix2 = np.diag([4, 5, 6])\n\n# \u8fdb\u884c\u52a0\u6cd5\u548c\u4e58\u6cd5\nsum_matrix = matrix1 + matrix2\nproduct_matrix = matrix1 @ matrix2  # \u4f7f\u7528@\u8fd0\u7b97\u7b26\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\n\nprint(&quot;\u548c\u77e9\u9635:\\n&quot;, sum_matrix)\nprint(&quot;\u79ef\u77e9\u9635:\\n&quot;, product_matrix)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u4fbf\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u5bf9\u89d2\u77e9\u9635\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u8868\u793a\u5bf9\u89d2\u77e9\u9635\u3002\u901a\u8fc7numpy.diag\u51fd\u6570\u3001\u4f7f\u7528numpy.eye\u51fd\u6570\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1149999,"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\/1149996"}],"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=1149996"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149996\/revisions"}],"predecessor-version":[{"id":1150003,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149996\/revisions\/1150003"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149999"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}