{"id":1106293,"date":"2025-01-08T16:38:50","date_gmt":"2025-01-08T08:38:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1106293.html"},"modified":"2025-01-08T16:38:52","modified_gmt":"2025-01-08T08:38:52","slug":"python%e5%a6%82%e4%bd%95%e5%af%b9%e4%b8%80%e4%b8%aa%e7%9f%a9%e9%98%b5%e8%bf%9b%e8%a1%8c%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1106293.html","title":{"rendered":"python\u5982\u4f55\u5bf9\u4e00\u4e2a\u77e9\u9635\u8fdb\u884c\u5904\u7406"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25070807\/0346e801-1a3b-4c04-99f6-961a17b9efb3.webp\" alt=\"python\u5982\u4f55\u5bf9\u4e00\u4e2a\u77e9\u9635\u8fdb\u884c\u5904\u7406\" \/><\/p>\n<p><p> <strong>Python\u5bf9\u77e9\u9635\u8fdb\u884c\u5904\u7406\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u64cd\u4f5c\u3001\u4f7f\u7528pandas\u5e93\u8fdb\u884c\u6570\u636e\u64cd\u4f5c\u3001\u4f7f\u7528SciPy\u5e93\u8fdb\u884c\u77e9\u9635\u64cd\u4f5c\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u5904\u7406\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u56e0\u5176\u9ad8\u6548\u548c\u7b80\u6d01\u3002\u4e0b\u9762\u8be6\u7ec6\u5c55\u5f00\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u77e9\u9635\u5904\u7406\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001NUMPY\u5e93\u6982\u8ff0<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u57fa\u7840\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9\u9ad8\u6027\u80fd\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u5e7f\u6cdb\u7684\u6570\u5b66\u51fd\u6570\u5e93\u7684\u652f\u6301\u3002NumPy\u7684\u6838\u5fc3\u662f\u5176\u5f3a\u5927\u7684n\u7ef4\u6570\u7ec4\u5bf9\u8c61\uff08ndarray\uff09\uff0c\u80fd\u591f\u6709\u6548\u5730\u5b58\u50a8\u548c\u64cd\u4f5c\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5NumPy<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u5e93\u3002\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><h3>\u4e8c\u3001\u521b\u5efa\u77e9\u9635<\/h3>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u5217\u8868\u521b\u5efa\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528NumPy\u7684<code>array<\/code>\u51fd\u6570\u5c06\u5217\u8868\u6216\u5143\u7ec4\u8f6c\u6362\u4e3a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528<code>arange<\/code>\u548c<code>reshape<\/code>\u51fd\u6570\u521b\u5efa\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>arange<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u5185\u7684\u6570\u7ec4\uff0c\u7136\u540e\u4f7f\u7528<code>reshape<\/code>\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6307\u5b9a\u5f62\u72b6\u7684\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.arange(1, 10).reshape(3, 3)<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u77e9\u9635\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u77e9\u9635\u5143\u7d20\u8bbf\u95ee<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6765\u8bbf\u95ee\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">element = matrix[0, 1]<\/p>\n<p>print(element)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u77e9\u9635\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u5207\u7247\u64cd\u4f5c\u6765\u8bbf\u95ee\u77e9\u9635\u7684\u5b50\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sub_matrix = matrix[0:2, 1:3]<\/p>\n<p>print(sub_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u77e9\u9635\u5143\u7d20\u8d4b\u503c<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u7d22\u5f15\u6216\u5207\u7247\u6765\u5bf9\u77e9\u9635\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix[0, 0] = 10<\/p>\n<p>matrix[1:3, 1:3] = 0<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u77e9\u9635\u8fd0\u7b97<\/h3>\n<\/p>\n<p><h4>1\u3001\u77e9\u9635\u52a0\u6cd5<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>+<\/code>\u64cd\u4f5c\u7b26\u6216<code>add<\/code>\u51fd\u6570\u8fdb\u884c\u77e9\u9635\u52a0\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<p>result = matrix1 + matrix2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u77e9\u9635\u51cf\u6cd5<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>-<\/code>\u64cd\u4f5c\u7b26\u6216<code>subtract<\/code>\u51fd\u6570\u8fdb\u884c\u77e9\u9635\u51cf\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">result = matrix1 - matrix2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u77e9\u9635\u4e58\u6cd5<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>*<\/code>\u64cd\u4f5c\u7b26\u8fdb\u884c\u5143\u7d20\u4e58\u6cd5\uff0c\u6216\u4f7f\u7528<code>dot<\/code>\u51fd\u6570\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5143\u7d20\u4e58\u6cd5<\/p>\n<p>result = matrix1 * matrix2<\/p>\n<p>print(result)<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>result = np.dot(matrix1, matrix2)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4\u3001\u77e9\u9635\u8f6c\u7f6e<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>transpose<\/code>\u51fd\u6570\u6216<code>.T<\/code>\u5c5e\u6027\u8fdb\u884c\u77e9\u9635\u8f6c\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">transposed_matrix = matrix.T<\/p>\n<p>print(transposed_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u77e9\u9635\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1\u3001\u77e9\u9635\u6c42\u9006<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>linalg.inv<\/code>\u51fd\u6570\u8ba1\u7b97\u77e9\u9635\u7684\u9006\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.array([[1, 2], [3, 4]])<\/p>\n<p>inverse_matrix = np.linalg.inv(matrix)<\/p>\n<p>print(inverse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u77e9\u9635\u884c\u5217\u5f0f<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>linalg.det<\/code>\u51fd\u6570\u8ba1\u7b97\u77e9\u9635\u7684\u884c\u5217\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">determinant = np.linalg.det(matrix)<\/p>\n<p>print(determinant)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u77e9\u9635\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>linalg.eig<\/code>\u51fd\u6570\u8ba1\u7b97\u77e9\u9635\u7684\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">eigenvalues, eigenvectors = np.linalg.eig(matrix)<\/p>\n<p>print(eigenvalues)<\/p>\n<p>print(eigenvectors)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528Pandas\u5e93\u8fdb\u884c\u77e9\u9635\u5904\u7406<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u4e5f\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u7279\u522b\u9002\u5408\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u9884\u5904\u7406\u3002\u867d\u7136Pandas\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u6570\u636e\u8868\u683c\uff0c\u4f46\u4e5f\u53ef\u4ee5\u7528\u4e8e\u77e9\u9635\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Pandas<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86Pandas\u5e93\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 pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528Pandas\u5904\u7406\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Pandas\u7684<code>DataFrame<\/code>\u5bf9\u8c61\u6765\u521b\u5efa\u548c\u5904\u7406\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u77e9\u9635<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6], &#39;C&#39;: [7, 8, 9]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<h2><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong><\/h2>\n<p>transposed_df = df.T<\/p>\n<p>print(transposed_df)<\/p>\n<h2><strong>\u77e9\u9635\u52a0\u6cd5<\/strong><\/h2>\n<p>df2 = df + df<\/p>\n<p>print(df2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528SciPy\u5e93\u8fdb\u884c\u77e9\u9635\u5904\u7406<\/h3>\n<\/p>\n<p><p>SciPy\u5e93\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u9ad8\u7ea7\u5e93\uff0c\u57fa\u4e8eNumPy\u6784\u5efa\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7684\u79d1\u5b66\u8ba1\u7b97\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5SciPy<\/h4>\n<\/p>\n<p><p>\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86SciPy\u5e93\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 scipy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528SciPy\u5904\u7406\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528SciPy\u7684<code>linalg<\/code>\u6a21\u5757\u8fdb\u884c\u77e9\u9635\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy import linalg<\/p>\n<h2><strong>\u521b\u5efa\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[1, 2], [3, 4]])<\/p>\n<h2><strong>\u77e9\u9635\u6c42\u9006<\/strong><\/h2>\n<p>inverse_matrix = linalg.inv(matrix)<\/p>\n<p>print(inverse_matrix)<\/p>\n<h2><strong>\u77e9\u9635\u884c\u5217\u5f0f<\/strong><\/h2>\n<p>determinant = linalg.det(matrix)<\/p>\n<p>print(determinant)<\/p>\n<h2><strong>\u77e9\u9635\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf<\/strong><\/h2>\n<p>eigenvalues, eigenvectors = linalg.eig(matrix)<\/p>\n<p>print(eigenvalues)<\/p>\n<p>print(eigenvectors)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u77e9\u9635\u5904\u7406\u7684\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1\u3001\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u548c\u6570\u636e\u9884\u5904\u7406\u4e2d\uff0c\u77e9\u9635\u64cd\u4f5c\u975e\u5e38\u5e38\u89c1\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u77e9\u9635\u8fd0\u7b97\u8fdb\u884c\u6570\u636e\u7684\u6807\u51c6\u5316\u3001\u5f52\u4e00\u5316\u3001\u7f3a\u5931\u503c\u586b\u5145\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6df1\u5ea6\u5b66\u4e60<\/h4>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u4e2d\uff0c\u77e9\u9635\u8fd0\u7b97\u662f\u57fa\u7840\u3002\u4f8b\u5982\uff0c\u7ebf\u6027\u56de\u5f52\u3001\u903b\u8f91\u56de\u5f52\u3001\u795e\u7ecf\u7f51\u7edc\u7b49\u7b97\u6cd5\u90fd\u6d89\u53ca\u5927\u91cf\u7684\u77e9\u9635\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u4e2d\uff0c\u56fe\u50cf\u53ef\u4ee5\u770b\u4f5c\u662f\u77e9\u9635\uff0c\u6bcf\u4e2a\u50cf\u7d20\u70b9\u5bf9\u5e94\u4e00\u4e2a\u77e9\u9635\u5143\u7d20\u3002\u53ef\u4ee5\u4f7f\u7528\u77e9\u9635\u8fd0\u7b97\u8fdb\u884c\u56fe\u50cf\u7684\u65cb\u8f6c\u3001\u7f29\u653e\u3001\u6ee4\u6ce2\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u7269\u7406\u548c\u5de5\u7a0b\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>\u5728\u7269\u7406\u548c\u5de5\u7a0b\u8ba1\u7b97\u4e2d\uff0c\u77e9\u9635\u8fd0\u7b97\u7528\u4e8e\u6c42\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4\u3001\u8ba1\u7b97\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u3001\u8fdb\u884c\u7ed3\u6784\u5206\u6790\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u77e9\u9635\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u662f\u4e00\u4e2a\u91cd\u8981\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u4f18\u5316\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528NumPy\u7684\u5e7f\u64ad\u673a\u5236<\/h4>\n<\/p>\n<p><p>NumPy\u7684\u5e7f\u64ad\u673a\u5236\u5141\u8bb8\u5728\u4e0d\u540c\u5f62\u72b6\u7684\u6570\u7ec4\u4e4b\u95f4\u8fdb\u884c\u64cd\u4f5c\uff0c\u65e0\u9700\u663e\u5f0f\u5730\u8fdb\u884c\u5faa\u73af\uff0c\u4ece\u800c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>vector = np.array([1, 2, 3])<\/p>\n<h2><strong>\u5e7f\u64ad\u673a\u5236<\/strong><\/h2>\n<p>result = matrix + vector<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528NumPy\u7684\u5411\u91cf\u5316\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5411\u91cf\u5316\u64cd\u4f5c\u53ef\u4ee5\u907f\u514d\u663e\u5f0f\u7684Python\u5faa\u73af\uff0c\u4ece\u800c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u975e\u5411\u91cf\u5316\u64cd\u4f5c<\/p>\n<p>result = np.zeros(matrix.shape)<\/p>\n<p>for i in range(matrix.shape[0]):<\/p>\n<p>    for j in range(matrix.shape[1]):<\/p>\n<p>        result[i, j] = matrix[i, j] + vector[j]<\/p>\n<p>print(result)<\/p>\n<h2><strong>\u5411\u91cf\u5316\u64cd\u4f5c<\/strong><\/h2>\n<p>result = matrix + vector<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4f7f\u7528NumPy\u7684\u5185\u7f6e\u51fd\u6570<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u53ef\u4ee5\u907f\u514d\u663e\u5f0f\u7684\u5faa\u73af\uff0c\u4ece\u800c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u975e\u5185\u7f6e\u51fd\u6570<\/p>\n<p>result = np.zeros(matrix.shape)<\/p>\n<p>for i in range(matrix.shape[0]):<\/p>\n<p>    for j in range(matrix.shape[1]):<\/p>\n<p>        result[i, j] = np.sqrt(matrix[i, j])<\/p>\n<p>print(result)<\/p>\n<h2><strong>\u5185\u7f6e\u51fd\u6570<\/strong><\/h2>\n<p>result = np.sqrt(matrix)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u77e9\u9635\u5904\u7406\u65b9\u6cd5\uff0c\u5176\u4e2dNumPy\u5e93\u662f\u6700\u5e38\u7528\u548c\u9ad8\u6548\u7684\u5de5\u5177\u3002\u901a\u8fc7\u5b66\u4e60\u548c\u638c\u63e1NumPy\u7684\u57fa\u672c\u64cd\u4f5c\u548c\u9ad8\u7ea7\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u8fdb\u884c\u77e9\u9635\u521b\u5efa\u3001\u8bbf\u95ee\u3001\u8d4b\u503c\u3001\u8fd0\u7b97\u3001\u8f6c\u7f6e\u3001\u6c42\u9006\u3001\u884c\u5217\u5f0f\u3001\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\u7b49\u64cd\u4f5c\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408Pandas\u548cSciPy\u5e93\u8fdb\u884c\u66f4\u590d\u6742\u7684\u77e9\u9635\u5904\u7406\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u77e9\u9635\u5904\u7406\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u5206\u6790\u3001\u673a\u5668\u5b66\u4e60\u3001\u56fe\u50cf\u5904\u7406\u3001\u7269\u7406\u548c\u5de5\u7a0b\u8ba1\u7b97\u7b49\u9886\u57df\u3002\u901a\u8fc7\u4f18\u5316\u77e9\u9635\u8fd0\u7b97\uff0c\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\uff0c\u89e3\u51b3\u5927\u89c4\u6a21\u6570\u636e\u5904\u7406\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u521b\u5efa\u77e9\u9635\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4<code>pip install numpy<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u521b\u5efa\u77e9\u9635\u7684\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528<code>numpy.array()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a2&#215;2\u7684\u77e9\u9635\uff1a  <\/p>\n<pre><code class=\"language-python\">import numpy as np\nmatrix = np.array([[1, 2], [3, 4]])\nprint(matrix)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\u4e00\u4e2a\u77e9\u9635\uff1a  <\/p>\n<pre><code>[[1 2]\n [3 4]]\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5bf9\u77e9\u9635\u8fdb\u884c\u57fa\u672c\u8fd0\u7b97\uff1f<\/strong><br \/>\u4f7f\u7528NumPy\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u77e9\u9635\u7684\u57fa\u672c\u8fd0\u7b97\uff0c\u5982\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u8f6c\u7f6e\u3002\u4ee5\u77e9\u9635\u52a0\u6cd5\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528<code>+<\/code>\u8fd0\u7b97\u7b26\u8fdb\u884c\u4e24\u4e2a\u76f8\u540c\u5f62\u72b6\u77e9\u9635\u7684\u76f8\u52a0\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix1 = np.array([[1, 2], [3, 4]])\nmatrix2 = np.array([[5, 6], [7, 8]])\nresult = matrix1 + matrix2\nprint(result)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\uff1a  <\/p>\n<pre><code>[[ 6  8]\n [10 12]]\n<\/code><\/pre>\n<p>\u7c7b\u4f3c\u5730\uff0c\u5176\u4ed6\u8fd0\u7b97\u5982\u51cf\u6cd5\u3001\u4e58\u6cd5\u548c\u8f6c\u7f6e\u90fd\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684\u8fd0\u7b97\u7b26\u6216\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u5982\u4f55\u5bf9\u77e9\u9635\u8fdb\u884c\u9ad8\u7ea7\u64cd\u4f5c\uff0c\u5982\u6c42\u9006\u6216\u7279\u5f81\u503c\uff1f<\/strong><br \/>\u5728NumPy\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.linalg<\/code>\u6a21\u5757\u8fdb\u884c\u9ad8\u7ea7\u77e9\u9635\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c\u8981\u6c42\u4e00\u4e2a\u77e9\u9635\u7684\u9006\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.linalg.inv()<\/code>\u51fd\u6570\u3002\u5bf9\u4e8e\u7279\u5f81\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.linalg.eig()<\/code>\u51fd\u6570\u3002\u4ee5\u4e0b\u662f\u6c42\u9006\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">matrix = np.array([[1, 2], [3, 4]])\ninverse_matrix = np.linalg.inv(matrix)\nprint(inverse_matrix)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u5c06\u662f\u77e9\u9635\u7684\u9006\u3002\u5bf9\u4e8e\u7279\u5f81\u503c\u7684\u8ba1\u7b97\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">eigenvalues, eigenvectors = np.linalg.eig(matrix)\nprint(&quot;\u7279\u5f81\u503c:&quot;, eigenvalues)\nprint(&quot;\u7279\u5f81\u5411\u91cf:&quot;, eigenvectors)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u663e\u793a\u77e9\u9635\u7684\u7279\u5f81\u503c\u548c\u5bf9\u5e94\u7684\u7279\u5f81\u5411\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5bf9\u77e9\u9635\u8fdb\u884c\u5904\u7406\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u8fdb\u884c\u64cd\u4f5c\u3001\u4f7f\u7528pandas\u5e93\u8fdb\u884c\u6570\u636e\u64cd\u4f5c\u3001 [&hellip;]","protected":false},"author":3,"featured_media":1106296,"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\/1106293"}],"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=1106293"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106293\/revisions"}],"predecessor-version":[{"id":1106299,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106293\/revisions\/1106299"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1106296"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1106293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1106293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1106293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}