{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "toc": true
   },
   "source": [
    "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
    "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#数组的合并\" data-toc-modified-id=\"数组的合并-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>数组的合并</a></span><ul class=\"toc-item\"><li><span><a href=\"#np.vstack-沿纵轴拼接\" data-toc-modified-id=\"np.vstack-沿纵轴拼接-1.1\"><span class=\"toc-item-num\">1.1&nbsp;&nbsp;</span>np.vstack 沿纵轴拼接</a></span></li><li><span><a href=\"#np.hstack-沿横轴拼接\" data-toc-modified-id=\"np.hstack-沿横轴拼接-1.2\"><span class=\"toc-item-num\">1.2&nbsp;&nbsp;</span>np.hstack 沿横轴拼接</a></span></li><li><span><a href=\"#指定拼接方向的np.concatenate()\" data-toc-modified-id=\"指定拼接方向的np.concatenate()-1.3\"><span class=\"toc-item-num\">1.3&nbsp;&nbsp;</span>指定拼接方向的np.concatenate()</a></span></li></ul></li><li><span><a href=\"#数组的分割\" data-toc-modified-id=\"数组的分割-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>数组的分割</a></span><ul class=\"toc-item\"><li><span><a href=\"#np.hsplit-横向进行分割\" data-toc-modified-id=\"np.hsplit-横向进行分割-2.1\"><span class=\"toc-item-num\">2.1&nbsp;&nbsp;</span>np.hsplit 横向进行分割</a></span></li><li><span><a href=\"#np.vsplit()横向进行分割\" data-toc-modified-id=\"np.vsplit()横向进行分割-2.2\"><span class=\"toc-item-num\">2.2&nbsp;&nbsp;</span>np.vsplit()横向进行分割</a></span></li><li><span><a href=\"#可指定方向的np.array_split()\" data-toc-modified-id=\"可指定方向的np.array_split()-2.3\"><span class=\"toc-item-num\">2.3&nbsp;&nbsp;</span>可指定方向的np.array_split()</a></span></li></ul></li></ul></div>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    " import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#全部行都能输出\n",
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = \"all\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数组的合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1, 2,3], [7, 8, 9]])\n",
    "b = np.array([[100, 101, 102], [103, 104, 105]])\n",
    "c = np.array([[0,0], [0, 0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "array([[100, 101, 102],\n",
       "       [103, 104, 105]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [0, 0]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a\n",
    "b\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## np.vstack 沿纵轴拼接"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**在纵向拼接, 增加的是行,列不变**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  7,   8,   9],\n",
       "       [100, 101, 102],\n",
       "       [103, 104, 105]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "all the input array dimensions except for the concatenation axis must match exactly",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-abd99944b1f4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvstack\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m   \u001b[1;31m#维度必须匹配\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mD:\\Anaconda3\\lib\\site-packages\\numpy\\core\\shape_base.py\u001b[0m in \u001b[0;36mvstack\u001b[1;34m(tup)\u001b[0m\n\u001b[0;32m    281\u001b[0m     \"\"\"\n\u001b[0;32m    282\u001b[0m     \u001b[0m_warn_for_nonsequence\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtup\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 283\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0m_nx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconcatenate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0matleast_2d\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_m\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0m_m\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mtup\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    284\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    285\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: all the input array dimensions except for the concatenation axis must match exactly"
     ]
    }
   ],
   "source": [
    "np.vstack((a,c))   #维度必须匹配"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## np.hstack 沿横轴拼接"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**在横向拼接, 增加的是列,行不变**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1, 2,3], [7, 8, 9]])\n",
    "b = np.array([[100, 101, 102], [103, 104, 105]])\n",
    "c = np.array([[0,0], [0, 0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "array([[100, 101, 102],\n",
       "       [103, 104, 105]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [0, 0]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a\n",
    "b\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3, 100, 101, 102],\n",
       "       [  7,   8,   9, 103, 104, 105]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((a,b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3, 0, 0],\n",
       "       [7, 8, 9, 0, 0]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((a,c))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 指定拼接方向的np.concatenate()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**参数axis=0默认在纵轴上拼接,axis=1横向拼接**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = np.array([[1, 2,3], [7, 8, 9]])\n",
    "b = np.array([[100, 101, 102], [103, 104, 105]])\n",
    "c = np.array([[0,0], [0, 0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "array([[100, 101, 102],\n",
       "       [103, 104, 105]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "array([[0, 0],\n",
       "       [0, 0]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a\n",
    "b\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  7,   8,   9],\n",
       "       [100, 101, 102],\n",
       "       [103, 104, 105]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((a,b), axis=0)   #axis=0默认在纵轴上拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3, 100, 101, 102],\n",
       "       [  7,   8,   9, 103, 104, 105]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((a,b), axis=1)   #axis=1在横轴上拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数组的分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14, 15, 16, 17, 18],\n",
       "       [19, 20, 21, 22, 23, 24, 25, 26, 27],\n",
       "       [28, 29, 30, 31, 32, 33, 34, 35, 36]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(1,37).reshape(4,9)\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## np.hsplit 横向进行分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 1,  2,  3],\n",
       "        [10, 11, 12],\n",
       "        [19, 20, 21],\n",
       "        [28, 29, 30]]), array([[ 4,  5,  6],\n",
       "        [13, 14, 15],\n",
       "        [22, 23, 24],\n",
       "        [31, 32, 33]]), array([[ 7,  8,  9],\n",
       "        [16, 17, 18],\n",
       "        [25, 26, 27],\n",
       "        [34, 35, 36]])]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.hsplit(a,3)    # 第二个参数只写一个整数时,会在横向进行平均分割\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3]\n",
      " [10 11 12]\n",
      " [19 20 21]\n",
      " [28 29 30]]\n",
      "******************************\n",
      "[[ 4  5  6]\n",
      " [13 14 15]\n",
      " [22 23 24]\n",
      " [31 32 33]]\n",
      "******************************\n",
      "[[ 7  8  9]\n",
      " [16 17 18]\n",
      " [25 26 27]\n",
      " [34 35 36]]\n",
      "******************************\n"
     ]
    }
   ],
   "source": [
    "for i in b:\n",
    "    print(i)\n",
    "    print('*'*30)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14, 15, 16, 17, 18],\n",
       "       [19, 20, 21, 22, 23, 24, 25, 26, 27],\n",
       "       [28, 29, 30, 31, 32, 33, 34, 35, 36]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 1,  2,  3],\n",
       "        [10, 11, 12],\n",
       "        [19, 20, 21],\n",
       "        [28, 29, 30]]), array([[ 4,  5],\n",
       "        [13, 14],\n",
       "        [22, 23],\n",
       "        [31, 32]]), array([[ 6,  7,  8,  9],\n",
       "        [15, 16, 17, 18],\n",
       "        [24, 25, 26, 27],\n",
       "        [33, 34, 35, 36]])]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(a,(3,5))   #在第三列和第五列的后面划一刀"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 1,  2,  3],\n",
       "        [10, 11, 12],\n",
       "        [19, 20, 21],\n",
       "        [28, 29, 30]]), array([[ 4,  5],\n",
       "        [13, 14],\n",
       "        [22, 23],\n",
       "        [31, 32]]), array([[ 6],\n",
       "        [15],\n",
       "        [24],\n",
       "        [33]]), array([[ 7],\n",
       "        [16],\n",
       "        [25],\n",
       "        [34]]), array([[ 8,  9],\n",
       "        [17, 18],\n",
       "        [26, 27],\n",
       "        [35, 36]])]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(a,(3,5,6,7)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## np.vsplit()横向进行分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14, 15, 16, 17, 18],\n",
       "       [19, 20, 21, 22, 23, 24, 25, 26, 27],\n",
       "       [28, 29, 30, 31, 32, 33, 34, 35, 36]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[1, 2, 3, 4, 5, 6, 7, 8, 9]]),\n",
       " array([[10, 11, 12, 13, 14, 15, 16, 17, 18]]),\n",
       " array([[19, 20, 21, 22, 23, 24, 25, 26, 27]]),\n",
       " array([[28, 29, 30, 31, 32, 33, 34, 35, 36]])]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(a,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[1, 2, 3, 4, 5, 6, 7, 8, 9]]),\n",
       " array([[10, 11, 12, 13, 14, 15, 16, 17, 18],\n",
       "        [19, 20, 21, 22, 23, 24, 25, 26, 27]]),\n",
       " array([[28, 29, 30, 31, 32, 33, 34, 35, 36]])]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(a,(1,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 可指定方向的np.array_split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14, 15, 16, 17, 18],\n",
       "       [19, 20, 21, 22, 23, 24, 25, 26, 27],\n",
       "       [28, 29, 30, 31, 32, 33, 34, 35, 36]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[1, 2, 3, 4, 5, 6, 7, 8, 9]]),\n",
       " array([[10, 11, 12, 13, 14, 15, 16, 17, 18],\n",
       "        [19, 20, 21, 22, 23, 24, 25, 26, 27]]),\n",
       " array([[28, 29, 30, 31, 32, 33, 34, 35, 36]])]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array_split(a,(1,3),axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 1],\n",
       "        [10],\n",
       "        [19],\n",
       "        [28]]), array([[ 2,  3],\n",
       "        [11, 12],\n",
       "        [20, 21],\n",
       "        [29, 30]]), array([[ 4,  5,  6,  7,  8,  9],\n",
       "        [13, 14, 15, 16, 17, 18],\n",
       "        [22, 23, 24, 25, 26, 27],\n",
       "        [31, 32, 33, 34, 35, 36]])]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array_split(a,(1,3),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": true,
   "toc_position": {
    "height": "523.537px",
    "left": "0px",
    "top": "110.284px",
    "width": "269.813px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "toc-autonumbering": true
 },
 "nbformat": 4,
 "nbformat_minor": 2
}