{"id":1126463,"date":"2025-01-08T20:01:28","date_gmt":"2025-01-08T12:01:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1126463.html"},"modified":"2025-01-08T20:01:31","modified_gmt":"2025-01-08T12:01:31","slug":"python%e5%a6%82%e4%bd%95%e4%b8%8b%e8%bd%bd%e7%ac%ac%e4%b8%89%e6%96%b9%e5%ba%93numpy","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1126463.html","title":{"rendered":"python\u5982\u4f55\u4e0b\u8f7d\u7b2c\u4e09\u65b9\u5e93numpy"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25090740\/83ae9558-b7af-46f3-892d-c8826ac904eb.webp\" alt=\"python\u5982\u4f55\u4e0b\u8f7d\u7b2c\u4e09\u65b9\u5e93numpy\" \/><\/p>\n<p><p> <strong>\u8981\u4e0b\u8f7dPython\u7684\u7b2c\u4e09\u65b9\u5e93NumPy\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u3002<\/strong> \u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u548cpip\uff1b\u5176\u6b21\uff0c\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165<code>pip install numpy<\/code>\u5e76\u56de\u8f66\uff0c\u8fd9\u5c06\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5NumPy\u5e93\u3002<strong>\u786e\u4fdd\u7f51\u7edc\u8fde\u63a5\u7a33\u5b9a<\/strong>\uff0c<strong>\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f<\/strong>\u662f\u4e24\u4e2a\u91cd\u8981\u6b65\u9aa4\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u8fd9\u4e9b\u6b65\u9aa4\u548c\u4e00\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<hr>\n<h2><strong>\u4e00\u3001\u786e\u4fdd\u5df2\u5b89\u88c5Python\u548cpip<\/strong><\/h2>\n<p><p>\u5728\u4e0b\u8f7dNumPy\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u4f60\u7684\u8ba1\u7b97\u673a\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u548cpip\u3002\u8fd9\u662f\u56e0\u4e3apip\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u8d1f\u8d23\u4e0b\u8f7d\u548c\u7ba1\u7406Python\u5e93\u3002<\/p>\n<\/p>\n<p><h3>1.1 \u68c0\u67e5Python\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>\u8981\u68c0\u67e5\u662f\u5426\u5b89\u88c5\u4e86Python\uff0c\u4f60\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679cPython\u5df2\u5b89\u88c5\uff0c\u4f60\u5c06\u770b\u5230\u4e00\u4e2a\u7c7b\u4f3c\u4e8e<code>Python 3.8.5<\/code>\u7684\u7248\u672c\u53f7\u3002\u5426\u5219\uff0c\u4f60\u9700\u8981\u4ecePython\u7684\u5b98\u65b9\u7f51\u7ad9\uff08<a href=\"https:\/\/www.python.org\/\">python.org<\/a>\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\u3002<\/p>\n<\/p>\n<p><h3>1.2 \u68c0\u67e5pip\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5pip\u662f\u5426\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip --version<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679cpip\u5df2\u5b89\u88c5\uff0c\u4f60\u5c06\u770b\u5230\u4e00\u4e2a\u7c7b\u4f3c\u4e8e<code>pip 20.1.1 from ...<\/code>\u7684\u8f93\u51fa\u3002\u5982\u679cpip\u672a\u5b89\u88c5\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u5b83\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m ensurepip --upgrade<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528pip\u5b89\u88c5NumPy<\/strong><\/h2>\n<p><h3>2.1 \u57fa\u672c\u5b89\u88c5\u547d\u4ee4<\/h3>\n<\/p>\n<p><p>\u786e\u4fddPython\u548cpip\u5df2\u7ecf\u6b63\u786e\u5b89\u88c5\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5NumPy\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\u4f1a\u81ea\u52a8\u4ecePython\u7684\u5b98\u65b9\u5305\u7ba1\u7406\u4ed3\u5e93PyPI\u4e2d\u4e0b\u8f7d\u5e76\u5b89\u88c5NumPy\u5e93\u3002\u5982\u679c\u5b89\u88c5\u6210\u529f\uff0c\u4f60\u4f1a\u770b\u5230\u4e00\u7cfb\u5217\u7684\u4e0b\u8f7d\u548c\u5b89\u88c5\u8fdb\u5ea6\u4fe1\u606f\uff0c\u6700\u540e\u663e\u793a<code>Successfully installed numpy<\/code>\u7684\u63d0\u793a\u3002<\/p>\n<\/p>\n<p><h3>2.2 \u6307\u5b9a\u7248\u672c\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u4f60\u53ef\u80fd\u9700\u8981\u5b89\u88c5\u7279\u5b9a\u7248\u672c\u7684NumPy\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy==1.19.2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5c06<code>1.19.2<\/code>\u66ff\u6362\u4e3a\u4f60\u9700\u8981\u7684\u7248\u672c\u53f7\u3002<\/p>\n<\/p>\n<p><h3>2.3 \u5b89\u88c5\u5230\u7279\u5b9a\u8def\u5f84<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5c06NumPy\u5b89\u88c5\u5230\u7279\u5b9a\u7684\u8def\u5f84\uff0c\u53ef\u4ee5\u4f7f\u7528<code>--target<\/code>\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy --target=\/path\/to\/your\/directory<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u9a8c\u8bc1\u5b89\u88c5<\/strong><\/h2>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6700\u597d\u9a8c\u8bc1\u4e00\u4e0bNumPy\u662f\u5426\u5b89\u88c5\u6210\u529f\u4ee5\u53ca\u662f\u5426\u80fd\u591f\u6b63\u5e38\u4f7f\u7528\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u9a8c\u8bc1\uff1a<\/p>\n<\/p>\n<p><h3>3.1 \u5728Python\u4ea4\u4e92\u6a21\u5f0f\u4e2d\u5bfc\u5165NumPy<\/h3>\n<\/p>\n<p><p>\u6253\u5f00Python\u4ea4\u4e92\u6a21\u5f0f\uff08\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165<code>python<\/code>\u6216<code>python3<\/code>\uff09\uff0c\u7136\u540e\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>print(np.__version__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679cNumPy\u5b89\u88c5\u6210\u529f\u4e14\u80fd\u6b63\u5e38\u4f7f\u7528\uff0c\u4f60\u5c06\u770b\u5230NumPy\u7684\u7248\u672c\u53f7\uff0c\u5982<code>1.19.2<\/code>\u3002<\/p>\n<\/p>\n<p><h3>3.2 \u8fd0\u884c\u7b80\u5355\u7684NumPy\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u8fdb\u4e00\u6b65\u9a8c\u8bc1\uff0c\u4f60\u53ef\u4ee5\u8fd0\u884c\u4e00\u4e9b\u7b80\u5355\u7684NumPy\u4ee3\u7801\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>a = np.array([1, 2, 3])<\/p>\n<p>print(a)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u8f93\u51fa\u4e0e\u9884\u671f\u4e00\u81f4\uff0c\u8bf4\u660eNumPy\u5b89\u88c5\u6210\u529f\u4e14\u80fd\u591f\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u89e3\u51b3\u5e38\u89c1\u95ee\u9898<\/strong><\/h2>\n<p><p>\u5c3d\u7ba1\u5b89\u88c5\u8fc7\u7a0b\u76f8\u5bf9\u7b80\u5355\uff0c\u4f46\u6709\u65f6\u4e5f\u4f1a\u9047\u5230\u4e00\u4e9b\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>4.1 \u7f51\u7edc\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u7531\u4e8e\u7f51\u7edc\u95ee\u9898\uff0cpip\u4e0b\u8f7d\u901f\u5ea6\u53ef\u80fd\u4f1a\u5f88\u6162\uff0c\u751a\u81f3\u5931\u8d25\u3002\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4f7f\u7528\u56fd\u5185\u7684\u955c\u50cf\u6e90\u6765\u52a0\u901f\u4e0b\u8f7d\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u963f\u91cc\u4e91\u7684\u955c\u50cf\u6e90\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy -i https:\/\/mirrors.aliyun.com\/pypi\/simple\/<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.2 \u6743\u9650\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u9047\u5230\u6743\u9650\u95ee\u9898\uff0c\u5bfc\u81f4pip\u5b89\u88c5\u5931\u8d25\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>--user<\/code>\u53c2\u6570\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy --user<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.3 \u517c\u5bb9\u6027\u95ee\u9898<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0cNumPy\u7684\u67d0\u4e2a\u7248\u672c\u53ef\u80fd\u4e0e\u5f53\u524d\u7684Python\u7248\u672c\u4e0d\u517c\u5bb9\uff0c\u8fd9\u65f6\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u5b89\u88c5\u4e0d\u540c\u7248\u672c\u7684NumPy\u6216\u5347\u7ea7\u4f60\u7684Python\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h3>4.4 \u66f4\u65b0pip<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\uff0c\u65e7\u7248\u672c\u7684pip\u53ef\u80fd\u65e0\u6cd5\u6b63\u786e\u5b89\u88c5\u67d0\u4e9b\u5e93\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5347\u7ea7pip\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install --upgrade pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>4.5 \u865a\u62df\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u907f\u514d\u4e0d\u540c\u9879\u76ee\u4e4b\u95f4\u7684\u5305\u4f9d\u8d56\u51b2\u7a81\uff0c\u5efa\u8bae\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u7ba1\u7406Python\u5305\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u521b\u5efa\u548c\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python -m venv myenv<\/p>\n<p>source myenv\/bin\/activate  # Linux\u548cMacOS<\/p>\n<p>myenv\\Scripts\\activate  # Windows<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5NumPy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001NumPy\u57fa\u672c\u7528\u6cd5\u7b80\u4ecb<\/strong><\/h2>\n<p><p>\u5728\u6210\u529f\u5b89\u88c5\u5e76\u9a8c\u8bc1NumPy\u540e\uff0c\u4f60\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528\u8fd9\u4e2a\u5f3a\u5927\u7684\u5e93\u6765\u8fdb\u884c\u5404\u79cd\u79d1\u5b66\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9bNumPy\u7684\u57fa\u672c\u7528\u6cd5\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<p><h3>5.1 \u521b\u5efa\u6570\u7ec4<\/h3>\n<\/p>\n<p><p>NumPy\u6700\u57fa\u672c\u7684\u529f\u80fd\u662f\u521b\u5efa\u6570\u7ec4\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7<code>np.array<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u7ef4\u3001\u4e8c\u7ef4\u751a\u81f3\u591a\u7ef4\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>a = np.array([1, 2, 3])<\/p>\n<p>b = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.2 \u6570\u7ec4\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>NumPy\u652f\u6301\u591a\u79cd\u6570\u7ec4\u8fd0\u7b97\uff0c\u5982\u52a0\u6cd5\u3001\u51cf\u6cd5\u3001\u4e58\u6cd5\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = np.array([1, 2, 3])<\/p>\n<p>b = np.array([4, 5, 6])<\/p>\n<p>print(a + b)  # [5 7 9]<\/p>\n<p>print(a * b)  # [4 10 18]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.3 \u6570\u5b66\u51fd\u6570<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u6570\u5b66\u51fd\u6570\uff0c\u5982\u6c42\u548c\u3001\u5e73\u5747\u503c\u3001\u6807\u51c6\u5dee\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = np.array([1, 2, 3])<\/p>\n<p>print(np.sum(a))  # 6<\/p>\n<p>print(np.mean(a))  # 2.0<\/p>\n<p>print(np.std(a))  # 0.816496580927726<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.4 \u77e9\u9635\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>NumPy\u8fd8\u652f\u6301\u77e9\u9635\u8fd0\u7b97\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8fdb\u884c\u7ebf\u6027\u4ee3\u6570\u8ba1\u7b97\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = np.array([[1, 2], [3, 4]])<\/p>\n<p>b = np.array([[5, 6], [7, 8]])<\/p>\n<p>print(np.dot(a, b))<\/p>\n<h2><strong>[[19 22]<\/strong><\/h2>\n<h2><strong> [43 50]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>5.5 \u968f\u673a\u6570\u751f\u6210<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u968f\u673a\u6570\u751f\u6210\u529f\u80fd\uff0c\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u5206\u5e03\u7684\u968f\u673a\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">random_array = np.random.rand(3, 3)  # \u751f\u62103x3\u7684\u968f\u673a\u6570\u7ec4<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516d\u3001NumPy\u9ad8\u7ea7\u7528\u6cd5<\/strong><\/h2>\n<p><p>\u5728\u638c\u63e1\u4e86\u57fa\u672c\u7528\u6cd5\u4e4b\u540e\uff0c\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63a2\u7d22NumPy\u7684\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>6.1 \u5e7f\u64ad\u673a\u5236<\/h3>\n<\/p>\n<p><p>NumPy\u7684\u5e7f\u64ad\u673a\u5236\u53ef\u4ee5\u8ba9\u4e0d\u540c\u5f62\u72b6\u7684\u6570\u7ec4\u8fdb\u884c\u8fd0\u7b97\uff0c\u8fd9\u662f\u5176\u5f3a\u5927\u4e4b\u5904\u4e4b\u4e00\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = np.array([1, 2, 3])<\/p>\n<p>b = np.array([[4], [5], [6]])<\/p>\n<p>print(a + b)<\/p>\n<h2><strong>[[5 6 7]<\/strong><\/h2>\n<h2><strong> [6 7 8]<\/strong><\/h2>\n<h2><strong> [7 8 9]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.2 \u9ad8\u7ea7\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7d22\u5f15\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5c\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = np.array([1, 2, 3, 4, 5])<\/p>\n<p>print(a[1:4])  # [2 3 4]<\/p>\n<p>print(a[a &gt; 3])  # [4 5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.3 \u6570\u7ec4\u53d8\u5f62<\/h3>\n<\/p>\n<p><p>NumPy\u5141\u8bb8\u4f60\u8f7b\u677e\u5730\u53d8\u5f62\u6570\u7ec4\uff0c\u8fd9\u662f\u5904\u7406\u591a\u7ef4\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p>print(a.reshape(3, 2))<\/p>\n<h2><strong>[[1 2]<\/strong><\/h2>\n<h2><strong> [3 4]<\/strong><\/h2>\n<h2><strong> [5 6]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>6.4 \u5408\u5e76\u4e0e\u5206\u5272<\/h3>\n<\/p>\n<p><p>NumPy\u8fd8\u652f\u6301\u6570\u7ec4\u7684\u5408\u5e76\u4e0e\u5206\u5272\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = np.array([[1, 2], [3, 4]])<\/p>\n<p>b = np.array([[5, 6], [7, 8]])<\/p>\n<p>print(np.vstack((a, b)))<\/p>\n<h2><strong>[[1 2]<\/strong><\/h2>\n<h2><strong> [3 4]<\/strong><\/h2>\n<h2><strong> [5 6]<\/strong><\/h2>\n<h2><strong> [7 8]]<\/strong><\/h2>\n<p>print(np.hstack((a, b)))<\/p>\n<h2><strong>[[1 2 5 6]<\/strong><\/h2>\n<h2><strong> [3 4 7 8]]<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e03\u3001NumPy\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u7684\u5e94\u7528<\/strong><\/h2>\n<p><p>NumPy\u662f\u6570\u636e\u79d1\u5b66\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><h3>7.1 \u6570\u636e\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u9879\u76ee\u4e2d\uff0c\u6570\u636e\u9884\u5904\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u73af\u3002NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u6765\u5904\u7406\u548c\u6e05\u6d17\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7f3a\u5931\u503c\u5904\u7406<\/p>\n<p>data = np.array([1, 2, np.nan, 4])<\/p>\n<p>cleaned_data = data[~np.isnan(data)]<\/p>\n<p>print(cleaned_data)  # [1. 2. 4.]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7.2 \u6570\u636e\u5206\u6790<\/h3>\n<\/p>\n<p><p>NumPy\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u5feb\u901f\u7684\u6570\u636e\u5206\u6790\u548c\u63a2\u7d22\u6027\u6570\u636e\u5206\u6790\uff08EDA\uff09\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = np.array([1, 2, 3, 4, 5])<\/p>\n<p>print(np.mean(data))  # 3.0<\/p>\n<p>print(np.median(data))  # 3.0<\/p>\n<p>print(np.var(data))  # 2.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>7.3 <a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/h3>\n<\/p>\n<p><p>NumPy\u5728\u673a\u5668\u5b66\u4e60\u4e2d\u4e5f\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u5f88\u591a\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u90fd\u662f\u57fa\u4e8eNumPy\u5b9e\u73b0\u7684\uff0c\u4f8b\u5982\u7ebf\u6027\u56de\u5f52\u3001\u903b\u8f91\u56de\u5f52\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u7ebf\u6027\u56de\u5f52\u793a\u4f8b<\/p>\n<p>X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])<\/p>\n<p>y = np.dot(X, np.array([1, 2])) + 3<\/p>\n<p>print(y)  # [ 6  8  9 11]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516b\u3001NumPy\u4e0e\u5176\u4ed6\u5e93\u7684\u96c6\u6210<\/strong><\/h2>\n<p><p>NumPy\u4e0d\u4ec5\u81ea\u8eab\u529f\u80fd\u5f3a\u5927\uff0c\u8fd8\u80fd\u5f88\u597d\u5730\u4e0e\u5176\u4ed6\u79d1\u5b66\u8ba1\u7b97\u5e93\u96c6\u6210\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h3>8.1 \u4e0ePandas\u96c6\u6210<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u57fa\u4e8eNumPy\u7684\u5f3a\u5927\u6570\u636e\u5206\u6790\u5e93\u3002\u4f60\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5c06NumPy\u6570\u7ec4\u8f6c\u5316\u4e3aPandas\u6570\u636e\u6846\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>a = np.array([[1, 2], [3, 4]])<\/p>\n<p>df = pd.DataFrame(a, columns=[&#39;A&#39;, &#39;B&#39;])<\/p>\n<p>print(df)<\/p>\n<h2><strong>   A  B<\/strong><\/h2>\n<h2><strong>0  1  2<\/strong><\/h2>\n<h2><strong>1  3  4<\/strong><\/h2>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8.2 \u4e0eMatplotlib\u96c6\u6210<\/h3>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528NumPy\u751f\u6210\u6570\u636e\u5e76\u901a\u8fc7Matplotlib\u8fdb\u884c\u53ef\u89c6\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<p>plt.plot(x, y)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>8.3 \u4e0eSciPy\u96c6\u6210<\/h3>\n<\/p>\n<p><p>SciPy\u662f\u4e00\u4e2a\u57fa\u4e8eNumPy\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u7ea7\u7684\u6570\u5b66\u3001\u79d1\u5b66\u548c\u5de5\u7a0b\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy import optimize<\/p>\n<h2><strong>\u4f7f\u7528SciPy\u8fdb\u884c\u4f18\u5316<\/strong><\/h2>\n<p>def f(x):<\/p>\n<p>    return x2 + 10*np.sin(x)<\/p>\n<p>result = optimize.minimize(f, 0)<\/p>\n<p>print(result.x)  # \u4f18\u5316\u7ed3\u679c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e5d\u3001NumPy\u6027\u80fd\u4f18\u5316<\/strong><\/h2>\n<p><p>\u5c3d\u7ba1NumPy\u5df2\u7ecf\u975e\u5e38\u9ad8\u6548\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u6027\u80fd\u4f18\u5316\u4ecd\u7136\u5f88\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>9.1 \u77e2\u91cf\u5316\u8fd0\u7b97<\/h3>\n<\/p>\n<p><p>\u5c3d\u91cf\u4f7f\u7528NumPy\u7684\u77e2\u91cf\u5316\u8fd0\u7b97\uff0c\u800c\u4e0d\u662fPython\u7684\u5faa\u73af\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6027\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = np.array([1, 2, 3, 4, 5])<\/p>\n<p>b = np.array([6, 7, 8, 9, 10])<\/p>\n<h2><strong>\u77e2\u91cf\u5316\u8fd0\u7b97<\/strong><\/h2>\n<p>c = a * b<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>9.2 \u4f7f\u7528\u9ad8\u7ea7\u7d22\u5f15<\/h3>\n<\/p>\n<p><p>NumPy\u7684\u9ad8\u7ea7\u7d22\u5f15\u529f\u80fd\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6570\u636e\u64cd\u4f5c\u7684\u6548\u7387\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = np.random.rand(1000, 1000)<\/p>\n<h2><strong>\u4f7f\u7528\u9ad8\u7ea7\u7d22\u5f15<\/strong><\/h2>\n<p>b = a[a &gt; 0.5]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>9.3 \u5185\u5b58\u7ba1\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u5185\u5b58\u7ba1\u7406\u975e\u5e38\u91cd\u8981\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528NumPy\u7684\u5185\u5b58\u6620\u5c04\u529f\u80fd\u6765\u5904\u7406\u5927\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">a = np.memmap(&#39;data.dat&#39;, dtype=&#39;float32&#39;, mode=&#39;r&#39;, shape=(1000, 1000))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u3001NumPy\u672a\u6765\u53d1\u5c55<\/strong><\/h2>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u793e\u533a\u975e\u5e38\u6d3b\u8dc3\u3002\u672a\u6765\uff0cNumPy\u5c06\u7ee7\u7eed\u53d1\u5c55\uff0c\u63d0\u4f9b\u66f4\u591a\u5f3a\u5927\u548c\u9ad8\u6548\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>10.1 \u793e\u533a\u8d21\u732e<\/h3>\n<\/p>\n<p><p>NumPy\u7684\u5f00\u53d1\u548c\u7ef4\u62a4\u79bb\u4e0d\u5f00\u793e\u533a\u7684\u8d21\u732e\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7GitHub\u53c2\u4e0eNumPy\u7684\u5f00\u53d1\uff0c\u4e3a\u5176\u8d21\u732e\u4ee3\u7801\u6216\u6587\u6863\u3002<\/p>\n<\/p>\n<p><h3>10.2 \u65b0\u7279\u6027<\/h3>\n<\/p>\n<p><p>NumPy\u793e\u533a\u4e0d\u65ad\u5f15\u5165\u65b0\u7279\u6027\u548c\u4f18\u5316\uff0c\u4ee5\u6ee1\u8db3\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u7684\u9700\u6c42\u3002\u4f8b\u5982\uff0cNumPy\u6b63\u5728\u5f15\u5165\u66f4\u591a\u9ad8\u7ea7\u7684\u7ebf\u6027\u4ee3\u6570\u548c\u7edf\u8ba1\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>10.3 \u4e0e\u65b0\u6280\u672f\u7684\u96c6\u6210<\/h3>\n<\/p>\n<p><p>\u968f\u7740\u65b0\u6280\u672f\u7684\u53d1\u5c55\uff0cNumPy\u4e5f\u5728\u4e0d\u65ad\u4e0e\u4e4b\u96c6\u6210\u3002\u4f8b\u5982\uff0cNumPy\u6b63\u5728\u4e0eDask\u7b49\u5206\u5e03\u5f0f\u8ba1\u7b97\u6846\u67b6\u96c6\u6210\uff0c\u4ee5\u652f\u6301\u5927\u89c4\u6a21\u6570\u636e\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<hr>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8be6\u7ec6\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u5982\u4f55\u4e0b\u8f7d\u3001\u5b89\u88c5\u548c\u4f7f\u7528NumPy\u3002NumPy\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5e93\uff0c\u4e0d\u4ec5\u5728\u79d1\u5b66\u8ba1\u7b97\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u8fd8\u80fd\u4e0e\u5176\u4ed6\u6570\u636e\u79d1\u5b66\u548c\u673a\u5668\u5b66\u4e60\u5e93\u65e0\u7f1d\u96c6\u6210\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528NumPy\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b89\u88c5NumPy\u5e93\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u5b89\u88c5NumPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177pip\u3002\u5728\u547d\u4ee4\u884c\u754c\u9762\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<code>pip install numpy<\/code>\uff0c\u7136\u540e\u6309\u4e0b\u56de\u8f66\u952e\u3002\u7cfb\u7edf\u4f1a\u81ea\u52a8\u4e0b\u8f7d\u5e76\u5b89\u88c5NumPy\u5e93\u53ca\u5176\u4f9d\u8d56\u9879\u3002\u786e\u4fdd\u4f60\u7684Python\u73af\u5883\u5df2\u7ecf\u914d\u7f6e\u597d\uff0c\u5e76\u4e14pip\u662f\u6700\u65b0\u7248\u672c\uff0c\u4ee5\u907f\u514d\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<p><strong>\u5728\u5b89\u88c5NumPy\u4e4b\u524d\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u5b89\u88c5NumPy\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684Python\u7248\u672c\u517c\u5bb9NumPy\u3002\u901a\u5e38\uff0cNumPy\u652f\u6301Python 3.6\u53ca\u66f4\u9ad8\u7248\u672c\u3002\u6b64\u5916\uff0c\u68c0\u67e5\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u662f\u5426\u5177\u5907\u5fc5\u8981\u7684\u7f16\u8bd1\u5de5\u5177\uff0c\u4ee5\u4fbf\u987a\u5229\u5b89\u88c5\u3002\u5982\u679c\u4f7f\u7528\u865a\u62df\u73af\u5883\uff0c\u786e\u4fdd\u5df2\u7ecf\u6fc0\u6d3b\u76f8\u5e94\u7684\u73af\u5883\uff0c\u4ee5\u4fbf\u5728\u6b63\u786e\u7684\u4f4d\u7f6e\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<p><strong>\u5982\u4f55\u9a8c\u8bc1NumPy\u662f\u5426\u6210\u529f\u5b89\u88c5\uff1f<\/strong><br \/>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728Python\u4ea4\u4e92\u5f0f\u73af\u5883\u4e2d\u8f93\u5165<code>import numpy<\/code>\u6765\u9a8c\u8bc1NumPy\u662f\u5426\u6210\u529f\u5b89\u88c5\u3002\u5982\u679c\u6ca1\u6709\u4efb\u4f55\u9519\u8bef\u63d0\u793a\uff0c\u8bf4\u660eNumPy\u5df2\u6b63\u786e\u5b89\u88c5\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u901a\u8fc7<code>print(numpy.__version__)<\/code>\u547d\u4ee4\u67e5\u770b\u5f53\u524d\u5b89\u88c5\u7684NumPy\u7248\u672c\uff0c\u786e\u4fdd\u5176\u7b26\u5408\u4f60\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4e0b\u8f7dPython\u7684\u7b2c\u4e09\u65b9\u5e93NumPy\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5305\u7ba1\u7406\u5de5\u5177pip\u3002 \u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2 [&hellip;]","protected":false},"author":3,"featured_media":1126473,"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\/1126463"}],"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=1126463"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126463\/revisions"}],"predecessor-version":[{"id":1126477,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1126463\/revisions\/1126477"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1126473"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1126463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1126463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1126463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}