title: Split a 3 dimensional numpy array into 3x3 grids
body: <p>I have a 3 dimensional numpy array. I want to split it into 3x3 grids. Is there a function in numpy that can do it?</p>
  
  <p>Let's say I have the following 3 dimensional array with shape (9, 9, 3)</p>
  
  <pre><code>array([[[  0,   1,   2],
          [  3,   4,   5],
          [  6,   7,   8],
          [  9,  10,  11],
          [ 12,  13,  14],
          [ 15,  16,  17],
          [ 18,  19,  20],
          [ 21,  22,  23],
          [ 24,  25,  26]],
  
         [[ 27,  28,  29],
          [ 30,  31,  32],
          [ 33,  34,  35],
          [ 36,  37,  38],
          [ 39,  40,  41],
          [ 42,  43,  44],
          [ 45,  46,  47],
          [ 48,  49,  50],
          [ 51,  52,  53]],
  
         [[ 54,  55,  56],
          [ 57,  58,  59],
          [ 60,  61,  62],
          [ 63,  64,  65],
          [ 66,  67,  68],
          [ 69,  70,  71],
          [ 72,  73,  74],
          [ 75,  76,  77],
          [ 78,  79,  80]],
  
         [[ 81,  82,  83],
          [ 84,  85,  86],
          [ 87,  88,  89],
          [ 90,  91,  92],
          [ 93,  94,  95],
          [ 96,  97,  98],
          [ 99, 100, 101],
          [102, 103, 104],
          [105, 106, 107]],
  
         [[108, 109, 110],
          [111, 112, 113],
          [114, 115, 116],
          [117, 118, 119],
          [120, 121, 122],
          [123, 124, 125],
          [126, 127, 128],
          [129, 130, 131],
          [132, 133, 134]],
  
         [[135, 136, 137],
          [138, 139, 140],
          [141, 142, 143],
          [144, 145, 146],
          [147, 148, 149],
          [150, 151, 152],
          [153, 154, 155],
          [156, 157, 158],
          [159, 160, 161]],
  
         [[162, 163, 164],
          [165, 166, 167],
          [168, 169, 170],
          [171, 172, 173],
          [174, 175, 176],
          [177, 178, 179],
          [180, 181, 182],
          [183, 184, 185],
          [186, 187, 188]],
  
         [[189, 190, 191],
          [192, 193, 194],
          [195, 196, 197],
          [198, 199, 200],
          [201, 202, 203],
          [204, 205, 206],
          [207, 208, 209],
          [210, 211, 212],
          [213, 214, 215]],
  
         [[216, 217, 218],
          [219, 220, 221],
          [222, 223, 224],
          [225, 226, 227],
          [228, 229, 230],
          [231, 232, 233],
          [234, 235, 236],
          [237, 238, 239],
          [240, 241, 242]]])
  </code></pre>
  
  <p>I want to split it into 9 3x3 grids each with shape (3,3,3)</p>
  
  <p>I have mentioned the first 3 of the 9 grids below.</p>
  
  <pre><code>array([[[ 0,  1,  2],
          [ 3,  4,  5],
          [ 6,  7,  8]],
  
         [[27, 28, 29],
          [30, 31, 32],
          [33, 34, 35]],
  
         [[54, 55, 56],
          [57, 58, 59],
          [60, 61, 62]]])
  
  array([[[9, 10, 11 ],
          [36, 37, 38 ],
          [63, 64, 65 ]],
  
         [[12, 13, 14 ],
          [39, 40, 41],
          [66, 67, 68]],
  
         [[15, 16, 17],
          [42, 43, 44],
          [69, 70, 71]]])
  
  array([[[18, 19, 20],
          [45, 46, 47],
          [72, 73, 74]],
  
         [[21, 22, 23],
          [48, 49, 50],
          [75, 76, 77]],
  
         [[24, 25, 26],
          [51, 52, 53],
          [78, 79, 80]]])
  </code></pre>
  
  <p>The logic of the split in the 2D is</p>
  
  <pre><code>R1C1 | R1C2 | R1C3| R1C4 | R1C5 | R1C6 | R1C7 | R1C8 | R1C9 |
  R2C1 | R2C2 | R2C3| R2C4 | R2C5 | R2C6 | R2C7 | R2C8 | R2C9 |
  R3C1 | R3C2 | R3C3| R3C4 | R3C5 | R3C6 | R3C7 | R3C8 | R3C9 |
  R4C1 | R4C2 | R4C3| R4C4 | R4C5 | R4C6 | R4C7 | R4C8 | R4C9 |
  R5C1 | R5C2 | R5C3| R5C4 | R5C5 | R5C6 | R5C7 | R5C8 | R5C9 |
  R6C1 | R6C2 | R6C3| R6C4 | R6C5 | R6C6 | R6C7 | R6C8 | R6C9 |
  R7C1 | R7C2 | R7C3| R7C4 | R7C5 | R7C6 | R7C7 | R7C8 | R7C9 |
  R8C1 | R8C2 | R8C3| R8C4 | R8C5 | R8C6 | R8C7 | R8C8 | R8C9 |
  R9C1 | R9C2 | R9C3| R9C4 | R9C5 | R9C6 | R9C7 | R9C8 | R9C9 |
  </code></pre>
  
  <p>I want the nine grids as follows</p>
  
  <pre><code>R1C1 | R1C2 | R1C3|
  R2C1 | R2C2 | R2C3|
  R3C1 | R3C2 | R3C3|
  
  R1C4 | R1C5 | R1C6 |
  R2C4 | R2C5 | R2C6 |
  R3C4 | R3C5 | R3C6 |
  
  R1C7 | R1C8 | R1C9 |
  R2C7 | R2C8 | R2C9 |
  R3C7 | R3C8 | R3C9 |
  
  R4C1 | R4C2 | R4C3|
  R5C1 | R5C2 | R5C3|
  R6C1 | R6C2 | R6C3|
  
  R4C4 | R4C5 | R4C6 |
  R5C4 | R5C5 | R5C6 |
  R6C4 | R6C5 | R6C6 |
  
   R4C7 | R4C8 | R4C9 |
   R5C7 | R5C8 | R5C9 |
   R6C7 | R6C8 | R6C9 |
  
  R7C1 | R7C2 | R7C3|
  R8C1 | R8C2 | R8C3|
  R9C1 | R9C2 | R9C3|
  
  R7C4 | R7C5 | R7C6 |
  R8C4 | R8C5 | R8C6 |
  R9C4 | R9C5 | R9C6 |
  
  R7C7 | R7C8 | R7C9 |
  R8C7 | R8C8 | R8C9 |
  R9C7 | R9C8 | R9C9 |
  </code></pre>
  
  <p>So far the only function that I found is <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.split.html" rel="nofollow">split</a>. But it only splits the array into sub arrays and not grids. I have to loop through the result and call <code>split</code> again with <code>axis=1</code>.</p>