{"id":1126620,"date":"2025-01-08T20:02:57","date_gmt":"2025-01-08T12:02:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1126620.html"},"modified":"2025-01-08T20:03:00","modified_gmt":"2025-01-08T12:03:00","slug":"python%e5%a6%82%e4%bd%95%e7%bb%99%e7%9f%a9%e9%98%b5%e9%99%a4%e4%bb%a5%e4%b8%80%e4%b8%aa%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1126620.html","title":{"rendered":"python\u5982\u4f55\u7ed9\u77e9\u9635\u9664\u4ee5\u4e00\u4e2a\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090858\/33647ebe-0020-41b9-a577-4b8c5ffa7687.webp\" alt=\"python\u5982\u4f55\u7ed9\u77e9\u9635\u9664\u4ee5\u4e00\u4e2a\u6570\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u7ed9\u77e9\u9635\u9664\u4ee5\u4e00\u4e2a\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002NumPy\u4e2d\u7684\u6570\u7ec4\uff08ndarray\uff09\u7c7b\u578b\u652f\u6301\u76f4\u63a5\u7684\u7b97\u672f\u8fd0\u7b97\u3002<\/strong> \u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 NumPy \u7684\u6570\u7ec4\u9664\u6cd5\u529f\u80fd\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\uff1a\u9996\u5148\uff0c\u5bfc\u5165 NumPy \u5e93\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\uff0c\u6700\u540e\u4f7f\u7528\u9664\u6cd5\u8fd0\u7b97\u7b26 <code>\/<\/code> \u5bf9\u77e9\u9635\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u9664\u6cd5\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u8be6\u7ec6\u5730\u8bb2\u89e3\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u77e9\u9635\u9664\u6cd5\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762\uff1a<strong>NumPy\u5e93\u7684\u57fa\u7840\u77e5\u8bc6\u3001\u77e9\u9635\u7684\u521b\u5efa\u3001\u77e9\u9635\u8fd0\u7b97\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/strong>\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001NUMPY\u5e93\u57fa\u7840\u77e5\u8bc6<\/h3>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u652f\u6301\u5927\u91cf\u7ef4\u5ea6\u6570\u7ec4\u4e0e\u77e9\u9635\u8fd0\u7b97\u7684\u80fd\u529b\uff0c\u540c\u65f6\u4e5f\u5305\u542b\u4e86\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\u5e93\u3002NumPy\u7684\u6838\u5fc3\u662fndarray\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5c01\u88c5\u4e86\u540c\u7c7b\u578b\u5143\u7d20\u7684n\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><h4>1.1 \u5b89\u88c5NumPy<\/h4>\n<\/p>\n<p><p>\u5728\u4f7f\u7528NumPy\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5176\u5df2\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\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>1.2 \u5bfc\u5165NumPy<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u4f7f\u7528NumPy\u4e4b\u524d\uff0c\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u5bfc\u5165\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u77e9\u9635\u7684\u521b\u5efa<\/h3>\n<\/p>\n<p><p>\u5728NumPy\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u521b\u5efa\u77e9\u9635\uff08\u6570\u7ec4\uff09\uff0c\u5305\u62ec\u4ece\u73b0\u6709\u6570\u636e\u521b\u5efa\u3001\u4f7f\u7528\u7279\u5b9a\u51fd\u6570\u751f\u6210\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2.1 \u4ece\u5217\u8868\u521b\u5efa\u77e9\u9635<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u76f4\u63a5\u4ecePython\u7684\u5217\u8868\u521b\u5efaNumPy\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2 \u4f7f\u7528\u7279\u5b9a\u51fd\u6570\u521b\u5efa\u77e9\u9635<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u591a\u4e2a\u51fd\u6570\u6765\u751f\u6210\u7279\u5b9a\u7c7b\u578b\u7684\u77e9\u9635\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>zeros<\/strong>: \u521b\u5efa\u4e00\u4e2a\u5168\u96f6\u77e9\u9635<\/li>\n<li><strong>ones<\/strong>: \u521b\u5efa\u4e00\u4e2a\u5168\u4e00\u77e9\u9635<\/li>\n<li><strong>eye<\/strong>: \u521b\u5efa\u4e00\u4e2a\u5355\u4f4d\u77e9\u9635<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\">zeros_matrix = np.zeros((3, 3))<\/p>\n<p>ones_matrix = np.ones((3, 3))<\/p>\n<p>identity_matrix = np.eye(3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u77e9\u9635\u8fd0\u7b97<\/h3>\n<\/p>\n<p><h4>3.1 \u57fa\u7840\u7b97\u672f\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>NumPy\u6570\u7ec4\u652f\u6301\u57fa\u672c\u7684\u7b97\u672f\u8fd0\u7b97\uff0c\u8fd9\u5305\u62ec\u52a0\u3001\u51cf\u3001\u4e58\u3001\u9664\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u76f4\u63a5\u5bf9\u77e9\u9635\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u9664\u6cd5\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>divisor = 3<\/p>\n<p>result = matrix \/ divisor<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>result<\/code>\u5c06\u662f\u4e00\u4e2a\u4e0e<code>matrix<\/code>\u540c\u5f62\u72b6\u7684\u77e9\u9635\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u539f\u77e9\u9635\u4e2d\u76f8\u5e94\u5143\u7d20\u9664\u4ee5<code>divisor<\/code>\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u5e7f\u64ad\u673a\u5236<\/h4>\n<\/p>\n<p><p>NumPy\u7684\u5e7f\u64ad\u673a\u5236\u5141\u8bb8\u5bf9\u5f62\u72b6\u4e0d\u540c\u7684\u6570\u7ec4\u8fdb\u884c\u7b97\u672f\u8fd0\u7b97\u3002\u8fd9\u5728\u7ed9\u77e9\u9635\u9664\u4ee5\u4e00\u4e2a\u6570\u65f6\u7279\u522b\u6709\u7528\uff0c\u56e0\u4e3a\u6807\u91cf\u4f1a\u81ea\u52a8\u201c\u5e7f\u64ad\u201d\u5230\u4e0e\u77e9\u9635\u540c\u6837\u7684\u5f62\u72b6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>divisor = 3<\/p>\n<p>result = matrix \/ divisor<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c<code>divisor<\/code>\u662f\u4e00\u4e2a\u6807\u91cf\uff0c\u4f46NumPy\u4f1a\u5c06\u5176\u5e7f\u64ad\u4e3a\u4e0e<code>matrix<\/code>\u540c\u6837\u5927\u5c0f\u7684\u6570\u7ec4\uff0c\u7136\u540e\u8fdb\u884c\u9010\u5143\u7d20\u9664\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>4.1 \u6570\u636e\u5f52\u4e00\u5316<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5f52\u4e00\u5316\u5904\u7406\u3002\u5f52\u4e00\u5316\u7684\u4e00\u4e2a\u5e38\u89c1\u65b9\u6cd5\u662f\u5c06\u6570\u636e\u7684\u6bcf\u4e2a\u503c\u9664\u4ee5\u8be5\u6570\u636e\u96c6\u7684\u6700\u5927\u503c\u6216\u67d0\u4e2a\u7279\u5b9a\u7684\u6570\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]])<\/p>\n<p>normalized_data = data \/ np.max(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>normalized_data<\/code>\u5c06\u662f\u4e00\u4e2a\u5f52\u4e00\u5316\u540e\u7684\u77e9\u9635\uff0c\u5176\u4e2d\u6bcf\u4e2a\u503c\u90fd\u57280\u52301\u4e4b\u95f4\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u56fe\u50cf\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\uff0c\u56fe\u50cf\u901a\u5e38\u8868\u793a\u4e3a\u77e9\u9635\u3002\u4e3a\u4e86\u8c03\u6574\u56fe\u50cf\u7684\u4eae\u5ea6\uff0c\u53ef\u4ee5\u5c06\u5176\u6bcf\u4e2a\u50cf\u7d20\u503c\u9664\u4ee5\u4e00\u4e2a\u5e38\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">image = np.array([[255, 128, 64], [32, 16, 8], [4, 2, 1]])<\/p>\n<p>adjusted_image = image \/ 2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\u7684<code>adjusted_image<\/code>\u77e9\u9635\u8868\u793a\u8c03\u6574\u4eae\u5ea6\u540e\u7684\u56fe\u50cf\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8fdb\u9636\u5185\u5bb9<\/h3>\n<\/p>\n<p><h4>5.1 \u4f7f\u7528NumPy\u7684\u66f4\u591a\u77e9\u9635\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>NumPy\u4e0d\u4ec5\u53ef\u4ee5\u8fdb\u884c\u57fa\u672c\u7684\u77e9\u9635\u8fd0\u7b97\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f8b\u5982\u77e9\u9635\u7684\u8f6c\u7f6e\u3001\u9006\u77e9\u9635\u8ba1\u7b97\u3001\u884c\u5217\u5f0f\u8ba1\u7b97\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">matrix = np.array([[1, 2], [3, 4]])<\/p>\n<p>transpose_matrix = np.transpose(matrix)<\/p>\n<p>inverse_matrix = np.linalg.inv(matrix)<\/p>\n<p>determinant = np.linalg.det(matrix)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2 \u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<p><p>NumPy\u7684\u77e9\u9635\u8fd0\u7b97\u5728\u6027\u80fd\u4e0a\u975e\u5e38\u9ad8\u6548\uff0c\u56e0\u4e3a\u5b83\u4eec\u90fd\u662f\u7528C\u8bed\u8a00\u5b9e\u73b0\u7684\u3002\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u4f7f\u7528NumPy\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528NumPy\u5e93\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5bf9\u77e9\u9635\u8fdb\u884c\u9664\u6cd5\u64cd\u4f5c\u3002\u901a\u8fc7\u5b66\u4e60NumPy\u7684\u57fa\u672c\u7528\u6cd5\u3001\u77e9\u9635\u7684\u521b\u5efa\u65b9\u6cd5\u548c\u77e9\u9635\u8fd0\u7b97\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u7684\u77e9\u9635\u8ba1\u7b97\u3002\u8fd9\u4e0d\u4ec5\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\u6709\u5e7f\u6cdb\u5e94\u7528\uff0c\u8fd8\u5728\u6570\u636e\u5904\u7406\u3001\u673a\u5668\u5b66\u4e60\u548c\u56fe\u50cf\u5904\u7406\u7b49\u9886\u57df\u4e2d\u53d1\u6325\u91cd\u8981\u4f5c\u7528\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6709\u6548\u5730\u5bf9\u77e9\u9635\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u6765\u8f7b\u677e\u5730\u5bf9\u77e9\u9635\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86NumPy\u5e93\u3002\u7136\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u77e9\u9635\u5e76\u4f7f\u7528\u9664\u6cd5\u8fd0\u7b97\u7b26\u201c\/\u201d\u5c06\u5176\u9664\u4ee5\u4e00\u4e2a\u6570\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\nmatrix = np.array([[1, 2, 3], [4, 5, 6]])\nresult = matrix \/ 2\nprint(result)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u77e9\u9635\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u88ab2\u9664\u4ee5\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u77e9\u9635\u9664\u6cd5\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u77e9\u9635\u9664\u6cd5\u65f6\uff0c\u786e\u4fdd\u4f60\u9664\u7684\u6570\u5b57\u4e0d\u4e3a\u96f6\uff0c\u56e0\u4e3a\u8fd9\u5c06\u5bfc\u81f4\u8ba1\u7b97\u9519\u8bef\u6216\u629b\u51fa\u5f02\u5e38\u3002\u6b64\u5916\uff0c\u4e86\u89e3NumPy\u7684\u5e7f\u64ad\u673a\u5236\u975e\u5e38\u91cd\u8981\uff0c\u8fd9\u6837\u5728\u5904\u7406\u4e0d\u540c\u5f62\u72b6\u7684\u77e9\u9635\u65f6\uff0c\u53ef\u4ee5\u907f\u514d\u4e00\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u7528\u5176\u4ed6\u5e93\u6765\u7ed9\u77e9\u9635\u9664\u4ee5\u4e00\u4e2a\u6570\uff1f<\/strong><br \/>\u9664\u4e86NumPy\uff0c\u5176\u4ed6\u5e93\u5982Pandas\u548cTensorFlow\u4e5f\u652f\u6301\u77e9\u9635\u7684\u9664\u6cd5\u8fd0\u7b97\u3002\u4f7f\u7528Pandas\u65f6\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aDataFrame\u5e76\u76f4\u63a5\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\uff0c\u800c\u5728TensorFlow\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528tf.divide()\u51fd\u6570\u6765\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\u3002\u8fd9\u4e9b\u5e93\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u5408\u9002\u7684\u5e93\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\u548c\u4f7f\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u7ed9\u77e9\u9635\u9664\u4ee5\u4e00\u4e2a\u6570\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u77e9\u9635\u64cd\u4f5c\u529f\u80fd\u3002NumPy\u4e2d\u7684 [&hellip;]","protected":false},"author":3,"featured_media":1126627,"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\/1126620"}],"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=1126620"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126620\/revisions"}],"predecessor-version":[{"id":1126630,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126620\/revisions\/1126630"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1126627"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1126620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1126620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1126620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}