{"id":1089695,"date":"2025-01-08T13:55:48","date_gmt":"2025-01-08T05:55:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1089695.html"},"modified":"2025-01-08T13:55:50","modified_gmt":"2025-01-08T05:55:50","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%95%b0%e7%bb%84%e6%94%be%e5%88%b0%e7%9f%a9%e9%98%b5%e4%b8%ad-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1089695.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24202510\/79df793e-8f35-4c34-ac56-4bbec141b3e8.webp\" alt=\"python\u5982\u4f55\u5c06\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u5c06\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6bd4\u5982\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5217\u8868\u5d4c\u5957\u3001\u4f7f\u7528Pandas\u5e93\u7b49\u3002\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\u3001\u4f7f\u7528\u5217\u8868\u5d4c\u5957\u521b\u5efa\u77e9\u9635\u3001\u4f7f\u7528Pandas\u5e93\u521b\u5efaDataFrame\u7b49\u3002<\/strong> \u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u53ca\u5176\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u7684\u652f\u6301\u3002NumPy\u5e93\u4e2d\u7684<code>numpy.array<\/code>\u51fd\u6570\u53ef\u4ee5\u5c06\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\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\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5c06\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = [1, 2, 3]<\/p>\n<p>array2 = [4, 5, 6]<\/p>\n<p>array3 = [7, 8, 9]<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d<\/strong><\/h2>\n<p>matrix = np.array([array1, array2, array3])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1 2 3]<\/p>\n<p> [4 5 6]<\/p>\n<p> [7 8 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e09\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u5e76\u5c06\u5b83\u4eec\u653e\u5230\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff08\u77e9\u9635\uff09\u4e2d\u3002NumPy\u7684<code>array<\/code>\u51fd\u6570\u4f1a\u81ea\u52a8\u5c06\u8fd9\u4e9b\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5217\u8868\u5d4c\u5957\u521b\u5efa\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5217\u8868\u662f\u4e00\u79cd\u975e\u5e38\u7075\u6d3b\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u5305\u542b\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u5d4c\u5957\u7684\u65b9\u5f0f\u521b\u5efa\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u5d4c\u5957\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5c06\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u7ef4\u6570\u7ec4<\/p>\n<p>array1 = [1, 2, 3]<\/p>\n<p>array2 = [4, 5, 6]<\/p>\n<p>array3 = [7, 8, 9]<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d<\/strong><\/h2>\n<p>matrix = [array1, array2, array3]<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1, 2, 3], [4, 5, 6], [7, 8, 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e09\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u5e76\u5c06\u5b83\u4eec\u653e\u5230\u4e00\u4e2a\u5d4c\u5957\u5217\u8868\u4e2d\u3002\u5d4c\u5957\u5217\u8868\u662f\u4e00\u79cd\u7b80\u5355\u4f46\u6709\u6548\u7684\u65b9\u5f0f\u6765\u8868\u793a\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Pandas\u5e93\u521b\u5efaDataFrame<\/h3>\n<\/p>\n<p><p>Pandas\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u8868\u683c\u6570\u636e\u7684\u652f\u6301\u3002Pandas\u5e93\u4e2d\u7684<code>DataFrame<\/code>\u5bf9\u8c61\u53ef\u4ee5\u7528\u4e8e\u5c06\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>\u5b89\u88c5Pandas\u5e93<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Pandas\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efaDataFrame<\/h4>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5c06\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = [1, 2, 3]<\/p>\n<p>array2 = [4, 5, 6]<\/p>\n<p>array3 = [7, 8, 9]<\/p>\n<h2><strong>\u5c06\u4e00\u7ef4\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d<\/strong><\/h2>\n<p>matrix = pd.DataFrame([array1, array2, array3])<\/p>\n<p>print(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>   0  1  2<\/p>\n<p>0  1  2  3<\/p>\n<p>1  4  5  6<\/p>\n<p>2  7  8  9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e09\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u5e76\u5c06\u5b83\u4eec\u653e\u5230\u4e00\u4e2aDataFrame\u4e2d\u3002Pandas\u7684<code>DataFrame<\/code>\u5bf9\u8c61\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001NumPy\u521b\u5efa\u7279\u6b8a\u77e9\u9635<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u57fa\u672c\u7684\u4e8c\u7ef4\u6570\u7ec4\u521b\u5efa\uff0cNumPy\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u6765\u521b\u5efa\u7279\u6b8a\u77e9\u9635\uff0c\u6bd4\u5982\u5168\u96f6\u77e9\u9635\u3001\u5168\u4e00\u77e9\u9635\u3001\u5355\u4f4d\u77e9\u9635\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u521b\u5efa\u5168\u96f6\u77e9\u9635<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a3x3\u7684\u5168\u96f6\u77e9\u9635<\/strong><\/h2>\n<p>zero_matrix = np.zeros((3, 3))<\/p>\n<p>print(zero_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[0. 0. 0.]<\/p>\n<p> [0. 0. 0.]<\/p>\n<p> [0. 0. 0.]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u5168\u4e00\u77e9\u9635<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a3x3\u7684\u5168\u4e00\u77e9\u9635<\/p>\n<p>one_matrix = np.ones((3, 3))<\/p>\n<p>print(one_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1. 1. 1.]<\/p>\n<p> [1. 1. 1.]<\/p>\n<p> [1. 1. 1.]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u521b\u5efa\u5355\u4f4d\u77e9\u9635<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u4e00\u4e2a3x3\u7684\u5355\u4f4d\u77e9\u9635<\/p>\n<p>identity_matrix = np.eye(3)<\/p>\n<p>print(identity_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\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><p>\u8fd9\u4e9b\u7279\u6b8a\u77e9\u9635\u5728\u79d1\u5b66\u8ba1\u7b97\u548c\u7ebf\u6027\u4ee3\u6570\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u77e9\u9635\u7684\u57fa\u672c\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u521b\u5efa\u4e86\u77e9\u9635\u4e4b\u540e\uff0c\u6211\u4eec\u901a\u5e38\u8fd8\u9700\u8981\u5bf9\u77e9\u9635\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u77e9\u9635\u52a0\u6cd5\u3001\u77e9\u9635\u4e58\u6cd5\u3001\u77e9\u9635\u8f6c\u7f6e\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u77e9\u9635\u52a0\u6cd5<\/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>matrix1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>matrix2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])<\/p>\n<h2><strong>\u77e9\u9635\u52a0\u6cd5<\/strong><\/h2>\n<p>result = matrix1 + matrix2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[10 10 10]<\/p>\n<p> [10 10 10]<\/p>\n<p> [10 10 10]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u77e9\u9635\u4e58\u6cd5<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u4e58\u6cd5<\/p>\n<p>result = np.dot(matrix1, matrix2)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[ 30  24  18]<\/p>\n<p> [ 84  69  54]<\/p>\n<p> [138 114  90]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u77e9\u9635\u8f6c\u7f6e<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u8f6c\u7f6e<\/p>\n<p>transpose_matrix = np.transpose(matrix1)<\/p>\n<p>print(transpose_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[1 4 7]<\/p>\n<p> [2 5 8]<\/p>\n<p> [3 6 9]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e9b\u57fa\u672c\u64cd\u4f5c\u662f\u77e9\u9635\u8ba1\u7b97\u4e2d\u7684\u5e38\u89c1\u9700\u6c42\uff0cNumPy\u63d0\u4f9b\u4e86\u7b80\u6d01\u800c\u9ad8\u6548\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u77e9\u9635\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5bf9\u77e9\u9635\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u6c42\u9006\u77e9\u9635\u3001\u77e9\u9635\u5206\u89e3\u7b49\u3002NumPy\u548c\u5176\u4ed6\u79d1\u5b66\u8ba1\u7b97\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u51fd\u6570\u6765\u5b9e\u73b0\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u6c42\u9006\u77e9\u9635<\/h4>\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([[1, 2], [3, 4]])<\/p>\n<h2><strong>\u6c42\u9006\u77e9\u9635<\/strong><\/h2>\n<p>inverse_matrix = np.linalg.inv(matrix)<\/p>\n<p>print(inverse_matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>[[-2.   1. ]<\/p>\n<p> [ 1.5 -0.5]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u77e9\u9635\u5206\u89e3<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u5206\u89e3\uff08\u4f8b\u5982LU\u5206\u89e3\uff09<\/p>\n<p>from scipy.linalg import lu<\/p>\n<p>P, L, U = lu(matrix)<\/p>\n<p>print(&quot;P:\\n&quot;, P)<\/p>\n<p>print(&quot;L:\\n&quot;, L)<\/p>\n<p>print(&quot;U:\\n&quot;, U)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<\/p>\n<p><pre><code>P:<\/p>\n<p>[[0. 1.]<\/p>\n<p> [1. 0.]]<\/p>\n<p>L:<\/p>\n<p>[[1.         0.        ]<\/p>\n<p> [0.33333333 1.        ]]<\/p>\n<p>U:<\/p>\n<p>[[3.         4.        ]<\/p>\n<p> [0.         0.66666667]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u77e9\u9635\u7684\u9ad8\u7ea7\u64cd\u4f5c\u5728\u79d1\u5b66\u8ba1\u7b97\u3001\u5de5\u7a0b\u5e94\u7528\u548c\u6570\u636e\u5206\u6790\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5e94\u7528\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u5982\u4f55\u5c06\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d\u5e76\u8fdb\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u5b9e\u4f8b\u6765\u5c55\u793a\u8fd9\u4e9b\u77e5\u8bc6\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>\u5b9e\u4f8b\uff1a\u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u56fe\u50cf\u901a\u5e38\u88ab\u8868\u793a\u4e3a\u77e9\u9635\u3002\u6bcf\u4e2a\u50cf\u7d20\u7684\u989c\u8272\u503c\u5bf9\u5e94\u77e9\u9635\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u77e9\u9635\u64cd\u4f5c\u6765\u5bf9\u56fe\u50cf\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u7070\u5ea6\u56fe\u50cf\uff08\u77e9\u9635\uff09<\/strong><\/h2>\n<p>image = np.array([[0, 50, 100],<\/p>\n<p>                  [150, 200, 250],<\/p>\n<p>                  [255, 200, 150]])<\/p>\n<h2><strong>\u663e\u793a\u539f\u59cb\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(image, cmap=&#39;gray&#39;)<\/p>\n<p>plt.title(&#39;Original Image&#39;)<\/p>\n<p>plt.show()<\/p>\n<h2><strong>\u56fe\u50cf\u53cd\u8f6c\uff08\u77e9\u9635\u64cd\u4f5c\uff09<\/strong><\/h2>\n<p>inverted_image = 255 - image<\/p>\n<h2><strong>\u663e\u793a\u53cd\u8f6c\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>plt.imshow(inverted_image, cmap=&#39;gray&#39;)<\/p>\n<p>plt.title(&#39;Inverted Image&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u7070\u5ea6\u56fe\u50cf\uff08\u77e9\u9635\uff09\uff0c\u7136\u540e\u901a\u8fc7\u77e9\u9635\u64cd\u4f5c\u5bf9\u56fe\u50cf\u8fdb\u884c\u4e86\u53cd\u8f6c\u5904\u7406\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528Matplotlib\u5e93\u5c06\u539f\u59cb\u56fe\u50cf\u548c\u53cd\u8f6c\u540e\u7684\u56fe\u50cf\u8fdb\u884c\u4e86\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528NumPy\u5e93\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\u3001\u4f7f\u7528\u5217\u8868\u5d4c\u5957\u521b\u5efa\u77e9\u9635\u3001\u4f7f\u7528Pandas\u5e93\u521b\u5efaDataFrame\u7b49\u3002NumPy\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002\u901a\u8fc7\u5b66\u4e60\u8fd9\u4e9b\u65b9\u6cd5\u548c\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u77e9\u9635\u8ba1\u7b97\u95ee\u9898\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u77e9\u9635\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u77e9\u9635\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86NumPy\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>numpy.reshape()<\/code>\u51fd\u6570\u6216\u8005<code>numpy.array()<\/code>\u51fd\u6570\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u6240\u9700\u5f62\u72b6\u7684\u77e9\u9635\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<code>arr = [1, 2, 3, 4, 5, 6]<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528<code>arr.reshape(2, 3)<\/code>\u6765\u521b\u5efa\u4e00\u4e2a2\u884c3\u5217\u7684\u77e9\u9635\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u521b\u5efa\u548c\u64cd\u4f5c\u77e9\u9635\uff1f<\/strong><br \/>\u9664\u4e86NumPy\u4e4b\u5916\uff0cSciPy\u548cPandas\u4e5f\u662f\u5e38\u7528\u7684\u5e93\u3002SciPy\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u7ea7\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\uff0c\u800cPandas\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u4e8c\u7ef4\u6570\u636e\u7ed3\u6784\uff08\u5982DataFrame\uff09\uff0c\u9002\u5408\u4e8e\u6570\u636e\u5206\u6790\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\uff0c\u53ef\u4ee5\u63d0\u5347\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u77e9\u9635\u4e2d\u7684\u7f3a\u5931\u6570\u636e\uff1f<\/strong><br \/>\u5904\u7406\u7f3a\u5931\u6570\u636e\u662f\u6570\u636e\u5206\u6790\u4e2d\u7684\u5e38\u89c1\u4efb\u52a1\u3002\u5728NumPy\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>numpy.nan<\/code>\u6765\u8868\u793a\u7f3a\u5931\u503c\uff0c\u5e76\u901a\u8fc7<code>numpy.nanmean()<\/code>\u7b49\u51fd\u6570\u6765\u8ba1\u7b97\u77e9\u9635\u7684\u5747\u503c\uff0c\u5ffd\u7565\u7f3a\u5931\u503c\u3002Pandas\u5e93\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u65b9\u6cd5\uff0c\u6bd4\u5982<code>fillna()<\/code>\u53ef\u4ee5\u7528\u6765\u586b\u5145\u7f3a\u5931\u503c\uff0c<code>dropna()<\/code>\u53ef\u4ee5\u5220\u9664\u5305\u542b\u7f3a\u5931\u6570\u636e\u7684\u884c\u6216\u5217\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u5e94\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u5c06\u6570\u7ec4\u653e\u5230\u77e9\u9635\u4e2d\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6bd4\u5982\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528\u5217\u8868\u5d4c\u5957\u3001\u4f7f\u7528Pandas\u5e93\u7b49\u3002\u6700\u5e38 [&hellip;]","protected":false},"author":3,"featured_media":1089702,"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\/1089695"}],"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=1089695"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089695\/revisions"}],"predecessor-version":[{"id":1089704,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1089695\/revisions\/1089704"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1089702"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1089695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1089695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1089695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}