{"id":1155220,"date":"2025-01-13T18:00:42","date_gmt":"2025-01-13T10:00:42","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1155220.html"},"modified":"2025-01-13T18:00:45","modified_gmt":"2025-01-13T10:00:45","slug":"%e5%a6%82%e4%bd%95%e7%bb%99python%e8%a3%85numpy","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1155220.html","title":{"rendered":"\u5982\u4f55\u7ed9python\u88c5numpy"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25184649\/d2cfd10a-316a-47c7-ac8c-f778c34fc4b1.webp\" alt=\"\u5982\u4f55\u7ed9python\u88c5numpy\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u7ed9Python\u88c5numpy\uff1a\u4f7f\u7528pip\u5b89\u88c5\u3001\u4f7f\u7528conda\u5b89\u88c5\u3001\u4e0b\u8f7d\u9884\u7f16\u8bd1\u5305\u3001\u4ece\u6e90\u7801\u7f16\u8bd1<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u4f7f\u7528pip\u5b89\u88c5<\/strong>\u662f\u6700\u5e38\u89c1\u548c\u65b9\u4fbf\u7684\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528pip\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>pip\u662fPython\u7684\u5305\u7ba1\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5b89\u88c5\u5404\u79cdPython\u5e93\u3002\u5b89\u88c5numpy\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u3002<\/li>\n<li>\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5e76\u56de\u8f66\uff1a\n<pre><code>pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1a\n<pre><code class=\"language-python\">import numpy as np<\/p>\n<p>print(np.__version__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p>\u5982\u679c\u6ca1\u6709\u62a5\u9519\uff0c\u5e76\u4e14\u8f93\u51fa\u4e86numpy\u7684\u7248\u672c\u53f7\uff0c\u5219\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/li>\n<\/p>\n<\/ol>\n<p><p>\u4f7f\u7528pip\u5b89\u88c5numpy\u7684\u4f18\u70b9\u662f\u64cd\u4f5c\u7b80\u5355\uff0c\u53ea\u9700\u8981\u4e00\u6761\u547d\u4ee4\u5c31\u53ef\u4ee5\u5b8c\u6210\u5b89\u88c5\uff0c\u5e76\u4e14pip\u4f1a\u81ea\u52a8\u5904\u7406\u4f9d\u8d56\u5173\u7cfb\u3002\u5982\u679c\u4f60\u7684Python\u73af\u5883\u4e2d\u6ca1\u6709pip\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5pip\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python -m ensurepip --default-pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528conda\u5b89\u88c5<\/h3>\n<\/p>\n<p><p>Anaconda\u662f\u4e00\u6b3e\u975e\u5e38\u6d41\u884c\u7684Python\u6570\u636e\u79d1\u5b66\u5e73\u53f0\uff0c\u5305\u542b\u4e86\u5f88\u591a\u5e38\u7528\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\u3002\u4f7f\u7528Anaconda\u81ea\u5e26\u7684\u5305\u7ba1\u7406\u5de5\u5177conda\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5b89\u88c5numpy\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00Anaconda Prompt\u6216\u547d\u4ee4\u884c\u3002<\/li>\n<li>\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5e76\u56de\u8f66\uff1a\n<pre><code>conda install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1a\n<pre><code class=\"language-python\">import numpy as np<\/p>\n<p>print(np.__version__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p>\u5982\u679c\u6ca1\u6709\u62a5\u9519\uff0c\u5e76\u4e14\u8f93\u51fa\u4e86numpy\u7684\u7248\u672c\u53f7\uff0c\u5219\u8bf4\u660e\u5b89\u88c5\u6210\u529f\u3002<\/li>\n<\/p>\n<\/ol>\n<p><p>\u4f7f\u7528conda\u5b89\u88c5numpy\u7684\u4f18\u70b9\u662fAnaconda\u5e73\u53f0\u5305\u542b\u4e86\u5f88\u591a\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u79d1\u5b66\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7684\u5f00\u53d1\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4e0b\u8f7d\u9884\u7f16\u8bd1\u5305<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u7531\u4e8e\u7f51\u7edc\u539f\u56e0\u65e0\u6cd5\u4f7f\u7528pip\u6216conda\u5b89\u88c5numpy\uff0c\u53ef\u4ee5\u9009\u62e9\u4e0b\u8f7dnumpy\u7684\u9884\u7f16\u8bd1\u5305\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00<a href=\"https:\/\/pypi.org\/project\/numpy\/#files\">https:\/\/pypi.org\/project\/numpy\/#files<\/a>\u3002<\/li>\n<li>\u6839\u636e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u548cPython\u7248\u672c\u4e0b\u8f7d\u5bf9\u5e94\u7684.whl\u6587\u4ef6\u3002<\/li>\n<li>\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u8fdb\u5165\u4e0b\u8f7d\u76ee\u5f55\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5e76\u56de\u8f66\uff1a\n<pre><code>pip install numpy-&lt;version&gt;-&lt;platform&gt;.whl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1a\n<pre><code class=\"language-python\">import numpy as np<\/p>\n<p>print(np.__version__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e0b\u8f7d\u9884\u7f16\u8bd1\u5305\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u5728\u6ca1\u6709\u7f51\u7edc\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u5b89\u88c5\uff0c\u4f46\u9700\u8981\u624b\u52a8\u9009\u62e9\u548c\u4e0b\u8f7d\u9002\u5408\u81ea\u5df1\u73af\u5883\u7684\u5305\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4ece\u6e90\u7801\u7f16\u8bd1<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u5b89\u88c5numpy\u7684\u6700\u65b0\u5f00\u53d1\u7248\u672c\u6216\u4fee\u6539numpy\u7684\u6e90\u7801\uff0c\u53ef\u4ee5\u9009\u62e9\u4ece\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u5e76\u56de\u8f66\uff1a\n<pre><code>git clone https:\/\/github.com\/numpy\/numpy.git<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u8fdb\u5165numpy\u76ee\u5f55\uff1a\n<pre><code>cd numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5b89\u88c5\u6784\u5efa\u5de5\u5177\uff1a\n<pre><code>pip install cython<\/p>\n<p>pip install wheel<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u6784\u5efa\u548c\u5b89\u88c5numpy\uff1a\n<pre><code>python setup.py build<\/p>\n<p>python setup.py install<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u9a8c\u8bc1\u5b89\u88c5\u662f\u5426\u6210\u529f\uff1a\n<pre><code class=\"language-python\">import numpy as np<\/p>\n<p>print(np.__version__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4ece\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5\u7684\u4f18\u70b9\u662f\u53ef\u4ee5\u83b7\u53d6\u6700\u65b0\u7684\u5f00\u53d1\u7248\u672c\uff0c\u5e76\u4e14\u53ef\u4ee5\u5bf9\u6e90\u7801\u8fdb\u884c\u4fee\u6539\uff0c\u4f46\u9700\u8981\u5b89\u88c5\u6784\u5efa\u5de5\u5177\u5e76\u4e14\u7f16\u8bd1\u65f6\u95f4\u8f83\u957f\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u89e3\u51b3<\/h3>\n<\/p>\n<p><p><strong>1. \u5b89\u88c5\u5931\u8d25<\/strong><\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u7531\u4e8e\u7f51\u7edc\u95ee\u9898\u6216\u4f9d\u8d56\u95ee\u9898\uff0c\u5b89\u88c5numpy\u53ef\u80fd\u4f1a\u5931\u8d25\u3002\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u4e0b\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528\u56fd\u5185\u955c\u50cf\u6e90\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u66f4\u65b0pip\u7248\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code>python -m pip install --upgrade pip<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p>\u68c0\u67e5Python\u7248\u672c\u548cpip\u7248\u672c\u662f\u5426\u5339\u914d\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><p><strong>2. \u7248\u672c\u4e0d\u5339\u914d<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u5b89\u88c5\u7684numpy\u7248\u672c\u548c\u5176\u4ed6\u5e93\u4e0d\u517c\u5bb9\uff0c\u53ef\u4ee5\u6307\u5b9a\u7248\u672c\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code>pip install numpy==1.21.0<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u6743\u9650\u95ee\u9898<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u5b89\u88c5numpy\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code>sudo pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001numpy\u7684\u57fa\u672c\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5b89\u88c5\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528numpy\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9bnumpy\u7684\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>1. \u521b\u5efa\u6570\u7ec4<\/strong><\/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>a = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>b = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u521b\u5efa\u5168\u96f6\u6570\u7ec4<\/strong><\/h2>\n<p>c = np.zeros((3, 3))<\/p>\n<h2><strong>\u521b\u5efa\u5168\u4e00\u6570\u7ec4<\/strong><\/h2>\n<p>d = np.ones((3, 3))<\/p>\n<h2><strong>\u521b\u5efa\u5355\u4f4d\u77e9\u9635<\/strong><\/h2>\n<p>e = np.eye(3)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u6570\u7ec4\u64cd\u4f5c<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u7ec4\u52a0\u6cd5<\/p>\n<p>f = a + a<\/p>\n<h2><strong>\u6570\u7ec4\u4e58\u6cd5<\/strong><\/h2>\n<p>g = a * 2<\/p>\n<h2><strong>\u6570\u7ec4\u5f62\u72b6<\/strong><\/h2>\n<p>h = b.shape<\/p>\n<h2><strong>\u6570\u7ec4\u8f6c\u7f6e<\/strong><\/h2>\n<p>i = b.T<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u6570\u5b66\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u7ec4\u6c42\u548c<\/p>\n<p>j = np.sum(a)<\/p>\n<h2><strong>\u6570\u7ec4\u5747\u503c<\/strong><\/h2>\n<p>k = np.mean(a)<\/p>\n<h2><strong>\u6570\u7ec4\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>l = np.std(a)<\/p>\n<h2><strong>\u6570\u7ec4\u6700\u5927\u503c<\/strong><\/h2>\n<p>m = np.max(a)<\/p>\n<h2><strong>\u6570\u7ec4\u6700\u5c0f\u503c<\/strong><\/h2>\n<p>n = np.min(a)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4. \u968f\u673a\u6570<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u968f\u673a\u6570\u7ec4<\/p>\n<p>o = np.random.rand(3, 3)<\/p>\n<h2><strong>\u751f\u6210\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>p = np.random.randint(0, 10, (3, 3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>5. \u77e9\u9635\u8fd0\u7b97<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u77e9\u9635\u4e58\u6cd5<\/p>\n<p>q = np.dot(b, b.T)<\/p>\n<h2><strong>\u77e9\u9635\u9006<\/strong><\/h2>\n<p>r = np.linalg.inv(np.eye(3))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662fnumpy\u7684\u4e00\u4e9b\u57fa\u672c\u7528\u6cd5\uff0c\u66f4\u591a\u9ad8\u7ea7\u7528\u6cd5\u53ef\u4ee5\u53c2\u8003numpy\u7684\u5b98\u65b9\u6587\u6863\u548c\u6559\u7a0b\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001numpy\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u7684\u5e94\u7528<\/h3>\n<\/p>\n<p><p>numpy\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><p><strong>1. \u6570\u636e\u9884\u5904\u7406<\/strong><\/p>\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\u6b65\u3002numpy\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u5f52\u4e00\u5316\u3001\u6807\u51c6\u5316\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u6837\u672c\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.rand(100, 3)<\/p>\n<h2><strong>\u6570\u636e\u5f52\u4e00\u5316<\/strong><\/h2>\n<p>data_normalized = (data - np.min(data, axis=0)) \/ (np.max(data, axis=0) - np.min(data, axis=0))<\/p>\n<h2><strong>\u6570\u636e\u6807\u51c6\u5316<\/strong><\/h2>\n<p>data_standardized = (data - np.mean(data, axis=0)) \/ np.std(data, axis=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u7279\u5f81\u5de5\u7a0b<\/strong><\/p>\n<\/p>\n<p><p>\u7279\u5f81\u5de5\u7a0b\u662f\u63d0\u9ad8\u673a\u5668\u5b66\u4e60\u6a21\u578b\u6027\u80fd\u7684\u5173\u952e\u6b65\u9aa4\u3002numpy\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u7279\u5f81\u63d0\u53d6\u3001\u7279\u5f81\u9009\u62e9\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u6837\u672c\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.rand(100, 3)<\/p>\n<h2><strong>\u7279\u5f81\u63d0\u53d6<\/strong><\/h2>\n<p>features = np.hstack((data, data2, np.log(data + 1)))<\/p>\n<h2><strong>\u7279\u5f81\u9009\u62e9<\/strong><\/h2>\n<p>selected_features = features[:, [0, 1, 4]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u6570\u503c\u8ba1\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u9879\u76ee\u4e2d\uff0c\u6570\u503c\u8ba1\u7b97\u662f\u975e\u5e38\u5e38\u89c1\u7684\u9700\u6c42\u3002numpy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u503c\u8ba1\u7b97\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u77e9\u9635\u8fd0\u7b97\u3001\u6c42\u89e3\u65b9\u7a0b\u7ec4\u3001\u7edf\u8ba1\u5206\u6790\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u6837\u672c\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.rand(100, 3)<\/p>\n<h2><strong>\u77e9\u9635\u8fd0\u7b97<\/strong><\/h2>\n<p>cov_matrix = np.cov(data.T)<\/p>\n<h2><strong>\u6c42\u89e3\u65b9\u7a0b\u7ec4<\/strong><\/h2>\n<p>a = np.array([[3, 2], [1, 2]])<\/p>\n<p>b = np.array([1, 2])<\/p>\n<p>solution = np.linalg.solve(a, b)<\/p>\n<h2><strong>\u7edf\u8ba1\u5206\u6790<\/strong><\/h2>\n<p>mean = np.mean(data, axis=0)<\/p>\n<p>std = np.std(data, axis=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001numpy\u4e0e\u5176\u4ed6\u5e93\u7684\u96c6\u6210<\/h3>\n<\/p>\n<p><p>numpy\u5e38\u5e38\u4e0e\u5176\u4ed6\u79d1\u5b66\u8ba1\u7b97\u5e93\u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u96c6\u6210\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p><strong>1. pandas<\/strong><\/p>\n<\/p>\n<p><p>pandas\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u64cd\u4f5c\u529f\u80fd\u3002numpy\u6570\u7ec4\u53ef\u4ee5\u65b9\u4fbf\u5730\u8f6c\u6362\u4e3apandas DataFrame\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>import pandas as pd<\/p>\n<h2><strong>\u751f\u6210\u6837\u672c\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.rand(100, 3)<\/p>\n<h2><strong>\u8f6c\u6362\u4e3aDataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data, columns=[&#39;A&#39;, &#39;B&#39;, &#39;C&#39;])<\/p>\n<h2><strong>DataFrame\u8f6c\u6362\u4e3anumpy\u6570\u7ec4<\/strong><\/h2>\n<p>data_array = df.values<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. matplotlib<\/strong><\/p>\n<\/p>\n<p><p>matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ed8\u56fe\u529f\u80fd\u3002numpy\u6570\u7ec4\u53ef\u4ee5\u65b9\u4fbf\u5730\u7528\u4e8e\u7ed8\u5236\u5404\u79cd\u56fe\u8868\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>\u751f\u6210\u6837\u672c\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 2 * np.pi, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>plt.xlabel(&#39;X&#39;)<\/p>\n<p>plt.ylabel(&#39;Y&#39;)<\/p>\n<p>plt.title(&#39;Sine Wave&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. scikit-learn<\/strong><\/p>\n<\/p>\n<p><p>scikit-learn\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u673a\u5668\u5b66\u4e60\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u548c\u5de5\u5177\u3002numpy\u6570\u7ec4\u53ef\u4ee5\u65b9\u4fbf\u5730\u7528\u4e8e\u8bad\u7ec3\u548c\u9884\u6d4b\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from sklearn.linear_model import LinearRegression<\/p>\n<h2><strong>\u751f\u6210\u6837\u672c\u6570\u636e<\/strong><\/h2>\n<p>X = np.random.rand(100, 1)<\/p>\n<p>y = 3 * X.squeeze() + 2 + np.random.randn(100)<\/p>\n<h2><strong>\u8bad\u7ec3\u7ebf\u6027\u56de\u5f52\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<p>model.fit(X, y)<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>y_pred = model.predict(X)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001numpy\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><p>numpy\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u503c\u8ba1\u7b97\u529f\u80fd\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\uff0c\u4ecd\u7136\u9700\u8981\u8fdb\u884c\u6027\u80fd\u4f18\u5316\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5e38\u89c1\u7684\u4f18\u5316\u6280\u5de7\uff1a<\/p>\n<\/p>\n<p><p><strong>1. \u5411\u91cf\u5316\u8fd0\u7b97<\/strong><\/p>\n<\/p>\n<p><p>\u907f\u514d\u4f7f\u7528\u5faa\u73af\u8fdb\u884c\u9010\u5143\u7d20\u64cd\u4f5c\uff0c\u5c3d\u91cf\u4f7f\u7528numpy\u63d0\u4f9b\u7684\u5411\u91cf\u5316\u8fd0\u7b97\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u6837\u672c\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.rand(1000000)<\/p>\n<h2><strong>\u5411\u91cf\u5316\u8fd0\u7b97<\/strong><\/h2>\n<p>result = np.sqrt(data)<\/p>\n<h2><strong>\u5faa\u73af\u8fd0\u7b97<\/strong><\/h2>\n<p>result_loop = np.array([np.sqrt(x) for x in data])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/strong><\/p>\n<\/p>\n<p><p>numpy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u5728\u5e95\u5c42\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u6027\u80fd\u901a\u5e38\u4f18\u4e8e\u624b\u52a8\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u6837\u672c\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.rand(1000000)<\/p>\n<h2><strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570<\/strong><\/h2>\n<p>mean = np.mean(data)<\/p>\n<h2><strong>\u624b\u52a8\u5b9e\u73b0<\/strong><\/h2>\n<p>mean_manual = np.sum(data) \/ data.size<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>3. \u4f7f\u7528\u591a\u7ebf\u7a0b<\/strong><\/p>\n<\/p>\n<p><p>numpy\u7684\u8bb8\u591a\u64cd\u4f5c\u662f\u53ef\u4ee5\u5e76\u884c\u5316\u7684\uff0c\u53ef\u4ee5\u5229\u7528\u591a\u7ebf\u7a0b\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>from concurrent.futures import ThreadPoolExecutor<\/p>\n<h2><strong>\u751f\u6210\u6837\u672c\u6570\u636e<\/strong><\/h2>\n<p>data = np.random.rand(1000000)<\/p>\n<h2><strong>\u591a\u7ebf\u7a0b\u8fd0\u7b97<\/strong><\/h2>\n<p>def sqrt_func(data_chunk):<\/p>\n<p>    return np.sqrt(data_chunk)<\/p>\n<p>with ThreadPoolExecutor() as executor:<\/p>\n<p>    result = np.concatenate(list(executor.map(sqrt_func, np.array_split(data, 8))))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>4. \u4f7f\u7528GPU<\/strong><\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u89c4\u6a21\u7684\u6570\u503c\u8ba1\u7b97\uff0c\u53ef\u4ee5\u4f7f\u7528GPU\u52a0\u901f\u3002\u53ef\u4ee5\u4f7f\u7528CuPy\u5e93\uff0c\u5b83\u662f\u4e00\u4e2a\u4e0enumpy\u517c\u5bb9\u7684GPU\u52a0\u901f\u5e93\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import cupy as cp<\/p>\n<h2><strong>\u751f\u6210\u6837\u672c\u6570\u636e<\/strong><\/h2>\n<p>data = cp.random.rand(1000000)<\/p>\n<h2><strong>GPU\u8fd0\u7b97<\/strong><\/h2>\n<p>result = cp.sqrt(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>numpy\u662fPython\u4e2d\u6700\u57fa\u7840\u548c\u6700\u91cd\u8981\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u503c\u8ba1\u7b97\u529f\u80fd\u3002\u5b89\u88c5numpy\u53ef\u4ee5\u901a\u8fc7pip\u3001conda\u3001\u4e0b\u8f7d\u9884\u7f16\u8bd1\u5305\u548c\u4ece\u6e90\u7801\u7f16\u8bd1\u7b49\u591a\u79cd\u65b9\u5f0f\u3002numpy\u5728\u6570\u636e\u79d1\u5b66\u3001\u673a\u5668\u5b66\u4e60\u548c\u6570\u503c\u8ba1\u7b97\u4e2d\u6709\u7740\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5e76\u4e14\u53ef\u4ee5\u4e0epandas\u3001matplotlib\u548cscikit-learn\u7b49\u5e93\u8fdb\u884c\u96c6\u6210\u3002\u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u5411\u91cf\u5316\u8fd0\u7b97\u3001\u5185\u7f6e\u51fd\u6570\u3001\u591a\u7ebf\u7a0b\u548cGPU\u7b49\u4f18\u5316\u6280\u5de7\u3002\u638c\u63e1numpy\u7684\u4f7f\u7528\u548c\u4f18\u5316\u6280\u5de7\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u6570\u636e\u79d1\u5b66\u9879\u76ee\u7684\u6548\u7387\u548c\u6548\u679c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u68c0\u67e5\u6211\u7684Python\u662f\u5426\u5df2\u7ecf\u5b89\u88c5\u4e86numpy\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u60a8\u7684Python\u73af\u5883\u4e2d\u662f\u5426\u5df2\u5b89\u88c5numpy\uff0c\u60a8\u53ef\u4ee5\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165<code>pip show numpy<\/code>\u3002\u5982\u679c\u5df2\u5b89\u88c5\uff0c\u60a8\u4f1a\u770b\u5230numpy\u7684\u7248\u672c\u548c\u5176\u4ed6\u76f8\u5173\u4fe1\u606f\u3002\u5982\u679c\u672a\u5b89\u88c5\uff0c\u60a8\u4f1a\u6536\u5230\u4e00\u6761\u6d88\u606f\uff0c\u63d0\u793a\u672a\u627e\u5230\u8be5\u5305\u3002<\/p>\n<p><strong>\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5numpy\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u865a\u62df\u73af\u5883\u4e2d\u5b89\u88c5numpy\u6709\u52a9\u4e8e\u7ba1\u7406\u9879\u76ee\u7684\u4f9d\u8d56\u5173\u7cfb\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>venv<\/code>\u6216<code>conda<\/code>\u7b49\u5de5\u5177\u521b\u5efa\u865a\u62df\u73af\u5883\u3002\u521b\u5efa\u865a\u62df\u73af\u5883\u540e\uff0c\u6fc0\u6d3b\u5b83\u5e76\u8fd0\u884c<code>pip install numpy<\/code>\u8fdb\u884c\u5b89\u88c5\u3002\u8fd9\u5c06\u786e\u4fdd\u60a8\u7684\u9879\u76ee\u4e0d\u4f1a\u53d7\u5230\u5168\u5c40Python\u73af\u5883\u4e2d\u5176\u4ed6\u5305\u7684\u5f71\u54cd\u3002<\/p>\n<p><strong>\u5982\u679c\u5728\u5b89\u88c5numpy\u65f6\u9047\u5230\u9519\u8bef\uff0c\u6211\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u9047\u5230\u5b89\u88c5\u9519\u8bef\u65f6\uff0c\u9996\u5148\u68c0\u67e5\u60a8\u7684pip\u548cPython\u7248\u672c\u662f\u5426\u662f\u6700\u65b0\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pip install --upgrade pip<\/code>\u548c<code>python --version<\/code>\u6765\u786e\u8ba4\u3002\u8fd8\u53ef\u4ee5\u5c1d\u8bd5\u6e05\u7406pip\u7f13\u5b58\uff0c\u547d\u4ee4\u4e3a<code>pip cache purge<\/code>\uff0c\u7136\u540e\u91cd\u65b0\u5b89\u88c5numpy\u3002\u5982\u679c\u95ee\u9898\u4f9d\u7136\u5b58\u5728\uff0c\u53ef\u4ee5\u67e5\u770b\u9519\u8bef\u4fe1\u606f\uff0c\u641c\u7d22\u76f8\u5173\u89e3\u51b3\u65b9\u6848\u6216\u8bbf\u95eenumpy\u7684\u5b98\u65b9\u6587\u6863\u548c\u793e\u533a\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u7ed9Python\u88c5numpy\uff1a\u4f7f\u7528pip\u5b89\u88c5\u3001\u4f7f\u7528conda\u5b89\u88c5\u3001\u4e0b\u8f7d\u9884\u7f16\u8bd1\u5305\u3001\u4ece\u6e90\u7801\u7f16\u8bd1\u3002\u5176\u4e2d\uff0c\u4f7f\u7528pip [&hellip;]","protected":false},"author":3,"featured_media":1155228,"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\/1155220"}],"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=1155220"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155220\/revisions"}],"predecessor-version":[{"id":1155231,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1155220\/revisions\/1155231"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1155228"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1155220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1155220"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1155220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}