{"id":1010492,"date":"2024-12-27T11:21:30","date_gmt":"2024-12-27T03:21:30","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1010492.html"},"modified":"2024-12-27T11:21:34","modified_gmt":"2024-12-27T03:21:34","slug":"python%e5%a6%82%e4%bd%95pip%e5%ae%89%e8%a3%85numpy","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1010492.html","title":{"rendered":"python\u5982\u4f55pip\u5b89\u88c5numpy"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085004\/0b0129e1-33e1-4e29-a47f-5119c804381f.webp\" alt=\"python\u5982\u4f55pip\u5b89\u88c5numpy\" \/><\/p>\n<p><p> \u8981\u5728Python\u4e2d\u901a\u8fc7pip\u5b89\u88c5NumPy\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7b80\u5355\u7684\u6b65\u9aa4\uff1a<strong>\u6253\u5f00\u547d\u4ee4\u884c\u3001\u8f93\u5165\u5b89\u88c5\u547d\u4ee4\u3001\u9a8c\u8bc1\u5b89\u88c5<\/strong>\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u7684\u8ba1\u7b97\u673a\u4e0a\u5df2\u5b89\u88c5Python\u548cpip\u3002\u63a5\u7740\uff0c\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u7a97\u53e3\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5NumPy\uff1a<code>pip install numpy<\/code>\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728Python\u73af\u5883\u4e2d\u8f93\u5165<code>import numpy<\/code>\u6765\u9a8c\u8bc1NumPy\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u8bf4\u660e\u8fd9\u4e9b\u6b65\u9aa4\u4ee5\u53ca\u4e00\u4e9b\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u548c\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u51c6\u5907\u73af\u5883<\/p>\n<\/p>\n<p><p>\u5728\u5b89\u88c5NumPy\u4e4b\u524d\uff0c\u786e\u4fdd\u4f60\u7684\u8ba1\u7b97\u673a\u4e0a\u5df2\u7ecf\u5b89\u88c5\u4e86Python\u548cpip\u3002Python\u662f\u4e00\u79cd\u7f16\u7a0b\u8bed\u8a00\uff0c\u800cpip\u662fPython\u7684\u5305\u7ba1\u7406\u5668\uff0c\u7528\u4e8e\u5b89\u88c5\u548c\u7ba1\u7406Python\u8f6f\u4ef6\u5305\u3002\u901a\u5e38\uff0c\u5728\u5b89\u88c5Python\u65f6\uff0cpip\u4f1a\u81ea\u52a8\u5b89\u88c5\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95eePython\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5Python\u3002\u5728\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0c\u786e\u4fdd\u52fe\u9009\u201cAdd Python to PATH\u201d\u9009\u9879\uff0c\u4ee5\u4fbf\u547d\u4ee4\u884c\u80fd\u591f\u8bc6\u522bPython\u548cpip\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u6253\u5f00\u547d\u4ee4\u884c\u6216\u7ec8\u7aef<\/p>\n<\/p>\n<p><p>\u6839\u636e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\uff0c\u9009\u62e9\u5408\u9002\u7684\u547d\u4ee4\u884c\u5de5\u5177\u3002\u5bf9\u4e8eWindows\u7528\u6237\uff0c\u4f7f\u7528\u201c\u547d\u4ee4\u63d0\u793a\u7b26\u201d\u6216\u201cPowerShell\u201d\uff1b\u5bf9\u4e8eMac\u548cLinux\u7528\u6237\uff0c\u4f7f\u7528\u201c\u7ec8\u7aef\u201d\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u8ba9\u4f60\u8f93\u5165\u548c\u6267\u884c\u547d\u4ee4\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8f93\u5165\u5b89\u88c5\u547d\u4ee4<\/p>\n<\/p>\n<p><p>\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\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>\u8be5\u547d\u4ee4\u5c06\u4ecePython\u5305\u7d22\u5f15\uff08PyPI\uff09\u4e0b\u8f7d\u5e76\u5b89\u88c5NumPy\u7684\u6700\u65b0\u7a33\u5b9a\u7248\u672c\u3002\u5b89\u88c5\u8fc7\u7a0b\u4e2d\uff0cpip\u5c06\u81ea\u52a8\u5904\u7406\u6240\u6709\u4f9d\u8d56\u9879\uff0c\u65e0\u9700\u624b\u52a8\u5e72\u9884\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u9a8c\u8bc1\u5b89\u88c5<\/p>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u9a8c\u8bc1NumPy\u662f\u5426\u6b63\u786e\u5b89\u88c5\u3002\u6253\u5f00Python\u4ea4\u4e92\u73af\u5883\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5982\u679c\u6ca1\u6709\u9519\u8bef\u6d88\u606f\uff0c\u5219\u8868\u793aNumPy\u5df2\u6210\u529f\u5b89\u88c5\u3002\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5NumPy\u7684\u7248\u672c\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>\u4e94\u3001\u5b89\u88c5\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u9047\u5230\u7684\u95ee\u9898\u53ca\u89e3\u51b3\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>1\u3001\u7f51\u7edc\u95ee\u9898<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u7f51\u7edc\u95ee\u9898\u4f1a\u5bfc\u81f4\u5b89\u88c5\u5931\u8d25\u3002\u5c1d\u8bd5\u4f7f\u7528\u56fd\u5185\u955c\u50cf\u6e90\u52a0\u901f\u4e0b\u8f7d\u8fc7\u7a0b\u3002\u4f60\u53ef\u4ee5\u5728\u5b89\u88c5\u547d\u4ee4\u4e2d\u6307\u5b9a\u955c\u50cf\u6e90\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy -i https:\/\/pypi.tuna.tsinghua.edu.cn\/simple<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u6743\u9650\u95ee\u9898<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u7ba1\u7406\u5458\u6743\u9650\u6765\u5b89\u88c5\u8f6f\u4ef6\u5305\u3002\u5bf9\u4e8eWindows\u7528\u6237\uff0c\u5c1d\u8bd5\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u547d\u4ee4\u63d0\u793a\u7b26\u3002\u5bf9\u4e8eMac\u548cLinux\u7528\u6237\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sudo<\/code>\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001pip\u7248\u672c\u8fc7\u65e7<\/p>\n<\/p>\n<p><p>\u5982\u679cpip\u7248\u672c\u8fc7\u65e7\uff0c\u4e5f\u53ef\u80fd\u5bfc\u81f4\u5b89\u88c5\u5931\u8d25\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\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><p>4\u3001Python\u7248\u672c\u517c\u5bb9\u6027<\/p>\n<\/p>\n<p><p>\u786e\u4fdd\u4f60\u7684Python\u7248\u672c\u4e0eNumPy\u517c\u5bb9\u3002NumPy\u901a\u5e38\u652f\u6301Python\u7684\u51e0\u4e2a\u6700\u65b0\u7248\u672c\u3002\u4f60\u53ef\u4ee5\u5728NumPy\u7684\u5b98\u65b9\u7f51\u7ad9\u4e0a\u627e\u5230\u652f\u6301\u7684Python\u7248\u672c\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528NumPy\u7684\u4e00\u4e9b\u57fa\u672c\u64cd\u4f5c<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u548c\u4e30\u5bcc\u7684\u6570\u5b66\u51fd\u6570\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684NumPy\u64cd\u4f5c\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>1\u3001\u521b\u5efa\u6570\u7ec4<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>arr1 = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>arr2 = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u6570\u7ec4\u8fd0\u7b97<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u7ec4\u52a0\u6cd5<\/p>\n<p>arr_sum = arr1 + 10<\/p>\n<h2><strong>\u6570\u7ec4\u4e58\u6cd5<\/strong><\/h2>\n<p>arr_product = arr1 * 2<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>matrix_product = np.dot(arr2, arr2.T)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u7edf\u8ba1\u51fd\u6570<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6c42\u548c<\/p>\n<p>total_sum = np.sum(arr1)<\/p>\n<h2><strong>\u5e73\u5747\u503c<\/strong><\/h2>\n<p>average = np.mean(arr1)<\/p>\n<h2><strong>\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>std_dev = np.std(arr1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>4\u3001\u6570\u7ec4\u5f62\u72b6\u548c\u91cd\u5851<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u67e5\u770b\u6570\u7ec4\u5f62\u72b6<\/p>\n<p>shape = arr2.shape<\/p>\n<h2><strong>\u91cd\u5851\u6570\u7ec4<\/strong><\/h2>\n<p>reshaped_array = arr1.reshape((5, 1))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001NumPy\u7684\u9ad8\u7ea7\u529f\u80fd<\/p>\n<\/p>\n<p><p>NumPy\u4e0d\u4ec5\u652f\u6301\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u7ebf\u6027\u4ee3\u6570\u3001\u5085\u91cc\u53f6\u53d8\u6362\u548c\u968f\u673a\u6570\u751f\u6210\u3002\u8fd9\u4e9b\u529f\u80fd\u4f7fNumPy\u6210\u4e3a\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p>1\u3001\u7ebf\u6027\u4ee3\u6570<\/p>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7ebf\u6027\u4ee3\u6570\u529f\u80fd\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u529f\u80fd\u6765\u8fdb\u884c\u77e9\u9635\u5206\u89e3\u3001\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u9006\u77e9\u9635<\/p>\n<p>inverse_matrix = np.linalg.inv(arr2)<\/p>\n<h2><strong>\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf<\/strong><\/h2>\n<p>eigenvalues, eigenvectors = np.linalg.eig(arr2)<\/p>\n<h2><strong>\u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4<\/strong><\/h2>\n<p>coefficients = np.array([[3, 1], [1, 2]])<\/p>\n<p>constants = np.array([9, 8])<\/p>\n<p>solutions = np.linalg.solve(coefficients, constants)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u5085\u91cc\u53f6\u53d8\u6362<\/p>\n<\/p>\n<p><p>NumPy\u7684\u5085\u91cc\u53f6\u53d8\u6362\u6a21\u5757\uff08numpy.fft\uff09\u53ef\u4ee5\u7528\u6765\u5206\u6790\u4fe1\u53f7\u9891\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u4e00\u7ef4\u5085\u91cc\u53f6\u53d8\u6362<\/p>\n<p>signal = np.array([1, 2, 3, 4])<\/p>\n<p>fft_result = np.fft.fft(signal)<\/p>\n<h2><strong>\u8ba1\u7b97\u4e8c\u7ef4\u5085\u91cc\u53f6\u53d8\u6362<\/strong><\/h2>\n<p>image = np.array([[1, 2], [3, 4]])<\/p>\n<p>fft2_result = np.fft.fft2(image)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u968f\u673a\u6570\u751f\u6210<\/p>\n<\/p>\n<p><p>NumPy\u7684\u968f\u673a\u6a21\u5757\uff08numpy.random\uff09\u63d0\u4f9b\u4e86\u5404\u79cd\u968f\u673a\u6570\u751f\u6210\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e00\u4e2a\u968f\u673a\u6574\u6570<\/p>\n<p>random_int = np.random.randint(0, 10)<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_float = np.random.rand()<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u670d\u4ece\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570<\/strong><\/h2>\n<p>random_normal = np.random.randn()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001NumPy\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>NumPy\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u5f97\u5230\u4e86\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u7279\u522b\u662f\u5728\u6570\u636e\u5206\u6790\u548c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u4e2d\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2aNumPy\u5728\u6570\u636e\u79d1\u5b66\u4e2d\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<\/p>\n<p><p>1\u3001\u6570\u636e\u9884\u5904\u7406<\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u79d1\u5b66\u4e2d\uff0c\u6570\u636e\u9884\u5904\u7406\u662f\u4e00\u4e2a\u5173\u952e\u6b65\u9aa4\u3002NumPy\u53ef\u4ee5\u5e2e\u52a9\u4f60\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u3001\u5f52\u4e00\u5316\u548c\u7279\u5f81\u5de5\u7a0b\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528NumPy\u6765\u5904\u7406\u7f3a\u5931\u6570\u636e\u3001\u7f29\u653e\u7279\u5f81\u4ee5\u53ca\u8fdb\u884c\u6570\u636e\u53d8\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5904\u7406\u7f3a\u5931\u6570\u636e<\/p>\n<p>data = np.array([1, np.nan, 3, 4, 5])<\/p>\n<p>data_cleaned = np.nan_to_num(data, nan=0)<\/p>\n<h2><strong>\u7279\u5f81\u7f29\u653e<\/strong><\/h2>\n<p>data_scaled = (data - np.mean(data)) \/ np.std(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u6570\u636e\u5206\u6790<\/p>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7edf\u8ba1\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u5206\u6790\u6570\u636e\u7684\u5206\u5e03\u548c\u8d8b\u52bf\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528NumPy\u6765\u8ba1\u7b97\u6570\u636e\u7684\u5206\u4f4d\u6570\u3001\u4e2d\u4f4d\u6570\u548c\u65b9\u5dee\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u5206\u4f4d\u6570<\/p>\n<p>quantiles = np.percentile(data, [25, 50, 75])<\/p>\n<h2><strong>\u8ba1\u7b97\u4e2d\u4f4d\u6570<\/strong><\/h2>\n<p>median = np.median(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u65b9\u5dee<\/strong><\/h2>\n<p>variance = np.var(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u673a\u5668\u5b66\u4e60<\/p>\n<\/p>\n<p><p>\u5728\u673a\u5668\u5b66\u4e60\u4e2d\uff0cNumPy\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u6a21\u578b\u548c\u7b97\u6cd5\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528NumPy\u6765\u5b9e\u73b0\u7ebf\u6027\u56de\u5f52\u3001\u903b\u8f91\u56de\u5f52\u548c\u652f\u6301\u5411\u91cf\u673a\u7b49\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9e\u73b0\u7b80\u5355\u7ebf\u6027\u56de\u5f52<\/p>\n<p>def linear_regression(X, y):<\/p>\n<p>    X_b = np.c_[np.ones((len(X), 1)), X]  # \u6dfb\u52a0 x0 = 1<\/p>\n<p>    theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)<\/p>\n<p>    return theta_best<\/p>\n<p>X = np.array([[1], [2], [3], [4], [5]])<\/p>\n<p>y = np.array([3, 7, 5, 9, 11])<\/p>\n<p>theta = linear_regression(X, y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001NumPy\u4e0e\u5176\u4ed6\u5e93\u7684\u96c6\u6210<\/p>\n<\/p>\n<p><p>NumPy\u4e0e\u5176\u4ed6\u79d1\u5b66\u8ba1\u7b97\u5e93\uff08\u5982SciPy\u3001Pandas\u548cMatplotlib\uff09\u9ad8\u5ea6\u96c6\u6210\uff0c\u4f7f\u5176\u6210\u4e3aPython\u6570\u636e\u751f\u6001\u7cfb\u7edf\u7684\u6838\u5fc3\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u96c6\u6210\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><p>1\u3001\u4e0eSciPy\u7684\u96c6\u6210<\/p>\n<\/p>\n<p><p>SciPy\u662f\u4e00\u4e2a\u57fa\u4e8eNumPy\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u5982\u4f18\u5316\u3001\u63d2\u503c\u548c\u4fe1\u53f7\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy import optimize<\/p>\n<h2><strong>\u4f7f\u7528SciPy\u8fdb\u884c\u51fd\u6570\u4f18\u5316<\/strong><\/h2>\n<p>def objective_function(x):<\/p>\n<p>    return x2 + 5*np.sin(x)<\/p>\n<p>result = optimize.minimize(objective_function, x0=0)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u4e0ePandas\u7684\u96c6\u6210<\/p>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u6570\u636e\u7ed3\u6784\u548c\u6570\u636e\u64cd\u4f5c\u5de5\u5177\u3002Pandas\u7684\u6838\u5fc3\u6570\u636e\u7ed3\u6784\uff08DataFrame\u548cSeries\uff09\u4e0eNumPy\u6570\u7ec4\u9ad8\u5ea6\u517c\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5c06NumPy\u6570\u7ec4\u8f6c\u6362\u4e3aPandas DataFrame<\/strong><\/h2>\n<p>data_frame = pd.DataFrame(data, columns=[&#39;Value&#39;])<\/p>\n<h2><strong>\u4ecePandas DataFrame\u4e2d\u63d0\u53d6NumPy\u6570\u7ec4<\/strong><\/h2>\n<p>array_from_df = data_frame[&#39;Value&#39;].values<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u4e0eMatplotlib\u7684\u96c6\u6210<\/p>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\u7684\u5e93\uff0c\u652f\u6301\u751f\u6210\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528NumPy\u751f\u6210\u7684\u6570\u636e\u4e0eMatplotlib\u8fdb\u884c\u96c6\u6210\uff0c\u4ee5\u521b\u5efa\u53ef\u89c6\u5316\u6548\u679c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u751f\u6210\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(0, 10, 100)<\/p>\n<p>y = np.sin(x)<\/p>\n<h2><strong>\u4f7f\u7528Matplotlib\u7ed8\u5236\u56fe\u8868<\/strong><\/h2>\n<p>plt.plot(x, y)<\/p>\n<p>plt.title(&#39;Sine Wave&#39;)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;sin(x)&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u3001NumPy\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<p><p>NumPy\u4ee5\u5176\u9ad8\u6548\u7684\u6570\u7ec4\u64cd\u4f5c\u800c\u95fb\u540d\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8fdb\u4e00\u6b65\u4f18\u5316\u6027\u80fd\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f18\u5316NumPy\u6027\u80fd\u7684\u6280\u5de7\uff1a<\/p>\n<\/p>\n<p><p>1\u3001\u4f7f\u7528\u5e7f\u64ad<\/p>\n<\/p>\n<p><p>\u5e7f\u64ad\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u8ba9\u4f60\u5728\u4e0d\u521b\u5efa\u989d\u5916\u6570\u7ec4\u7684\u60c5\u51b5\u4e0b\u8fdb\u884c\u6570\u7ec4\u8fd0\u7b97\u3002\u5e7f\u64ad\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u901f\u5ea6\uff0c\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5e7f\u64ad\u8fdb\u884c\u6570\u7ec4\u8fd0\u7b97<\/p>\n<p>a = np.array([1, 2, 3])<\/p>\n<p>b = np.array([4])<\/p>\n<p>result = a + b  # \u81ea\u52a8\u5e7f\u64adb<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2\u3001\u907f\u514d\u5faa\u73af<\/p>\n<\/p>\n<p><p>\u5c3d\u91cf\u4f7f\u7528NumPy\u7684\u5185\u7f6e\u51fd\u6570\u548c\u5411\u91cf\u5316\u64cd\u4f5c\uff0c\u800c\u4e0d\u662f\u4f7f\u7528Python\u5faa\u73af\u3002NumPy\u7684\u51fd\u6570\u901a\u5e38\u5728\u5e95\u5c42\u4f7f\u7528C\u8bed\u8a00\u5b9e\u73b0\uff0c\u56e0\u6b64\u66f4\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u51fd\u6570\u66ff\u4ee3\u5faa\u73af<\/p>\n<p>a = np.array([1, 2, 3, 4, 5])<\/p>\n<p>squared = np.square(a)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3\u3001\u4f7f\u7528NumPy\u7684\u9ad8\u7ea7\u51fd\u6570<\/p>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u5e95\u5c42\u8fdb\u884c\u4f18\u5316\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>numpy.dot<\/code>\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\uff0c\u800c\u4e0d\u662f\u624b\u52a8\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528NumPy\u7684\u9ad8\u7ea7\u51fd\u6570\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5<\/p>\n<p>a = np.array([[1, 2], [3, 4]])<\/p>\n<p>b = np.array([[5, 6], [7, 8]])<\/p>\n<p>product = np.dot(a, b)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u4e00\u3001\u5b66\u4e60\u8d44\u6e90\u548c\u793e\u533a\u652f\u6301<\/p>\n<\/p>\n<p><p>NumPy\u62e5\u6709\u4e00\u4e2a\u6d3b\u8dc3\u7684\u793e\u533a\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b66\u4e60\u8d44\u6e90\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u63a8\u8350\u7684\u5b66\u4e60\u8d44\u6e90\u548c\u793e\u533a\u652f\u6301\uff1a<\/p>\n<\/p>\n<p><p>1\u3001\u5b98\u65b9\u6587\u6863<\/p>\n<\/p>\n<p><p>NumPy\u7684\u5b98\u65b9\u6587\u6863\u662f\u4e86\u89e3\u5176\u529f\u80fd\u548c\u7528\u6cd5\u7684\u6700\u4f73\u8d44\u6e90\u3002\u6587\u6863\u4e2d\u5305\u542b\u8be6\u7ec6\u7684API\u8bf4\u660e\u3001\u4f7f\u7528\u793a\u4f8b\u548c\u5e38\u89c1\u95ee\u9898\u89e3\u7b54\u3002<\/p>\n<\/p>\n<p><p>2\u3001\u5728\u7ebf\u6559\u7a0b<\/p>\n<\/p>\n<p><p>\u8bb8\u591a\u5728\u7ebf\u5e73\u53f0\u63d0\u4f9bNumPy\u7684\u6559\u7a0b\u548c\u8bfe\u7a0b\uff0c\u5982Coursera\u3001edX\u548cYouTube\u3002\u8fd9\u4e9b\u8d44\u6e90\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u5165\u95e8\u5e76\u6df1\u5165\u5b66\u4e60NumPy\u7684\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>3\u3001\u793e\u533a\u8bba\u575b<\/p>\n<\/p>\n<p><p>NumPy\u793e\u533a\u975e\u5e38\u6d3b\u8dc3\uff0c\u8bb8\u591a\u5f00\u53d1\u8005\u5728\u8bba\u575b\u548c\u793e\u4ea4\u5a92\u4f53\u4e0a\u5206\u4eab\u4ed6\u4eec\u7684\u7ecf\u9a8c\u548c\u89e3\u51b3\u65b9\u6848\u3002\u4f60\u53ef\u4ee5\u5728Stack Overflow\u3001Reddit\u548cGitHub\u4e0a\u627e\u5230\u8bb8\u591a\u5173\u4e8eNumPy\u7684\u95ee\u9898\u548c\u8ba8\u8bba\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\u548c\u8d44\u6e90\uff0c\u4f60\u5c06\u80fd\u591f\u6210\u529f\u5b89\u88c5NumPy\uff0c\u5e76\u5229\u7528\u5176\u5f3a\u5927\u7684\u529f\u80fd\u8fdb\u884c\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5206\u6790\u3002\u65e0\u8bba\u4f60\u662f\u521d\u5b66\u8005\u8fd8\u662f\u6709\u7ecf\u9a8c\u7684\u5f00\u53d1\u8005\uff0cNumPy\u90fd\u5c06\u662f\u4f60\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u786e\u8ba4\u6211\u7684Python\u73af\u5883\u5df2\u5b89\u88c5pip\uff1f<\/strong><br \/>\u5728\u4f60\u7684\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8f93\u5165<code>pip --version<\/code>\uff0c\u5982\u679cpip\u5df2\u5b89\u88c5\uff0c\u4f1a\u663e\u793a\u7248\u672c\u4fe1\u606f\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5148\u5b89\u88c5pip\uff0c\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4e0b\u8f7d<code>get-pip.py<\/code>\u811a\u672c\u5e76\u8fd0\u884c<code>python get-pip.py<\/code>\u6765\u5b8c\u6210\u3002<\/p>\n<p><strong>\u5982\u679c\u5728\u5b89\u88c5numpy\u65f6\u51fa\u73b0\u6743\u9650\u9519\u8bef\uff0c\u6211\u8be5\u5982\u4f55\u5904\u7406\uff1f<\/strong><br \/>\u5982\u679c\u9047\u5230\u6743\u9650\u9519\u8bef\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5728\u547d\u4ee4\u524d\u6dfb\u52a0<code>sudo<\/code>\uff08\u5bf9\u4e8eMac\u548cLinux\u7528\u6237\uff09\uff0c\u5373\u8f93\u5165<code>sudo pip install numpy<\/code>\u3002\u5bf9\u4e8eWindows\u7528\u6237\uff0c\u53ef\u4ee5\u4ee5\u7ba1\u7406\u5458\u8eab\u4efd\u8fd0\u884c\u547d\u4ee4\u63d0\u793a\u7b26\u6765\u89e3\u51b3\u6743\u9650\u95ee\u9898\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\u8fdb\u5165Python\u89e3\u91ca\u5668\u5e76\u8f93\u5165<code>import numpy<\/code>\u6765\u9a8c\u8bc1\u3002\u5982\u679c\u6ca1\u6709\u9519\u8bef\u63d0\u793a\uff0c\u8bf4\u660enumpy\u5df2\u6210\u529f\u5b89\u88c5\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>numpy.__version__<\/code>\u6765\u68c0\u67e5\u5b89\u88c5\u7684numpy\u7248\u672c\uff0c\u786e\u4fdd\u5176\u7b26\u5408\u4f60\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u901a\u8fc7pip\u5b89\u88c5NumPy\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u7b80\u5355\u7684\u6b65\u9aa4\uff1a\u6253\u5f00\u547d\u4ee4\u884c\u3001\u8f93\u5165\u5b89\u88c5\u547d\u4ee4\u3001\u9a8c\u8bc1\u5b89\u88c5\u3002\u9996\u5148 [&hellip;]","protected":false},"author":3,"featured_media":1010506,"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\/1010492"}],"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=1010492"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010492\/revisions"}],"predecessor-version":[{"id":1010509,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1010492\/revisions\/1010509"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1010506"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1010492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1010492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1010492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}