{"id":1153235,"date":"2025-01-13T17:36:15","date_gmt":"2025-01-13T09:36:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1153235.html"},"modified":"2025-01-13T17:36:17","modified_gmt":"2025-01-13T09:36:17","slug":"python%e5%a6%82%e4%bd%95%e5%bc%95%e5%85%a5numpy-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1153235.html","title":{"rendered":"python+\u5982\u4f55\u5f15\u5165numpy"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25183035\/98a47ba7-de02-48fd-b9c9-019ae6d55da3.webp\" alt=\"python+\u5982\u4f55\u5f15\u5165numpy\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5f15\u5165NumPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5\u5e76\u901a\u8fc7import\u8bed\u53e5\u6765\u5f15\u5165\u3002NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61\u3001\u5404\u79cd\u5bfc\u5165\/\u5bfc\u51fa\u529f\u80fd\u3001\u7ebf\u6027\u4ee3\u6570\u64cd\u4f5c\u3001\u968f\u673a\u6570\u751f\u6210\u7b49\u529f\u80fd\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5NumPy<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5728Python\u73af\u5883\u4e2d\u5b89\u88c5NumPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5f15\u5165NumPy<\/strong>\uff1a\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u901a\u8fc7import\u8bed\u53e5\u5f15\u5165NumPy\u5e93\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u5f15\u5165NumPy\u5e93\u540e\uff0c\u4fbf\u53ef\u4ee5\u5229\u7528\u5176\u5f3a\u5927\u7684\u529f\u80fd\u6765\u8fdb\u884c\u5404\u79cd\u79d1\u5b66\u8ba1\u7b97\u548c\u6570\u636e\u5904\u7406\u5de5\u4f5c\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecdNumPy\u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\u3001\u5e38\u7528\u529f\u80fd\u4ee5\u53ca\u4e00\u4e9b\u9ad8\u7ea7\u5e94\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001NUMPY\u7684\u57fa\u672c\u529f\u80fd\u4e0e\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>1.1\u3001\u6570\u7ec4\u521b\u5efa<\/h4>\n<\/p>\n<p><p>NumPy\u7684\u6838\u5fc3\u662f\u5176\u5f3a\u5927\u7684\u591a\u7ef4\u6570\u7ec4\u5bf9\u8c61<code>ndarray<\/code>\u3002\u521b\u5efa\u6570\u7ec4\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u76f4\u63a5\u4f7f\u7528\u5217\u8868\u6216\u5143\u7ec4\u3001\u4f7f\u7528NumPy\u7684\u5185\u7f6e\u51fd\u6570\uff08\u5982<code>arange<\/code>\u3001<code>linspace<\/code>\u3001<code>zeros<\/code>\u3001<code>ones<\/code>\u7b49\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u4f7f\u7528\u5217\u8868\u521b\u5efa\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>arr1 = np.array([1, 2, 3, 4, 5])<\/p>\n<p>print(arr1)<\/p>\n<h2><strong>\u4f7f\u7528\u5143\u7ec4\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>arr2 = np.array([(1, 2, 3), (4, 5, 6)])<\/p>\n<p>print(arr2)<\/p>\n<h2><strong>\u521b\u5efa\u5168\u96f6\u6570\u7ec4<\/strong><\/h2>\n<p>zeros_array = np.zeros((3, 4))<\/p>\n<p>print(zeros_array)<\/p>\n<h2><strong>\u521b\u5efa\u5168\u4e00\u6570\u7ec4<\/strong><\/h2>\n<p>ones_array = np.ones((2, 3))<\/p>\n<p>print(ones_array)<\/p>\n<h2><strong>\u4f7f\u7528arange\u521b\u5efa\u6570\u7ec4<\/strong><\/h2>\n<p>range_array = np.arange(10, 20, 2)<\/p>\n<p>print(range_array)<\/p>\n<h2><strong>\u4f7f\u7528linspace\u521b\u5efa\u6570\u7ec4<\/strong><\/h2>\n<p>linspace_array = np.linspace(0, 1, 5)<\/p>\n<p>print(linspace_array)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2\u3001\u6570\u7ec4\u5c5e\u6027<\/h4>\n<\/p>\n<p><p><code>ndarray<\/code>\u5bf9\u8c61\u6709\u5f88\u591a\u5c5e\u6027\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u4e86\u89e3\u6570\u7ec4\u7684\u4fe1\u606f\uff0c\u5982<code>shape<\/code>\u3001<code>dtype<\/code>\u3001<code>size<\/code>\u3001<code>ndim<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u6570\u7ec4\u5f62\u72b6<\/strong><\/h2>\n<p>print(arr.shape)<\/p>\n<h2><strong>\u6570\u7ec4\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p>print(arr.dtype)<\/p>\n<h2><strong>\u6570\u7ec4\u5143\u7d20\u4e2a\u6570<\/strong><\/h2>\n<p>print(arr.size)<\/p>\n<h2><strong>\u6570\u7ec4\u7ef4\u5ea6<\/strong><\/h2>\n<p>print(arr.ndim)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.3\u3001\u6570\u7ec4\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u7ec4\u64cd\u4f5c\u529f\u80fd\uff0c\u5982\u6570\u7ec4\u5207\u7247\u3001\u5f62\u72b6\u53d8\u6362\u3001\u6570\u7ec4\u62fc\u63a5\u548c\u5206\u5272\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6570\u7ec4\u5207\u7247<\/p>\n<p>arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>print(arr[1:, 1:])<\/p>\n<h2><strong>\u6570\u7ec4\u5f62\u72b6\u53d8\u6362<\/strong><\/h2>\n<p>reshaped_arr = arr.reshape((1, 9))<\/p>\n<p>print(reshaped_arr)<\/p>\n<h2><strong>\u6570\u7ec4\u62fc\u63a5<\/strong><\/h2>\n<p>arr1 = np.array([1, 2, 3])<\/p>\n<p>arr2 = np.array([4, 5, 6])<\/p>\n<p>concatenated_arr = np.concatenate((arr1, arr2))<\/p>\n<p>print(concatenated_arr)<\/p>\n<h2><strong>\u6570\u7ec4\u5206\u5272<\/strong><\/h2>\n<p>split_arr = np.split(arr, 3)<\/p>\n<p>print(split_arr)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001NUMPY\u7684\u6570\u5b66\u8fd0\u7b97<\/h3>\n<\/p>\n<p><h4>2.1\u3001\u57fa\u672c\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>NumPy\u652f\u6301\u5bf9\u6570\u7ec4\u8fdb\u884c\u5404\u79cd\u57fa\u672c\u6570\u5b66\u8fd0\u7b97\uff0c\u5982\u52a0\u51cf\u4e58\u9664\u3001\u5e42\u8fd0\u7b97\u3001\u5bf9\u6570\u8fd0\u7b97\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u52a0\u6cd5<\/strong><\/h2>\n<p>print(arr + 2)<\/p>\n<h2><strong>\u4e58\u6cd5<\/strong><\/h2>\n<p>print(arr * 3)<\/p>\n<h2><strong>\u5e42\u8fd0\u7b97<\/strong><\/h2>\n<p>print(arr  2)<\/p>\n<h2><strong>\u5bf9\u6570\u8fd0\u7b97<\/strong><\/h2>\n<p>print(np.log(arr))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2\u3001\u7edf\u8ba1\u8fd0\u7b97<\/h4>\n<\/p>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u5404\u79cd\u7edf\u8ba1\u51fd\u6570\uff0c\u5982\u6c42\u548c\u3001\u5747\u503c\u3001\u4e2d\u4f4d\u6570\u3001\u6807\u51c6\u5dee\u3001\u65b9\u5dee\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u6c42\u548c<\/strong><\/h2>\n<p>print(np.sum(arr))<\/p>\n<h2><strong>\u5747\u503c<\/strong><\/h2>\n<p>print(np.mean(arr))<\/p>\n<h2><strong>\u4e2d\u4f4d\u6570<\/strong><\/h2>\n<p>print(np.median(arr))<\/p>\n<h2><strong>\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>print(np.std(arr))<\/p>\n<h2><strong>\u65b9\u5dee<\/strong><\/h2>\n<p>print(np.var(arr))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.3\u3001\u7ebf\u6027\u4ee3\u6570<\/h4>\n<\/p>\n<p><p>NumPy\u4e5f\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ebf\u6027\u4ee3\u6570\u529f\u80fd\uff0c\u5982\u77e9\u9635\u4e58\u6cd5\u3001\u77e9\u9635\u8f6c\u7f6e\u3001\u9006\u77e9\u9635\u3001\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr1 = np.array([[1, 2], [3, 4]])<\/p>\n<p>arr2 = np.array([[5, 6], [7, 8]])<\/p>\n<h2><strong>\u77e9\u9635\u4e58\u6cd5<\/strong><\/h2>\n<p>print(np.dot(arr1, arr2))<\/p>\n<h2><strong>\u77e9\u9635\u8f6c\u7f6e<\/strong><\/h2>\n<p>print(np.transpose(arr1))<\/p>\n<h2><strong>\u9006\u77e9\u9635<\/strong><\/h2>\n<p>print(np.linalg.inv(arr1))<\/p>\n<h2><strong>\u7279\u5f81\u503c\u548c\u7279\u5f81\u5411\u91cf<\/strong><\/h2>\n<p>eigenvalues, eigenvectors = np.linalg.eig(arr1)<\/p>\n<p>print(eigenvalues)<\/p>\n<p>print(eigenvectors)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001NUMPY\u7684\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\uff0c\u5982\u5747\u5300\u5206\u5e03\u3001\u6b63\u6001\u5206\u5e03\u3001\u4e8c\u9879\u5206\u5e03\u7b49\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u5747\u5300\u5206\u5e03<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\u53ef\u4ee5\u4f7f\u7528<code>np.random.uniform<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570<\/p>\n<p>uniform_random_numbers = np.random.uniform(0, 1, 10)<\/p>\n<p>print(uniform_random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2\u3001\u6b63\u6001\u5206\u5e03<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u53ef\u4ee5\u4f7f\u7528<code>np.random.normal<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570<\/p>\n<p>normal_random_numbers = np.random.normal(0, 1, 10)<\/p>\n<p>print(normal_random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.3\u3001\u4e8c\u9879\u5206\u5e03<\/h4>\n<\/p>\n<p><p>\u751f\u6210\u4e8c\u9879\u5206\u5e03\u7684\u968f\u673a\u6570\u53ef\u4ee5\u4f7f\u7528<code>np.random.binomial<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u751f\u6210\u4e8c\u9879\u5206\u5e03\u7684\u968f\u673a\u6570<\/p>\n<p>binomial_random_numbers = np.random.binomial(10, 0.5, 10)<\/p>\n<p>print(binomial_random_numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001NUMPY\u7684\u9ad8\u7ea7\u5e94\u7528<\/h3>\n<\/p>\n<p><h4>4.1\u3001\u5e7f\u64ad\u673a\u5236<\/h4>\n<\/p>\n<p><p>NumPy\u7684\u5e7f\u64ad\u673a\u5236\u5141\u8bb8\u4e0d\u540c\u5f62\u72b6\u7684\u6570\u7ec4\u8fdb\u884c\u8fd0\u7b97\uff0c\u8fd9\u662fNumPy\u9ad8\u6548\u8ba1\u7b97\u7684\u91cd\u8981\u7279\u6027\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr1 = np.array([1, 2, 3])<\/p>\n<p>arr2 = np.array([[4], [5], [6]])<\/p>\n<h2><strong>\u5e7f\u64ad\u673a\u5236<\/strong><\/h2>\n<p>result = arr1 + arr2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2\u3001\u77e2\u91cf\u5316\u8ba1\u7b97<\/h4>\n<\/p>\n<p><p>NumPy\u7684\u77e2\u91cf\u5316\u8ba1\u7b97\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u4ee3\u7801\u7684\u6267\u884c\u6548\u7387\uff0c\u907f\u514d\u4f7f\u7528Python\u7684\u5faa\u73af\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = np.arange(1000000)<\/p>\n<h2><strong>\u77e2\u91cf\u5316\u8ba1\u7b97<\/strong><\/h2>\n<p>result = arr * 2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.3\u3001\u5185\u5b58\u5e03\u5c40\u4e0e\u89c6\u56fe<\/h4>\n<\/p>\n<p><p>NumPy\u5141\u8bb8\u5bf9\u6570\u7ec4\u8fdb\u884c\u5185\u5b58\u5e03\u5c40\u7684\u4fee\u6539\u548c\u89c6\u56fe\u64cd\u4f5c\uff0c\u8fd9\u5bf9\u4e8e\u9ad8\u6548\u6570\u636e\u5904\u7406\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u4fee\u6539\u5185\u5b58\u5e03\u5c40<\/strong><\/h2>\n<p>arr_c = np.ascontiguousarray(arr)<\/p>\n<p>print(arr_c.flags)<\/p>\n<h2><strong>\u521b\u5efa\u6570\u7ec4\u89c6\u56fe<\/strong><\/h2>\n<p>arr_view = arr.view()<\/p>\n<p>print(arr_view)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001NUMPY\u4e0e\u5176\u4ed6\u79d1\u5b66\u8ba1\u7b97\u5e93\u7684\u7ed3\u5408<\/h3>\n<\/p>\n<p><h4>5.1\u3001\u4e0eSciPy\u7684\u7ed3\u5408<\/h4>\n<\/p>\n<p><p>SciPy\u662f\u57fa\u4e8eNumPy\u6784\u5efa\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u7ea7\u7684\u79d1\u5b66\u8ba1\u7b97\u529f\u80fd\uff0c\u5982\u4f18\u5316\u3001\u79ef\u5206\u3001\u63d2\u503c\u3001FFT\u3001\u4fe1\u53f7\u5904\u7406\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scipy.optimize as opt<\/p>\n<h2><strong>\u4f7f\u7528SciPy\u8fdb\u884c\u4f18\u5316<\/strong><\/h2>\n<p>def func(x):<\/p>\n<p>    return x2 + 5*np.sin(x)<\/p>\n<p>result = opt.minimize(func, 0)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2\u3001\u4e0ePandas\u7684\u7ed3\u5408<\/h4>\n<\/p>\n<p><p>Pandas\u662f\u57fa\u4e8eNumPy\u6784\u5efa\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u64cd\u4f5c\u548c\u5206\u6790\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u64cd\u4f5c<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;A&#39;: [1, 2, 3],<\/p>\n<p>    &#39;B&#39;: [4, 5, 6],<\/p>\n<p>    &#39;C&#39;: [7, 8, 9]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.3\u3001\u4e0eMatplotlib\u7684\u7ed3\u5408<\/h4>\n<\/p>\n<p><p>Matplotlib\u662f\u4e00\u4e2a\u7ed8\u56fe\u5e93\uff0c\u53ef\u4ee5\u4e0eNumPy\u7ed3\u5408\u4f7f\u7528\uff0c\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u4f7f\u7528Matplotlib\u8fdb\u884c\u6570\u636e\u53ef\u89c6\u5316<\/strong><\/h2>\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>\u516d\u3001NUMPY\u7684\u6027\u80fd\u4f18\u5316<\/h3>\n<\/p>\n<p><h4>6.1\u3001\u4f7f\u7528\u77e2\u91cf\u5316\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u907f\u514d\u4f7f\u7528Python\u5faa\u73af\uff0c\u901a\u8fc7\u77e2\u91cf\u5316\u64cd\u4f5c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = np.arange(1000000)<\/p>\n<h2><strong>\u4f7f\u7528\u77e2\u91cf\u5316\u64cd\u4f5c<\/strong><\/h2>\n<p>result = arr * 2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.2\u3001\u9009\u62e9\u9002\u5f53\u7684\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u9002\u5f53\u7684\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\uff0c\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = np.array([1, 2, 3], dtype=np.int32)<\/p>\n<p>print(arr.dtype)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.3\u3001\u4f7f\u7528NumPy\u7684\u5185\u7f6e\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u5c3d\u91cf\u4f7f\u7528NumPy\u7684\u5185\u7f6e\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u7528C\u8bed\u8a00\u5b9e\u73b0\u7684\uff0c\u5177\u6709\u66f4\u9ad8\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr = np.arange(1000000)<\/p>\n<h2><strong>\u4f7f\u7528NumPy\u7684\u5185\u7f6e\u51fd\u6570<\/strong><\/h2>\n<p>result = np.sum(arr)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001NUMPY\u7684\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h3>\n<\/p>\n<p><h4>7.1\u3001\u6570\u7ec4\u5f62\u72b6\u4e0d\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c\u65f6\uff0c\u5e38\u5e38\u4f1a\u9047\u5230\u6570\u7ec4\u5f62\u72b6\u4e0d\u5339\u914d\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>reshape<\/code>\u51fd\u6570\u6765\u89e3\u51b3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr1 = np.array([1, 2, 3])<\/p>\n<p>arr2 = np.array([[4], [5], [6]])<\/p>\n<h2><strong>\u89e3\u51b3\u6570\u7ec4\u5f62\u72b6\u4e0d\u5339\u914d\u95ee\u9898<\/strong><\/h2>\n<p>result = arr1 + arr2.reshape(3,)<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>7.2\u3001\u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d<\/h4>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u6570\u7ec4\u64cd\u4f5c\u65f6\uff0c\u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\u4e5f\u4f1a\u5bfc\u81f4\u9519\u8bef\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>astype<\/code>\u51fd\u6570\u6765\u89e3\u51b3\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">arr1 = np.array([1, 2, 3])<\/p>\n<p>arr2 = np.array([4.0, 5.0, 6.0])<\/p>\n<h2><strong>\u89e3\u51b3\u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d\u95ee\u9898<\/strong><\/h2>\n<p>result = arr1.astype(np.float64) + arr2<\/p>\n<p>print(result)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001NUMPY\u7684\u672a\u6765\u53d1\u5c55\u4e0e\u8d8b\u52bf<\/h3>\n<\/p>\n<p><h4>8.1\u3001\u6027\u80fd\u4f18\u5316<\/h4>\n<\/p>\n<p><p>\u968f\u7740\u786c\u4ef6\u7684\u53d1\u5c55\uff0cNumPy\u5c06\u7ee7\u7eed\u4f18\u5316\u5176\u6027\u80fd\uff0c\u5145\u5206\u5229\u7528\u591a\u6838\u5904\u7406\u5668\u548cGPU\u52a0\u901f\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><h4>8.2\u3001\u4e0e<a href=\"https:\/\/docs.pingcode.com\/tag\/AI\" target=\"_blank\">\u4eba\u5de5\u667a\u80fd<\/a>\u7ed3\u5408<\/h4>\n<\/p>\n<p><p>NumPy\u5728\u4eba\u5de5\u667a\u80fd\u9886\u57df\u6709\u5e7f\u6cdb\u5e94\u7528\uff0c\u672a\u6765\u5c06\u8fdb\u4e00\u6b65\u4e0e\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u7ed3\u5408\uff0c\u63d0\u4f9b\u66f4\u9ad8\u6548\u7684\u6570\u636e\u5904\u7406\u548c\u8ba1\u7b97\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h4>8.3\u3001\u8de8\u5e73\u53f0\u53d1\u5c55<\/h4>\n<\/p>\n<p><p>NumPy\u5c06\u7ee7\u7eed\u5728\u4e0d\u540c\u5e73\u53f0\u4e0a\u8fdb\u884c\u4f18\u5316\u548c\u53d1\u5c55\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7528\u6237\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u5185\u5bb9\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u5927\u5bb6\u5bf9\u5982\u4f55\u5728Python\u4e2d\u5f15\u5165NumPy\u4ee5\u53ca\u5176\u5f3a\u5927\u7684\u529f\u80fd\u6709\u4e86\u66f4\u6df1\u5165\u7684\u4e86\u89e3\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5bf9\u5927\u5bb6\u5b66\u4e60\u548c\u4f7f\u7528NumPy\u6709\u6240\u5e2e\u52a9\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\u4f7f\u7528NumPy\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u5de5\u5177\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<code>pip install numpy<\/code>\u3002\u5982\u679c\u4f7f\u7528\u7684\u662fAnaconda\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7<code>conda install numpy<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u5728\u4f60\u7684Python\u4ee3\u7801\u4e2d\u5bfc\u5165NumPy\u3002<\/p>\n<p><strong>\u5f15\u5165NumPy\u540e\uff0c\u6211\u5e94\u8be5\u5982\u4f55\u4f7f\u7528\u5b83\u7684\u57fa\u672c\u529f\u80fd\uff1f<\/strong><br \/>\u5728\u6210\u529f\u5f15\u5165NumPy\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>import numpy as np<\/code>\u6765\u7b80\u5316\u540e\u7eed\u7684\u8c03\u7528\u3002NumPy\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u521b\u5efa\u6570\u7ec4\u3001\u6267\u884c\u6570\u5b66\u8fd0\u7b97\u3001\u5904\u7406\u591a\u7ef4\u6570\u7ec4\u7b49\u3002\u57fa\u672c\u4f7f\u7528\u793a\u4f8b\u5305\u62ec\uff1a\u521b\u5efa\u6570\u7ec4<code>arr = np.array([1, 2, 3])<\/code>\uff0c\u8fdb\u884c\u6570\u7ec4\u8fd0\u7b97<code>arr + 5<\/code>\uff0c\u4ee5\u53ca\u8ba1\u7b97\u6570\u7ec4\u7684\u5747\u503c<code>np.mean(arr)<\/code>\u3002<\/p>\n<p><strong>\u5982\u679c\u5728\u5f15\u5165NumPy\u65f6\u9047\u5230\u9519\u8bef\uff0c\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u9519\u8bef\u901a\u5e38\u4e0eNumPy\u7684\u5b89\u88c5\u6709\u5173\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5728\u6b63\u786e\u7684Python\u73af\u5883\u4e2d\u8fd0\u884c\u4ee3\u7801\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip show numpy<\/code>\u6765\u68c0\u67e5\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002\u5982\u679c\u4ecd\u7136\u9047\u5230\u95ee\u9898\uff0c\u5c1d\u8bd5\u91cd\u65b0\u5b89\u88c5NumPy\uff0c\u6216\u8005\u68c0\u67e5Python\u548cNumPy\u7684\u7248\u672c\u517c\u5bb9\u6027\u3002\u786e\u4fdd\u73af\u5883\u8def\u5f84\u8bbe\u7f6e\u6b63\u786e\u4e5f\u975e\u5e38\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528\u865a\u62df\u73af\u5883\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5f15\u5165NumPy\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u5b89\u88c5\u5e76\u901a\u8fc7import\u8bed\u53e5\u6765\u5f15\u5165\u3002NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66 [&hellip;]","protected":false},"author":3,"featured_media":1153242,"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\/1153235"}],"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=1153235"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153235\/revisions"}],"predecessor-version":[{"id":1153245,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1153235\/revisions\/1153245"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1153242"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1153235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1153235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1153235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}