{"id":3628,"date":"2022-03-18T11:30:27","date_gmt":"2022-03-18T03:30:27","guid":{"rendered":"https:\/\/egonlin.com\/?p=3628"},"modified":"2022-03-18T11:30:27","modified_gmt":"2022-03-18T03:30:27","slug":"%e7%ac%ac%e4%ba%8c%e8%8a%82%ef%bc%9anumpy%e7%bb%83%e4%b9%a0%e9%a2%98","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=3628","title":{"rendered":"\u7b2c\u4e8c\u8282\uff1anumpy\u7ec3\u4e60\u9898"},"content":{"rendered":"<h2>numpy\u7ec3\u4e60<\/h2>\n<pre><code class=\"language-python\">import numpy as np\n\n# 1\u3001\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a10\u7684\u7a7a\u5411\u91cf\n\n# 2\u3001\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a10\u7684\u7a7a\u5411\u91cf\uff0c\u4f46\u662f\u7b2c\u4e94\u4e2a\u503c\u4e3a1\n\n# 3\u3001\u521b\u5efa\u4e00\u4e2a10-49\u7684ndarray\u6570\u7ec4\n\n# 4\u3001\u521b\u5efa\u4e00\u4e2a3x3\u7684\u77e9\u9635\uff0c\u5176\u503c\u57280-8\u4e4b\u95f4\n\n# 5\u3001\u521b\u5efa\u5305\u542b\u968f\u673a\u503c\u768410x10\u6570\u7ec4\uff0c\u5e76\u4e14\u627e\u5230\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\n\n# 6\u3001\u521b\u5efa\u5927\u5c0f\u4e3a30\u7684\u968f\u673a\u5411\u91cf\uff0c\u627e\u5230\u5e73\u5747\u503c\n\n# 7\u3001\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a100\u7684\u968f\u673a\u6570\u7ec4\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f\n\n# 8\u3001\u5c06\u4e00\u4e2afloat32\u7684\u6570\u7ec4\u8f6c\u6362\u4e3aint32\u4f4d\u7684\u6570\u7ec4\n\n# 9\u3001\u5982\u4f55\u4ea4\u6362\u4e24\u884c\u6570\u7ec4\n\n# 10\u3001\u521b\u5efa\u4e00\u4e2a\u8fb9\u754c\u503c\u4e3a1\u800c\u5185\u90e8\u90fd\u4e3a0\u7684\u6570\u7ec4<\/code><\/pre>\n<h4>1\u3001\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a10\u7684\u7a7a\u5411\u91cf<\/h4>\n<pre><code class=\"language-python\">arr = np.zeros(10)\narr\n> array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])<\/code><\/pre>\n<h4>2\u3001\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a10\u7684\u7a7a\u5411\u91cf\uff0c\u4f46\u662f\u7b2c\u4e94\u4e2a\u503c\u4e3a1<\/h4>\n<pre><code class=\"language-python\">arr = np.zeros(10)\narr[4] = 1\narr\n> array([0., 0., 0., 0., 1., 0., 0., 0., 0., 0.])<\/code><\/pre>\n<h4>3\u3001\u521b\u5efa\u4e00\u4e2a10-49\u7684ndarray\u6570\u7ec4<\/h4>\n<pre><code class=\"language-python\">arr = np.arange(10,50)\narr\n> array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n       27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,\n       44, 45, 46, 47, 48, 49])<\/code><\/pre>\n<h4>4\u3001\u521b\u5efa\u4e00\u4e2a3&#215;3\u7684\u77e9\u9635\uff0c\u5176\u503c\u57280-8\u4e4b\u95f4<\/h4>\n<pre><code class=\"language-python\">arr = np.arange(9).reshape(3,3)\narr\n> array([[0, 1, 2],\n       [3, 4, 5],\n       [6, 7, 8]])<\/code><\/pre>\n<h4>5\u3001\u521b\u5efa\u5305\u542b\u968f\u673a\u503c\u768410&#215;10\u6570\u7ec4\uff0c\u5e76\u4e14\u627e\u5230\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c<\/h4>\n<pre><code class=\"language-python\">arr = np.random.random((10,10))\nzmin,zmax = arr.min(),arr.max()\nprint(zmin,zmax)\n> 0.011169609398906433 0.9911674728361876<\/code><\/pre>\n<h4>6\u3001\u521b\u5efa\u5927\u5c0f\u4e3a30\u7684\u968f\u673a\u5411\u91cf\uff0c\u627e\u5230\u5e73\u5747\u503c<\/h4>\n<pre><code class=\"language-python\">arr = [np.random.randint(30) for _ in range(30)]\nnp.mean(arr)\n> 13.833333333333334<\/code><\/pre>\n<h4>7\u3001\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a100\u7684\u968f\u673a\u6570\u7ec4\uff0c\u5e76\u5bf9\u5176\u8fdb\u884c\u6392\u5e8f<\/h4>\n<pre><code class=\"language-python\">arr = [np.random.randint(100) for _ in range(100)]\narr.sort()\narr\n> [0,\n 1,\n 4,\n 5,\n 5,\n...\n 93,\n 97,\n 97,\n 99,\n 99]<\/code><\/pre>\n<h4>8\u3001\u5c06\u4e00\u4e2afloat32\u7684\u6570\u7ec4\u8f6c\u6362\u4e3aint32\u4f4d\u7684\u6570\u7ec4<\/h4>\n<pre><code class=\"language-python\">Z = np.arange(10,dtype=np.float32)\nZ = Z.astype(np.int32)\nZ.dtype\n> dtype(&#039;int32&#039;)<\/code><\/pre>\n<h4>9\u3001\u5982\u4f55\u4ea4\u6362\u4e24\u884c\u6570\u7ec4<\/h4>\n<pre><code class=\"language-python\">arr = np.arange(25).reshape(5, 5)\narr[[ 0, 1 ]] = arr[[ 1, 0 ]]\narr\n> array([[ 5,  6,  7,  8,  9],\n       [ 0,  1,  2,  3,  4],\n       [10, 11, 12, 13, 14],\n       [15, 16, 17, 18, 19],\n       [20, 21, 22, 23, 24]])<\/code><\/pre>\n<h4>10\u3001\u521b\u5efa\u4e00\u4e2a\u8fb9\u754c\u503c\u4e3a1\u800c\u5185\u90e8\u90fd\u4e3a0\u7684\u6570\u7ec4<\/h4>\n<pre><code class=\"language-python\">Z = np.ones((10, 10))\nZ[1:9, 1:9] = 0\nZ\n> array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n       [1., 0., 0., 0., 0., 0., 0., 0., 0., 1.],\n       [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])<\/code><\/pre>\n<p>\u200b    <\/p>\n","protected":false},"excerpt":{"rendered":"<p>numpy\u7ec3\u4e60 import numpy as np # 1\u3001\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a10\u7684\u7a7a\u5411\u91cf # 2\u3001\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[313,310],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/3628"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3628"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/3628\/revisions"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}