{"id":2093,"date":"2023-04-01T22:04:07","date_gmt":"2023-04-01T14:04:07","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=2093"},"modified":"2023-04-06T13:58:27","modified_gmt":"2023-04-06T05:58:27","slug":"common-operations-of-numpy-include-min-max-mean-sum-exp-sqrt-sort-multiplication-dot-product-concatenation-and-segmentation","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/common-operations-of-numpy-include-min-max-mean-sum-exp-sqrt-sort-multiplication-dot-product-concatenation-and-segmentation\/","title":{"rendered":"NumPy\u5e38\u89c1\u8fd0\u7b97\u4e4bmin\u3001max\u3001mean\u3001sum\u3001exp\u3001sqrt\u3001sort\u3001\u4e58\u6cd5\u3001\u70b9\u79ef\u3001\u62fc\u63a5\u3001\u5207\u5206"},"content":{"rendered":"<h2>\u57fa\u672c\u8fd0\u7b97<\/h2>\n<ul>\n<li>ndarray.min() \/ np.min(ndarray)<\/li>\n<li>ndarray.max() \/ np.max(ndarray)<\/li>\n<li>ndarray.mean() \/\u00a0np.mean(ndarray)<\/li>\n<li>ndarray.sum() \/\u00a0np.sum(ndarray)<\/li>\n<li>ndarray.argmax() \u6700\u5927\u503c\u7684\u7d22\u5f15<\/li>\n<li>np.exp()<\/li>\n<li>np.sqrt() \u5f00\u6839\u53f7<\/li>\n<li>np.sort() \u6392\u5e8f<\/li>\n<li>np.argsort() \u6392\u5e8f\u7684\u7d22\u5f15<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<pre><code class=\"language-python\">def numpy():\n    n = np.array([[5, 10, 15],\n                  [20, 25, 30],\n                  [35, 40, 45]])\n    print(n.min())  # 5\n    print(n.max())  # 45\n    print(n.mean())  # 25.0\n    print(n.sum())  # 225\n    # \u6307\u5b9a\u6240\u64cd\u4f5c\u7684\u7ef4\u5ea6\uff0caxis=0 \u6309\u5217\uff0caxis=1 \u6309\u884c\n    print(n.sum(axis=0))  # [60 75 90]\n    print(n.sum(axis=1))  # [ 30  75 120]\n\n    print(&#039;- &#039; * 20)\n    n1 = np.arange(3)\n    print(n1)  # [0 1 2]\n    print(np.exp(n1))  # [1.         2.71828183 7.3890561 ]\n    print(np.sqrt(n1))  # [0.         1.         1.41421356]\n\n    print(&#039;- &#039; * 20)\n    n2 = np.random.random((2, 3)) * 10\n    n3 = np.floor(n2)\n    print(n2)\n    &#039;&#039;&#039;\n    [[8.99880764 4.00463427 0.419855  ]\n    [6.25824191 3.02688928 3.98404113]]\n    &#039;&#039;&#039;\n    print(n3)\n    &#039;&#039;&#039;\n    [[8. 4. 0.]\n    [6. 3. 3.]]\n    &#039;&#039;&#039;\n\n    print(&#039;- &#039; * 20)\n    a = np.array([[4, 3, 5], [1, 6, 1]])\n    print(a)\n    a1 = np.sort(a, axis=1)\n    print(a1)\n    &#039;&#039;&#039;\n    [[3 4 5]\n    [1 1 6]]\n    &#039;&#039;&#039;\n    a.sort(axis=1)\n    print(a)\n    &#039;&#039;&#039;\n    [[3 4 5]\n    [1 1 6]]\n    &#039;&#039;&#039;\n\n    print(&#039;- &#039; * 20)\n    b = np.array([4, 3, 1, 2])\n    i = np.argsort(b)\n    print(i)  # [2 3 1 0]\n    print(b[i])  # [1 2 3 4]<\/code><\/pre>\n<h2>\u77e9\u9635\u8fd0\u7b97<\/h2>\n<p>\uff081\uff09\u77e9\u9635\u4e58\u6cd5<\/p>\n<ul>\n<li>ndarray1 * ndarray2<\/li>\n<li>ndarray1.dot(ndarray2)<\/li>\n<li>np.dot(ndarray1, ndarray2)<\/li>\n<\/ul>\n<p>\uff082\uff09\u62fc\u63a5 ndarray \u5bf9\u8c61<\/p>\n<ul>\n<li>np.hstack()  # \u6a2a\u7740\u62fc\u63a5<\/li>\n<li>np.vstack()  # \u7ad6\u7740\u62fc\u63a5<\/li>\n<\/ul>\n<p>\uff083\uff09\u5207\u5206 ndarray \u5bf9\u8c61<\/p>\n<ul>\n<li>np.hsplit() # \u6a2a\u7740\u5207<\/li>\n<li>np.vsplit() # \u7ad6\u7740\u5207<\/li>\n<\/ul>\n<pre><code class=\"language-python\">def numpyMatrix():\n    n1 = np.array([[1, 1],\n                   [0, 1]])\n    n2 = np.array([[2, 0],\n                   [3, 4]])\n    print(n1 * n2)\n    &#039;&#039;&#039;\n    [[2 0]\n    [0 4]]\n    &#039;&#039;&#039;\n    print(n1.dot(n2))\n    &#039;&#039;&#039;\n    [[5 4]\n    [3 4]]\n    &#039;&#039;&#039;\n    print(np.dot(n1, n2))\n    &#039;&#039;&#039;\n    [[5 4]\n    [3 4]]\n    &#039;&#039;&#039;\n    n1 = np.array([[1, 2],\n                   [3, 4]])\n    n2 = np.array([[5, 6],\n                   [7, 8]])\n\n    hstack = np.hstack((n1, n2))  # \u6a2a\u7740\u62fc\u63a5\n    print(hstack)\n    &#039;&#039;&#039;\n    [[1 2 5 6]\n    [3 4 7 8]]\n    &#039;&#039;&#039;\n    vstack = np.vstack((n1, n2))  # \u7ad6\u7740\u62fc\u63a5\n    print(vstack)\n    &#039;&#039;&#039;\n    [[1 2]\n     [3 4]\n     [5 6]\n     [7 8]]\n    &#039;&#039;&#039;\n\n    print(&#039;- &#039; * 20)\n    a = np.floor(10 * np.random.random((2, 12)))\n    print(a)\n    &#039;&#039;&#039;\n    [[9. 8. 4. 4. 5. 1. 2. 0. 7. 0. 7. 8.]\n    [9. 4. 3. 5. 5. 5. 0. 4. 9. 1. 2. 3.]]\n    &#039;&#039;&#039;\n    print(np.hsplit(a, 3))  # \u6a2a\u7740\u5207\n    &#039;&#039;&#039;\n    [array([[9., 8., 4., 4.],\n           [9., 4., 3., 5.]]), array([[5., 1., 2., 0.],\n           [5., 5., 0., 4.]]), array([[7., 0., 7., 8.],\n           [9., 1., 2., 3.]])]\n    &#039;&#039;&#039;\n    print(np.hsplit(a, (3, 5)))  # \u6307\u5b9a\u5207\u5206\u4f4d\u7f6e\n    &#039;&#039;&#039;\n    [array([[9., 8., 4.],\n           [9., 4., 3.]]), array([[4., 5.],\n           [5., 5.]]), array([[1., 2., 0., 7., 0., 7., 8.],\n           [5., 0., 4., 9., 1., 2., 3.]])]\n    &#039;&#039;&#039;\n    b = a.T\n    print(b)\n    &#039;&#039;&#039;\n    [[9. 9.]\n     [8. 4.]\n     [4. 3.]\n     [4. 5.]\n     [5. 5.]\n     [1. 5.]\n     [2. 0.]\n     [0. 4.]\n     [7. 9.]\n     [0. 1.]\n     [7. 2.]\n     [8. 3.]]\n    &#039;&#039;&#039;\n    print(np.vsplit(b, 3))  # \u7ad6\u7740\u5207\n    &#039;&#039;&#039;\n    [array([[9., 9.],\n           [8., 4.],\n           [4., 3.],\n           [4., 5.]]), array([[5., 5.],\n           [1., 5.],\n           [2., 0.],\n           [0., 4.]]), array([[7., 9.],\n           [0., 1.],\n           [7., 2.],\n           [8., 3.]])]\n    &#039;&#039;&#039;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u8fd0\u7b97 ndarray.min() \/ np.min(ndarray) ndarray.max() \/ np [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[527],"tags":[528],"class_list":["post-2093","post","type-post","status-publish","format-standard","hentry","category-ai","tag-numpy"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2093"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/2093\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}