{"id":929293,"date":"2024-12-26T16:52:55","date_gmt":"2024-12-26T08:52:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/929293.html"},"modified":"2024-12-26T16:52:57","modified_gmt":"2024-12-26T08:52:57","slug":"python-%e5%a6%82%e4%bd%95%e5%8a%a0%e8%bd%bdnum","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/929293.html","title":{"rendered":"python \u5982\u4f55\u52a0\u8f7dnum"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25064556\/e1caed59-486a-4ee8-88fd-ec1d01c2b580.webp\" alt=\"python \u5982\u4f55\u52a0\u8f7dnum\" \/><\/p>\n<p><p> <strong>Python\u52a0\u8f7dNumPy\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528<code>import<\/code>\u8bed\u53e5\u5bfc\u5165NumPy\u5e93\u3001\u901a\u8fc7<code>np.array()<\/code>\u521b\u5efa\u6570\u7ec4\u3001\u5229\u7528NumPy\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u548c\u6570\u636e\u5904\u7406\u3002\u63a8\u8350\u4f7f\u7528<code>import numpy as np<\/code>\u8fdb\u884c\u7b80\u5316\u3001\u63d0\u5347\u4ee3\u7801\u53ef\u8bfb\u6027\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b89\u88c5\u548c\u4f7f\u7528NumPy\u5e93\u3002<\/strong><\/p>\n<\/p>\n<p><p>NumPy\u662fPython\u4e2d\u4e00\u4e2a\u91cd\u8981\u7684\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u3002\u5b83\u4e3aPython\u63d0\u4f9b\u4e86\u5bf9\u9ad8\u6027\u80fd\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u7684\u652f\u6301\uff0c\u4ee5\u53ca\u5bf9\u8fd9\u4e9b\u6570\u7ec4\u8fdb\u884c\u5feb\u901f\u64cd\u4f5c\u7684\u51fd\u6570\u3002\u4e3a\u4e86\u5728Python\u4e2d\u4f7f\u7528NumPy\uff0c\u4f60\u9700\u8981\u9996\u5148\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\uff0c\u7136\u540e\u901a\u8fc7import\u8bed\u53e5\u5c06\u5176\u52a0\u8f7d\u5230\u4f60\u7684\u811a\u672c\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u5b89\u88c5NumPy<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528NumPy\u4e4b\u524d\uff0c\u9700\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u5982\u679c\u4f60\u7684Python\u73af\u5883\u4e2d\u8fd8\u6ca1\u6709NumPy\uff0c\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><p>\u8fd9\u4e2a\u547d\u4ee4\u5c06\u4ecePython\u7684\u5305\u7ba1\u7406\u7cfb\u7edf\uff08PyPI\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5NumPy\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5bfc\u5165NumPy<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u811a\u672c\u4e2d\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u6765\u52a0\u8f7dNumPy\u3002\u901a\u5e38\uff0c\u6211\u4eec\u4f1a\u4f7f\u7528<code>as<\/code>\u5173\u952e\u5b57\u4e3aNumPy\u6307\u5b9a\u4e00\u4e2a\u522b\u540d\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u4e66\u5199\uff0c\u5e76\u63d0\u9ad8\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u521b\u5efaNumPy\u6570\u7ec4<\/p>\n<\/p>\n<p><p>NumPy\u7684\u6838\u5fc3\u662f\u5176\u5f3a\u5927\u7684N\u7ef4\u6570\u7ec4\u5bf9\u8c61<code>ndarray<\/code>\u3002\u53ef\u4ee5\u901a\u8fc7<code>np.array()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u6570\u7ec4\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>array_1d = np.array([1, 2, 3, 4])<\/p>\n<h2><strong>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_2d = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>NumPy\u6570\u7ec4\u4e0ePython\u5217\u8868\u7c7b\u4f3c\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\u548c\u66f4\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001NumPy\u7684\u57fa\u672c\u64cd\u4f5c<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u7ec4\u8fd0\u7b97<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u5141\u8bb8\u5bf9\u6570\u7ec4\u6267\u884c\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\uff0c\u8fd9\u4e9b\u8fd0\u7b97\u4f1a\u81ea\u52a8\u5e94\u7528\u5230\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\u3002\u8fd9\u4f7f\u5f97\u5904\u7406\u5927\u91cf\u6570\u636e\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 2, 3, 4])<\/p>\n<h2><strong>\u52a0\u6cd5<\/strong><\/h2>\n<p>result_add = array + 1<\/p>\n<h2><strong>\u4e58\u6cd5<\/strong><\/h2>\n<p>result_mul = array * 2<\/p>\n<h2><strong>\u6c42\u548c<\/strong><\/h2>\n<p>sum_result = np.sum(array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6570\u7ec4\u7684\u5f62\u72b6\u548c\u7ef4\u5ea6<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u6570\u7ec4\u7684\u5f62\u72b6\u53ef\u4ee5\u901a\u8fc7<code>shape<\/code>\u5c5e\u6027\u83b7\u53d6\uff0c\u7ef4\u5ea6\u53ef\u4ee5\u901a\u8fc7<code>ndim<\/code>\u5c5e\u6027\u83b7\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u7ec4\u7684\u5f62\u72b6<\/strong><\/h2>\n<p>shape = array.shape<\/p>\n<h2><strong>\u83b7\u53d6\u6570\u7ec4\u7684\u7ef4\u5ea6<\/strong><\/h2>\n<p>dimension = array.ndim<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6570\u7ec4\u7d22\u5f15\u548c\u5207\u7247<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u6570\u7ec4\u652f\u6301\u591a\u79cd\u7d22\u5f15\u548c\u5207\u7247\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u548c\u4fee\u6539\u6570\u7ec4\u7684\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u7d22\u5f15<\/strong><\/h2>\n<p>element = array[2]  # \u8bbf\u95ee\u7b2c\u4e09\u4e2a\u5143\u7d20<\/p>\n<h2><strong>\u5207\u7247<\/strong><\/h2>\n<p>subset = array[1:4]  # \u8bbf\u95ee\u7b2c\u4e8c\u5230\u7b2c\u56db\u4e2a\u5143\u7d20<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001NumPy\u7684\u9ad8\u7ea7\u529f\u80fd<\/p>\n<\/p>\n<ol>\n<li><strong>\u5e7f\u64ad<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u652f\u6301\u4e00\u79cd\u79f0\u4e3a\u5e7f\u64ad\u7684\u673a\u5236\uff0c\u5141\u8bb8\u5bf9\u4e0d\u540c\u5f62\u72b6\u7684\u6570\u7ec4\u6267\u884c\u7b97\u672f\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>array1 = np.array([1, 2, 3])<\/p>\n<p>array2 = np.array([[1], [2], [3]])<\/p>\n<h2><strong>\u5e7f\u64ad\u673a\u5236<\/strong><\/h2>\n<p>result = array1 + array2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u7ebf\u6027\u4ee3\u6570<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ebf\u6027\u4ee3\u6570\u529f\u80fd\uff0c\u5305\u62ec\u77e9\u9635\u4e58\u6cd5\u3001\u884c\u5217\u5f0f\u8ba1\u7b97\u3001\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u77e9\u9635<\/strong><\/h2>\n<p>matrix1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>matrix2 = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>product = np.dot(matrix1, matrix2)<\/p>\n<h2><strong>\u8ba1\u7b97\u884c\u5217\u5f0f<\/strong><\/h2>\n<p>determinant = np.linalg.det(matrix1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u968f\u673a\u6570\u751f\u6210<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u7684\u5b50\u6a21\u5757<code>numpy.random<\/code>\u63d0\u4f9b\u4e86\u751f\u6210\u968f\u673a\u6570\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_int = np.random.randint(0, 10, size=5)<\/p>\n<h2><strong>\u751f\u6210\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_float = np.random.random(size=5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u6570\u7ec4\u7684\u91cd\u5851\u548c\u5408\u5e76<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u5141\u8bb8\u5bf9\u6570\u7ec4\u8fdb\u884c\u91cd\u5851\u548c\u5408\u5e76\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u91cd\u5851\u6570\u7ec4<\/strong><\/h2>\n<p>array = np.array([1, 2, 3, 4, 5, 6])<\/p>\n<p>reshaped_array = array.reshape(2, 3)<\/p>\n<h2><strong>\u5408\u5e76\u6570\u7ec4<\/strong><\/h2>\n<p>array1 = np.array([1, 2])<\/p>\n<p>array2 = np.array([3, 4])<\/p>\n<p>concatenated_array = np.concatenate((array1, array2))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001NumPy\u7684\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<ol>\n<li><strong>\u6570\u636e\u5206\u6790<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u662f\u8bb8\u591a\u6570\u636e\u5206\u6790\u5e93\uff08\u5982Pandas\uff09\u7684\u57fa\u7840\uff0c\u5728\u6570\u636e\u6e05\u6d17\u3001\u7edf\u8ba1\u5206\u6790\u7b49\u65b9\u9762\u6709\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u79d1\u5b66\u8ba1\u7b97<\/strong><\/li>\n<\/ol>\n<p><p>NumPy\u5e7f\u6cdb\u5e94\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u9886\u57df\uff0c\u5305\u62ec\u7269\u7406\u3001\u5316\u5b66\u3001\u751f\u7269\u7b49\u7814\u7a76\u4e2d\u7684\u6570\u503c\u6a21\u62df\u548c\u6570\u636e\u5904\u7406\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li><strong><a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0cNumPy\u5e38\u7528\u4e8e\u6570\u636e\u9884\u5904\u7406\u3001\u7279\u5f81\u63d0\u53d6\u7b49\u6b65\u9aa4\u3002\u8bb8\u591a\u673a\u5668\u5b66\u4e60\u5e93\uff08\u5982TensorFlow\u3001PyTorch\uff09\u90fd\u4f9d\u8d56\u4e8eNumPy\u8fdb\u884c\u5e95\u5c42\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u4f60\u5e94\u8be5\u5bf9\u5982\u4f55\u5728Python\u4e2d\u52a0\u8f7d\u548c\u4f7f\u7528NumPy\u6709\u4e86\u5168\u9762\u7684\u4e86\u89e3\u3002NumPy\u7684\u5f3a\u5927\u529f\u80fd\u548c\u7075\u6d3b\u6027\u4f7f\u5176\u6210\u4e3aPython\u79d1\u5b66\u8ba1\u7b97\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u638c\u63e1NumPy\u7684\u4f7f\u7528\u65b9\u6cd5\u5c06\u4e3a\u4f60\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u5de5\u4f5c\u5e26\u6765\u6781\u5927\u4fbf\u5229\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u52a0\u8f7dNumPy\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u52a0\u8f7dNumPy\u5e93\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5NumPy\uff1a<code>pip install numpy<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u60a8\u7684Python\u811a\u672c\u6216\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\uff0c\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u52a0\u8f7dNumPy\uff1a<code>import numpy as np<\/code>\u3002\u8fd9\u5c06\u4f7f\u60a8\u80fd\u591f\u4f7f\u7528NumPy\u7684\u6240\u6709\u529f\u80fd\u3002<\/p>\n<p><strong>NumPy\u5e93\u7684\u4e3b\u8981\u529f\u80fd\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>NumPy\u5e93\u662fPython\u4e2d\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u5927\u578b\u3001\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u6548\u7684\u64cd\u4f5c\uff0c\u4f8b\u5982\u6570\u7ec4\u7684\u7b97\u672f\u8fd0\u7b97\u3001\u7ebf\u6027\u4ee3\u6570\u3001\u5085\u91cc\u53f6\u53d8\u6362\u7b49\u3002\u6b64\u5916\uff0cNumPy\u8fd8\u5177\u6709\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528NumPy\u65f6\uff0c\u5982\u4f55\u786e\u4fdd\u6570\u636e\u7684\u6b63\u786e\u6027\u548c\u6709\u6548\u6027\uff1f<\/strong><br \/>\u5728\u4f7f\u7528NumPy\u65f6\uff0c\u786e\u4fdd\u6570\u636e\u7684\u6b63\u786e\u6027\u548c\u6709\u6548\u6027\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u505a\u5230\u8fd9\u4e00\u70b9\uff1a\u9996\u5148\uff0c\u4f7f\u7528NumPy\u63d0\u4f9b\u7684\u51fd\u6570\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u68c0\u67e5\uff0c\u786e\u4fdd\u8f93\u5165\u6570\u636e\u7b26\u5408\u9884\u671f\u683c\u5f0f\uff1b\u5176\u6b21\uff0c\u5229\u7528NumPy\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u6355\u6349\u548c\u5904\u7406\u6f5c\u5728\u7684\u9519\u8bef\uff1b\u6700\u540e\uff0c\u7f16\u5199\u5355\u5143\u6d4b\u8bd5\u4ee5\u9a8c\u8bc1NumPy\u64cd\u4f5c\u7684\u8f93\u51fa\uff0c\u786e\u4fdd\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u52a0\u8f7dNumPy\u7684\u65b9\u6cd5\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165NumPy\u5e93\u3001\u901a\u8fc7np.array()\u521b\u5efa\u6570 [&hellip;]","protected":false},"author":3,"featured_media":929295,"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\/929293"}],"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=929293"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/929293\/revisions"}],"predecessor-version":[{"id":929296,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/929293\/revisions\/929296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/929295"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=929293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=929293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=929293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}