{"id":232,"date":"2022-08-21T17:08:00","date_gmt":"2022-08-21T09:08:00","guid":{"rendered":"http:\/\/162.14.82.114\/?p=232"},"modified":"2022-08-23T21:52:22","modified_gmt":"2022-08-23T13:52:22","slug":"numpy%e5%9f%ba%e7%a1%80","status":"publish","type":"post","link":"http:\/\/162.14.82.114\/index.php\/232\/08\/21\/2022\/","title":{"rendered":"Numpy\u5b66\u4e60\u8bb0\u5f55\uff08\u4e00\uff09"},"content":{"rendered":"<h1>Numpy\u57fa\u7840<\/h1>\n<h2>\u5bfc\u5165\u5e93\u51fd\u6570<\/h2>\n<pre><code class=\"language-python\">import numpy as np <\/code><\/pre>\n<h2>NumPy Ndarray \u5bf9\u8c61<\/h2>\n<h3>ndarray \u5185\u90e8\u7531\u4ee5\u4e0b\u5185\u5bb9\u7ec4\u6210\uff1a<\/h3>\n<ul>\n<li>\u4e00\u4e2a\u6307\u5411\u6570\u636e\uff08\u5185\u5b58\u6216\u5185\u5b58\u6620\u5c04\u6587\u4ef6\u4e2d\u7684\u4e00\u5757\u6570\u636e\uff09\u7684\u6307\u9488\u3002<\/li>\n<li>\u6570\u636e\u7c7b\u578b\u6216 dtype\uff0c\u63cf\u8ff0\u5728\u6570\u7ec4\u4e2d\u7684\u56fa\u5b9a\u5927\u5c0f\u503c\u7684\u683c\u5b50\u3002<\/li>\n<li>\u4e00\u4e2a\u8868\u793a\u6570\u7ec4\u5f62\u72b6\uff08shape\uff09\u7684\u5143\u7ec4\uff0c\u8868\u793a\u5404\u7ef4\u5ea6\u5927\u5c0f\u7684\u5143\u7ec4\u3002<\/li>\n<li>\u4e00\u4e2a\u8de8\u5ea6\u5143\u7ec4\uff08stride\uff09\uff0c\u5176\u4e2d\u7684\u6574\u6570\u6307\u7684\u662f\u4e3a\u4e86\u524d\u8fdb\u5230\u5f53\u524d\u7ef4\u5ea6\u4e0b\u4e00\u4e2a\u5143\u7d20\u9700\u8981&quot;\u8de8\u8fc7&quot;\u7684\u5b57\u8282\u6570\u3002<\/li>\n<\/ul>\n<div align = \"center\">\n<img src = \"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208192053224.png\" >\n<\/div>\n<p>\u8de8\u5ea6\u53ef\u4ee5\u662f\u8d1f\u6570\uff0c\u8fd9\u6837\u4f1a\u4f7f\u6570\u7ec4\u5728\u5185\u5b58\u4e2d\u540e\u5411\u79fb\u52a8\uff0c\u5207\u7247\u4e2d <strong>obj[::-1]<\/strong> \u6216 <strong>obj[:,::-1]<\/strong> \u5c31\u662f\u5982\u6b64\u3002<\/p>\n<pre><code class=\"language-python\">numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)<\/code><\/pre>\n<div align = \"center\">\n<img src = \"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208192055249.png\" alt = \"temp5\">\n<\/div>\n<h3>\u6848\u4f8b<\/h3>\n<pre><code class=\"language-python\">import numpy as np \na = np.array([1,2,3])  \na\n# [1 2 3]<\/code><\/pre>\n<pre><code class=\"language-python\">import numpy as np \na = np.array([1, 2, 3, 4, 5], ndmin =  3)  \na \n#[[[1 2 3 4 5]]]<\/code><\/pre>\n<pre><code class=\"language-python\">import numpy as np \na = np.array([1,  2,  3], dtype = complex)  \na\n# array([1.+0.j, 2.+0.j, 3.+0.j])<\/code><\/pre>\n<h2>NumPy \u6570\u636e\u7c7b\u578b<\/h2>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208192102497.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208192102497.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20220819210226118\" style=\"zoom: 50%;\" \/><\/div><\/p>\n<h3>\u6570\u636e\u7c7b\u578b\u5bf9\u8c61 (dtype)<\/h3>\n<pre><code class=\"language-python\">numpy.dtype(object, align, copy)<\/code><\/pre>\n<ul>\n<li>object - \u8981\u8f6c\u6362\u4e3a\u7684\u6570\u636e\u7c7b\u578b\u5bf9\u8c61<\/li>\n<li>align - \u5982\u679c\u4e3a true\uff0c\u586b\u5145\u5b57\u6bb5\u4f7f\u5176\u7c7b\u4f3c C \u7684\u7ed3\u6784\u4f53\u3002<\/li>\n<li>copy - \u590d\u5236 dtype \u5bf9\u8c61 \uff0c\u5982\u679c\u4e3a false\uff0c\u5219\u662f\u5bf9\u5185\u7f6e\u6570\u636e\u7c7b\u578b\u5bf9\u8c61\u7684\u5f15\u7528<\/li>\n<\/ul>\n<p>\u5b57\u8282\u987a\u5e8f\u662f\u901a\u8fc7\u5bf9\u6570\u636e\u7c7b\u578b\u9884\u5148\u8bbe\u5b9a <strong>&lt;<\/strong> \u6216 <strong>&gt;<\/strong> \u6765\u51b3\u5b9a\u7684\u3002 <strong>&lt;<\/strong> \u610f\u5473\u7740\u5c0f\u7aef\u6cd5(\u6700\u5c0f\u503c\u5b58\u50a8\u5728\u6700\u5c0f\u7684\u5730\u5740\uff0c\u5373\u4f4e\u4f4d\u7ec4\u653e\u5728\u6700\u524d\u9762)\u3002<strong>&gt;<\/strong> \u610f\u5473\u7740\u5927\u7aef\u6cd5(\u6700\u91cd\u8981\u7684\u5b57\u8282\u5b58\u50a8\u5728\u6700\u5c0f\u7684\u5730\u5740\uff0c\u5373\u9ad8\u4f4d\u7ec4\u653e\u5728\u6700\u524d\u9762)\u3002<\/p>\n<h3>\u6848\u4f8b<\/h3>\n<pre><code class=\"language-python\">import numpy as np\ndt = np.dtype(np.int32)\ndt\n# dtype(&#039;int32&#039;)<\/code><\/pre>\n<pre><code class=\"language-python\">import numpy as np\n# int8, int16, int32, int64 \u56db\u79cd\u6570\u636e\u7c7b\u578b\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32 &#039;i1&#039;, &#039;i2&#039;,&#039;i4&#039;,&#039;i8&#039; \u4ee3\u66ff\ndt = np.dtype(&#039;i4&#039;)\ndt\n# dtype(&#039;int32&#039;)<\/code><\/pre>\n<pre><code class=\"language-python\">import numpy as np\n# \u5b57\u8282\u987a\u5e8f\u6807\u6ce8\ndt = np.dtype(&#039;&lt;i4&#039;)\ndt\n# dtype(&#039;int32&#039;)<\/code><\/pre>\n<pre><code class=\"language-python\">import numpy as np\n# \u521b\u5efa\u7ed3\u6784\u5316\u6570\u636e\u7c7b\u578b\ndt = np.dtype([(&#039;age&#039;,np.int8)]) \ndt\n# dtype([(&#039;age&#039;, &#039;i1&#039;)])<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5c06\u6570\u636e\u7c7b\u578b\u5e94\u7528\u4e8e ndarray \u5bf9\u8c61\nimport numpy as np\ndt = np.dtype([(&#039;age&#039;,np.int8)]) \na = np.array([(10,),(20,),(30,)], dtype = dt) \n# array([(10,), (20,), (30,)], dtype=[(&#039;age&#039;, &#039;i1&#039;)])\n# # \u7c7b\u578b\u5b57\u6bb5\u540d\u53ef\u4ee5\u7528\u4e8e\u5b58\u53d6\u5b9e\u9645\u7684 age \u5217\na[&#039;age&#039;]\n# array([10, 20, 30], dtype=int8)<\/code><\/pre>\n<p>\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\u5316\u6570\u636e\u7c7b\u578b student\uff0c\u5305\u542b\u5b57\u7b26\u4e32\u5b57\u6bb5 name\uff0c\u6574\u6570\u5b57\u6bb5 age\uff0c\u53ca\u6d6e\u70b9\u5b57\u6bb5 marks\uff0c\u5e76\u5c06\u8fd9\u4e2a dtype \u5e94\u7528\u5230 ndarray \u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"language-python\">dt = np.dtype([(&#039;name&#039;,&#039;S10&#039;),(&#039;age&#039;,&#039;i8&#039;),(&#039;class_id&#039;,&#039;i8&#039;)])\na = np.array([(&#039;a&#039;,16,1),(&#039;b&#039;,17,2),(&#039;c&#039;,18,3)],dtype = dt)\na\n# array([(b&#039;a&#039;, 16, 1), (b&#039;b&#039;, 17, 2), (b&#039;c&#039;, 18, 3)],\n#     dtype=[(&#039;name&#039;, &#039;S10&#039;), (&#039;age&#039;, &#039;&lt;i8&#039;), (&#039;class_id&#039;, &#039;&lt;i8&#039;)])<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208192131161.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208192131161.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20220819213133111\" style=\"zoom:50%;\" \/><\/div><\/p>\n<h2>NumPy \u6570\u7ec4\u5c5e\u6027<\/h2>\n<h3>\u77e5\u8bc6\u70b9<\/h3>\n<ul>\n<li>NumPy \u6570\u7ec4\u7684\u7ef4\u6570\u79f0\u4e3a\u79e9\uff08rank\uff09\uff0c\u79e9\u5c31\u662f\u8f74\u7684\u6570\u91cf\uff0c\u5373\u6570\u7ec4\u7684\u7ef4\u5ea6\uff0c\u4e00\u7ef4\u6570\u7ec4\u7684\u79e9\u4e3a 1\uff0c\u4e8c\u7ef4\u6570\u7ec4\u7684\u79e9\u4e3a 2\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/li>\n<li>\u6bcf\u4e00\u4e2a\u7ebf\u6027\u7684\u6570\u7ec4\u79f0\u4e3a\u662f\u4e00\u4e2a\u8f74\uff08axis\uff09\uff0c\u4e5f\u5c31\u662f\u7ef4\u5ea6\uff08dimensions\uff09\u3002\u6bd4\u5982\u8bf4\uff0c\u4e8c\u7ef4\u6570\u7ec4\u76f8\u5f53\u4e8e\u662f\u4e24\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u7b2c\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u53c8\u662f\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u3002\u6240\u4ee5\u4e00\u7ef4\u6570\u7ec4\u5c31\u662f NumPy \u4e2d\u7684\u8f74\uff08axis\uff09\uff0c\u7b2c\u4e00\u4e2a\u8f74\u76f8\u5f53\u4e8e\u662f\u5e95\u5c42\u6570\u7ec4\uff0c\u7b2c\u4e8c\u4e2a\u8f74\u662f\u5e95\u5c42\u6570\u7ec4\u91cc\u7684\u6570\u7ec4\u3002\u800c\u8f74\u7684\u6570\u91cf\u2014\u2014\u79e9\uff0c\u5c31\u662f\u6570\u7ec4\u7684\u7ef4\u6570\u3002<\/li>\n<li>\u5f88\u591a\u65f6\u5019\u53ef\u4ee5\u58f0\u660e axis\u3002axis=0\uff0c\u8868\u793a\u6cbf\u7740\u7b2c 0 \u8f74\u8fdb\u884c\u64cd\u4f5c\uff0c\u5373\u5bf9\u6bcf\u4e00\u5217\u8fdb\u884c\u64cd\u4f5c\uff1baxis=1\uff0c\u8868\u793a\u6cbf\u7740\u7b2c1\u8f74\u8fdb\u884c\u64cd\u4f5c\uff0c\u5373\u5bf9\u6bcf\u4e00\u884c\u8fdb\u884c\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208211706260.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208211706260.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20220819213347919\" style=\"zoom: 40%;\" \/><\/div><\/p>\n<h3>\u6848\u4f8b<\/h3>\n<pre><code class=\"language-python\"># \u8f6c\u7f6e\u6539\u53d8\u7ef4\u6570 ndarray.ndim\nimport numpy as np \na = np.arange(24)  \nprint (a.ndim)             \nb = a.reshape(2,4,3)  \nprint (b.ndim)\n# 1\n# 3<\/code><\/pre>\n<pre><code class=\"language-python\"># \u8c03\u6574\u6570\u7ec4\u5927\u5c0f ndarray.shape\na = np.array([[1,2,3],[4,5,6]])  \nprint (a.shape)\n# (2, 3)\na.shape =  (3,2)  \nprint(a)\n#[[1 2]\n# [3 4]\n# [5 6]]\n#reshape\u51fd\u6570\u4e5f\u53ef\u4ee5\u8c03\u6574\u6570\u7ec4\u5927\u5c0f\na = a.reshape(2,3)\na\n# array([[1, 2, 3],\n#        [4, 5, 6]])<\/code><\/pre>\n<pre><code class=\"language-python\"># \u4ee5\u5b57\u8282\u5f62\u5f0f\u8fd4\u56de\u6bcf\u4e2a\u5143\u7d20\u5927\u5c0f ndarray.itemsize\n# \u6570\u7ec4\u7684 dtype \u4e3a int8\uff08\u4e00\u4e2a\u5b57\u8282\uff09  \nx = np.array([1,2,3,4,5], dtype = np.int8)  \nprint (x.itemsize)\n# \u6570\u7ec4\u7684 dtype \u73b0\u5728\u4e3a float64\uff08\u516b\u4e2a\u5b57\u8282\uff09 \ny = np.array([1,2,3,4,5], dtype = np.float64)  \nprint (y.itemsize)\n# 1\n# 8<\/code><\/pre>\n<pre><code class=\"language-python\"># \u8fd4\u56de ndarray \u5bf9\u8c61\u7684\u5185\u5b58\u4fe1\u606f ndarray.flags\nx = np.array([1,2,3,4,5])  \nprint (x.flags)\n# C_CONTIGUOUS : True\n# F_CONTIGUOUS : True\n# OWNDATA : True\n# WRITEABLE : True\n# ALIGNED : True\n# WRITEBACKIFCOPY : False\n# UPDATEIFCOPY : False<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208211706176.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208211706176.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20220819214132866\" style=\"zoom: 40%;\" \/><\/div><\/p>\n<h2>NumPy \u521b\u5efa\u6570\u7ec4<\/h2>\n<h3>numpy.empty<\/h3>\n<pre><code class=\"language-python\">numpy.empty(shape, dtype = float, order = &#039;C&#039;)<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208211706559.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208211706559.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20220819214406997\" style=\"zoom:40%;\" \/><\/div><\/p>\n<h4>\u5b9e\u4f8b<\/h4>\n<pre><code class=\"language-python\">import numpy as np \nx = np.empty([3,2], dtype = int) \nprint (x)\n# [[         0 1072693248]\n#  [         0 1073741824]\n#  [         0 1074266112]]<\/code><\/pre>\n<h3>numpy.zeros<\/h3>\n<p>\u521b\u5efa\u6307\u5b9a\u5927\u5c0f\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u5143\u7d20\u4ee5 0 \u6765\u586b\u5145<\/p>\n<pre><code class=\"language-python\">numpy.zeros(shape, dtype = float, order = &#039;C&#039;)<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208211706893.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208211706893.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20220819214549035\" style=\"zoom:40%;\" \/><\/div><\/p>\n<h4>\u5b9e\u4f8b<\/h4>\n<pre><code class=\"language-python\"># \u9ed8\u8ba4\u4e3a\u6d6e\u70b9\u6570\nx = np.zeros(5) \nprint(x)\n# \u8bbe\u7f6e\u7c7b\u578b\u4e3a\u6574\u6570\ny = np.zeros((5,), dtype = int) \nprint(y) \n# \u81ea\u5b9a\u4e49\u7c7b\u578b\nz = np.zeros((2,2), dtype = [(&#039;x&#039;, &#039;i4&#039;), (&#039;y&#039;, &#039;i4&#039;)])  \nprint(z)\n# [0. 0. 0. 0. 0.]\n# [0 0 0 0 0]\n# [[(0, 0) (0, 0)]\n#  [(0, 0) (0, 0)]]<\/code><\/pre>\n<h3>numpy.ones<\/h3>\n<p>\u521b\u5efa\u6307\u5b9a\u5f62\u72b6\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u5143\u7d20\u4ee5 1 \u6765\u586b\u5145<\/p>\n<pre><code class=\"language-python\">numpy.ones(shape, dtype = None, order = &#039;C&#039;)<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208211707443.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/pic-for-be.oss-cn-hangzhou.aliyuncs.com\/img\/202208211707443.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20220819214802401\" style=\"zoom:40%;\" \/><\/div><\/p>\n<h4>\u5b9e\u4f8b<\/h4>\n<pre><code># \u9ed8\u8ba4\u4e3a\u6d6e\u70b9\u6570\nx = np.ones(5) \nprint(x)\n\n# \u81ea\u5b9a\u4e49\u7c7b\u578b\nx = np.ones([2,2], dtype = int)\nprint(x)\n# [1. 1. 1. 1. 1.]\n# [[1 1]\n#  [1 1]]<\/code><\/pre>\n<h2>NumPy \u4ece\u5df2\u6709\u7684\u6570\u7ec4\u521b\u5efa\u6570\u7ec4<\/h2>\n<h3>numpy.asarray()<\/h3>\n<pre><code class=\"language-python\">numpy.asarray(a, dtype = None, order = None)<\/code><\/pre>\n<ul>\n<li>a --&gt; \u4efb\u610f\u5f62\u5f0f\u7684\u8f93\u5165\u53c2\u6570\uff0c\u53ef\u4ee5\u662f\uff0c\u5217\u8868, \u5217\u8868\u7684\u5143\u7ec4, \u5143\u7ec4, \u5143\u7ec4\u7684\u5143\u7ec4, \u5143\u7ec4\u7684\u5217\u8868\uff0c\u591a\u7ef4\u6570\u7ec4<\/li>\n<li>dtype --&gt;\u6570\u636e\u7c7b\u578b<\/li>\n<li>order --&gt; \u53ef\u9009\uff0c\u6709&quot;C&quot;\u548c&quot;F&quot;\u4e24\u4e2a\u9009\u9879,\u5206\u522b\u4ee3\u8868\uff0c\u884c\u4f18\u5148\u548c\u5217\u4f18\u5148\uff0c\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\u7684\u5b58\u50a8\u5143\u7d20\u7684\u987a\u5e8f\u3002<\/li>\n<\/ul>\n<h4>\u5b9e\u4f8b<\/h4>\n<pre><code class=\"language-python\"># \u5c06\u5217\u8868\u8f6c\u6362\u4e3a ndarray\nimport numpy as np \nx =  [1,2,3] \na = np.asarray(x)  \na\n# [1 2 3]<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5c06\u5143\u7ec4\u8f6c\u6362\u4e3a ndarray\nx =  (1,2,3) \na = np.asarray(x)  \na\n# array([1, 2, 3])<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5c06\u5143\u7ec4\u5217\u8868\u8f6c\u6362\u4e3a ndarray\nx =  [(1,2,3),(4,5)] \na = np.asarray(x)  \na\n# array([(1, 2, 3), (4, 5)], dtype=object)<\/code><\/pre>\n<pre><code class=\"language-python\"># \u8bbe\u7f6e\u4e86 dtype \u53c2\u6570\nx =  [1,2,3] \na = np.asarray(x, dtype =  float)  \na<\/code><\/pre>\n<h3>numpy.frombuffer<\/h3>\n<p>numpy.frombuffer \u7528\u4e8e\u5b9e\u73b0\u52a8\u6001\u6570\u7ec4\u3002<\/p>\n<p>numpy.frombuffer \u63a5\u53d7 buffer \u8f93\u5165\u53c2\u6570\uff0c\u4ee5\u6d41\u7684\u5f62\u5f0f\u8bfb\u5165\u8f6c\u5316\u6210 ndarray \u5bf9\u8c61\u3002<\/p>\n<pre><code class=\"language-python\">numpy.frombuffer(buffer, dtype = float, count = -1, offset = 0)<\/code><\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong> <em>buffer \u662f\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0cPython3 \u9ed8\u8ba4 str \u662f Unicode \u7c7b\u578b\uff0c\u6240\u4ee5\u8981\u8f6c\u6210 bytestring \u5728\u539f str \u524d\u52a0\u4e0a b\u3002<\/em><\/p>\n<ul>\n<li><code>buffer<\/code> \u53ef\u4ee5\u662f\u4efb\u610f\u5bf9\u8c61\uff0c\u4f1a\u4ee5\u6d41\u7684\u5f62\u5f0f\u8bfb\u5165\u3002<\/li>\n<li><code>dtype<\/code> \u8fd4\u56de\u6570\u7ec4\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u9009<\/li>\n<li><code>count<\/code>\u8bfb\u53d6\u7684\u6570\u636e\u6570\u91cf\uff0c\u9ed8\u8ba4\u4e3a-1\uff0c\u8bfb\u53d6\u6240\u6709\u6570\u636e<\/li>\n<li><code>offset<\/code>\u8bfb\u53d6\u7684\u8d77\u59cb\u4f4d\u7f6e\uff0c\u9ed8\u8ba4\u4e3a0<\/li>\n<\/ul>\n<h4>\u5b9e\u4f8b<\/h4>\n<pre><code class=\"language-python\">import numpy as np \ns =  b&#039;Hello World&#039; \na = np.frombuffer(s, dtype =  &#039;S1&#039;)  \na\n# array([b&#039;H&#039;, b&#039;e&#039;, b&#039;l&#039;, b&#039;l&#039;, b&#039;o&#039;, b&#039; &#039;, b&#039;W&#039;, b&#039;o&#039;, b&#039;r&#039;, b&#039;l&#039;, b&#039;d&#039;],dtype=&#039;|S1&#039;)<\/code><\/pre>\n<h3>numpy.fromiter<\/h3>\n<p>numpy.fromiter \u65b9\u6cd5\u4ece\u53ef\u8fed\u4ee3\u5bf9\u8c61\u4e2d\u5efa\u7acb ndarray \u5bf9\u8c61\uff0c\u8fd4\u56de\u4e00\u7ef4\u6570\u7ec4\u3002<\/p>\n<pre><code class=\"language-python\">numpy.fromiter(iterable, dtype, count=-1)<\/code><\/pre>\n<ul>\n<li><code>iterable<\/code>\u53ef\u8fed\u4ee3\u5bf9\u8c61<\/li>\n<li><code>dtype<\/code>\u8fd4\u56de\u6570\u7ec4\u7684\u6570\u636e\u7c7b\u578b<\/li>\n<li><code>count<\/code>\u8bfb\u53d6\u7684\u6570\u636e\u6570\u91cf\uff0c\u9ed8\u8ba4\u4e3a-1\uff0c\u8bfb\u53d6\u6240\u6709\u6570\u636e<\/li>\n<\/ul>\n<pre><code class=\"language-python\">import numpy as np \n# \u4f7f\u7528 range \u51fd\u6570\u521b\u5efa\u5217\u8868\u5bf9\u8c61  \nlist=range(5)\nit=iter(list)\n# \u4f7f\u7528\u8fed\u4ee3\u5668\u521b\u5efa ndarray \nx=np.fromiter(it, dtype=float)\nx\n# array([0., 1., 2., 3., 4.])<\/code><\/pre>\n<h2>NumPy \u4ece\u6570\u503c\u8303\u56f4\u521b\u5efa\u6570\u7ec4<\/h2>\n<h3>numpy.arange<\/h3>\n<p>numpy \u5305\u4e2d\u7684\u4f7f\u7528 arange \u51fd\u6570\u521b\u5efa\u6570\u503c\u8303\u56f4\u5e76\u8fd4\u56de ndarray \u5bf9\u8c61<\/p>\n<pre><code class=\"language-python\">numpy.arange(start, stop, step, dtype)<\/code><\/pre>\n<h4>\u5b9e\u4f8b<\/h4>\n<pre><code class=\"language-python\"># \u751f\u6210 0 \u5230 5 \u7684\u6570\u7ec4\nimport numpy as np\nx = np.arange(5)  \nx\n# array([0, 1, 2, 3, 4])<\/code><\/pre>\n<pre><code class=\"language-python\">x = np.arange(5, dtype =  float)  \nx\narray([0., 1., 2., 3., 4.])<\/code><\/pre>\n<pre><code class=\"language-python\"># \u8bbe\u7f6e\u4e86\u8d77\u59cb\u503c\u3001\u7ec8\u6b62\u503c\u53ca\u6b65\u957f\nx = np.arange(10,20,2)  \nx\n# array([10, 12, 14, 16, 18])<\/code><\/pre>\n<h3>numpy.linspace<\/h3>\n<p>numpy.linspace \u51fd\u6570\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\uff0c\u6570\u7ec4\u662f\u4e00\u4e2a\u7b49\u5dee\u6570\u5217\u6784\u6210\u7684<\/p>\n<pre><code class=\"language-python\">np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)<\/code><\/pre>\n<ul>\n<li><code>start<\/code>          \u5e8f\u5217\u7684\u8d77\u59cb\u503c<\/li>\n<li><code>stop<\/code>           \u5e8f\u5217\u7684\u7ec8\u6b62\u503c\uff0c\u5982\u679c<code>endpoint<\/code>\u4e3a<code>true<\/code>\uff0c\u8be5\u503c\u5305\u542b\u4e8e\u6570\u5217\u4e2d<\/li>\n<li><code>num<\/code>             \u8981\u751f\u6210\u7684\u7b49\u6b65\u957f\u7684\u6837\u672c\u6570\u91cf\uff0c\u9ed8\u8ba4\u4e3a<code>50<\/code><\/li>\n<li><code>endpoint<\/code>     \u8be5\u503c\u4e3a <code>true<\/code> \u65f6\uff0c\u6570\u5217\u4e2d\u5305\u542b<code>stop<\/code>\u503c\uff0c\u53cd\u4e4b\u4e0d\u5305\u542b\uff0c\u9ed8\u8ba4\u662fTrue<\/li>\n<li><code>retstep<\/code>       \u5982\u679c\u4e3a True \u65f6\uff0c\u751f\u6210\u7684\u6570\u7ec4\u4e2d\u4f1a\u663e\u793a\u95f4\u8ddd\uff0c\u53cd\u4e4b\u4e0d\u663e\u793a<\/li>\n<li><code>dtype<\/code>         <code>ndarray<\/code> \u7684\u6570\u636e\u7c7b\u578b<\/li>\n<\/ul>\n<h4>\u5b9e\u4f8b<\/h4>\n<pre><code class=\"language-python\">import numpy as np\na = np.linspace(1,10,10)\na\n# array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5c06 endpoint \u8bbe\u4e3a false\uff0c\u4e0d\u5305\u542b\u7ec8\u6b62\u503c\uff1a\na = np.linspace(10, 20,  5, endpoint =  False)  \na\n# array([10., 12., 14., 16., 18.])<\/code><\/pre>\n<pre><code class=\"language-python\"># \u8bbe\u7f6e\u95f4\u8ddd\nimport numpy as np\na =np.linspace(1,10,10,retstep= True)\na\n# (array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]), 1.0)\n# \u62d3\u5c55\u4f8b\u5b50\nb =np.linspace(1,10,10).reshape([10,1])\nb\n# array([[ 1.],\n#        [ 2.],\n#        [ 3.],\n#        [ 4.],\n#        [ 5.],\n#        [ 6.],\n#        [ 7.],\n#        [ 8.],\n#        [ 9.],\n#        [10.]])<\/code><\/pre>\n<h3>numpy.logspace<\/h3>\n<pre><code class=\"language-python\">np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)<\/code><\/pre>\n<ul>\n<li>\n<p><code>start<\/code>       \u5e8f\u5217\u7684\u8d77\u59cb\u503c\u4e3a\uff1abase ** start<\/p>\n<\/li>\n<li>\n<p><code>stop<\/code>        \u5e8f\u5217\u7684\u7ec8\u6b62\u503c\u4e3a\uff1abase ** stop\u3002\u5982\u679c<code>endpoint<\/code>\u4e3a<code>true<\/code>\uff0c\u8be5\u503c\u5305\u542b\u4e8e\u6570\u5217\u4e2d<\/p>\n<\/li>\n<li>\n<p><code>num<\/code>          \u8981\u751f\u6210\u7684\u7b49\u6b65\u957f\u7684\u6837\u672c\u6570\u91cf\uff0c\u9ed8\u8ba4\u4e3a<code>50<\/code><\/p>\n<\/li>\n<li>\n<p><code>endpoint<\/code>  \u8be5\u503c\u4e3a <code>true<\/code> \u65f6\uff0c\u6570\u5217\u4e2d\u4e2d\u5305\u542b<code>stop<\/code>\u503c\uff0c\u53cd\u4e4b\u4e0d\u5305\u542b\uff0c\u9ed8\u8ba4\u662fTrue<\/p>\n<\/li>\n<li>\n<p><code>base<\/code>        \u5bf9\u6570 log \u7684\u5e95\u6570<\/p>\n<\/li>\n<li>\n<p><code>dtype<\/code>      <code>ndarray<\/code> \u7684\u6570\u636e\u7c7b\u578b<\/p>\n<\/li>\n<\/ul>\n<h4>\u5b9e\u4f8b<\/h4>\n<pre><code class=\"language-python\">import numpy as np\n# \u9ed8\u8ba4\u5e95\u6570\u662f 10\na = np.logspace(1.0,  2.0, num =  10)  \na\n# array([ 10.        ,  12.91549665,  16.68100537,  21.5443469 ,\n#         27.82559402,  35.93813664,  46.41588834,  59.94842503,\n#         77.42636827, 100.        ])<\/code><\/pre>\n<pre><code class=\"language-python\"># \u5c06\u5e95\u6570\u8bbe\u7f6e\u4e3a2\na = np.logspace(0,9,10,base=2)\na\n# array([  1.,   2.,   4.,   8.,  16.,  32.,  64., 128., 256., 512.])<\/code><\/pre>\n<h2>NumPy \u5207\u7247\u548c\u7d22\u5f15<\/h2>\n<p>ndarray \u6570\u7ec4\u53ef\u4ee5\u57fa\u4e8e 0 - n \u7684\u4e0b\u6807\u8fdb\u884c\u7d22\u5f15\uff0c\u5207\u7247\u5bf9\u8c61\u53ef\u4ee5\u901a\u8fc7\u5185\u7f6e\u7684 slice \u51fd\u6570\uff0c\u5e76\u8bbe\u7f6e start, stop \u53ca step \u53c2\u6570\u8fdb\u884c\uff0c\u4ece\u539f\u6570\u7ec4\u4e2d\u5207\u5272\u51fa\u4e00\u4e2a\u65b0\u6570\u7ec4<\/p>\n<pre><code class=\"language-python\">import numpy as np\na = np.arange(10)\ns = slice(2,7,2)   # \u4ece\u7d22\u5f15 2 \u5f00\u59cb\u5230\u7d22\u5f15 7 \u505c\u6b62\uff0c\u95f4\u9694\u4e3a2\n# \u4e0b\u9762\u8fd9\u79cd\u8868\u8fbe\u4e00\u6837\u53ef\u4ee5\u8fdb\u884c\u5207\u7247\n# b = a[2:7:2]   # \u4ece\u7d22\u5f15 2 \u5f00\u59cb\u5230\u7d22\u5f15 7 \u505c\u6b62\uff0c\u95f4\u9694\u4e3a 2\na[s]\n# array([2, 4, 6])<\/code><\/pre>\n<p>\u5192\u53f7 <strong>:<\/strong> \u7684\u89e3\u91ca\uff1a\u5982\u679c\u53ea\u653e\u7f6e\u4e00\u4e2a\u53c2\u6570\uff0c\u5982 <strong>[2]<\/strong>\uff0c\u5c06\u8fd4\u56de\u4e0e\u8be5\u7d22\u5f15\u76f8\u5bf9\u5e94\u7684\u5355\u4e2a\u5143\u7d20\u3002\u5982\u679c\u4e3a <strong>[2:]<\/strong>\uff0c\u8868\u793a\u4ece\u8be5\u7d22\u5f15\u5f00\u59cb\u4ee5\u540e\u7684\u6240\u6709\u9879\u90fd\u5c06\u88ab\u63d0\u53d6\u3002\u5982\u679c\u4f7f\u7528\u4e86\u4e24\u4e2a\u53c2\u6570\uff0c\u5982 <strong>[2:7]<\/strong>\uff0c\u90a3\u4e48\u5219\u63d0\u53d6\u4e24\u4e2a\u7d22\u5f15(\u4e0d\u5305\u62ec\u505c\u6b62\u7d22\u5f15)\u4e4b\u95f4\u7684\u9879<\/p>\n<p><strong>\u5207\u7247\u8fd8\u53ef\u4ee5\u5305\u62ec\u7701\u7565\u53f7 <\/strong>\u2026<strong>\uff0c\u6765\u4f7f\u9009\u62e9\u5143\u7ec4\u7684\u957f\u5ea6\u4e0e\u6570\u7ec4\u7684\u7ef4\u5ea6\u76f8\u540c\u3002 \u5982\u679c\u5728\u884c\u4f4d\u7f6e\u4f7f\u7528\u7701\u7565\u53f7\uff0c\u5b83\u5c06\u8fd4\u56de\u5305\u542b\u884c\u4e2d\u5143\u7d20\u7684 ndarray<\/strong><\/p>\n<pre><code class=\"language-python\">import numpy as np\na = np.array([[1,2,3],[3,4,5],[4,5,6]])  \na[...,1]\n# \u7b2c2\u5217\u5143\u7d20             array([2, 4, 5])\na[1,...]   \n# \u7b2c2\u884c\u5143\u7d20             array([3, 4, 5])\na[...,1:]  \n# \u7b2c2\u5217\u53ca\u5269\u4e0b\u7684\u6240\u6709\u5143\u7d20   array([4, 5, 6])<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Numpy\u57fa\u7840 \u5bfc\u5165\u5e93\u51fd\u6570 import numpy as np NumPy Ndarray \u5bf9\u8c61 ndarr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,32],"tags":[],"class_list":["post-232","post","type-post","status-publish","format-standard","hentry","category-artificial-intelligence","category-python-learn"],"_links":{"self":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":3,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":246,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/posts\/232\/revisions\/246"}],"wp:attachment":[{"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/162.14.82.114\/index.php\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}