Time for action – splitting arrays

The following steps demonstrate arrays splitting:

  1. Horizontal splitting: The ensuing code splits an array along its horizontal axis into three pieces of the same size and shape:
    In: a
    Out:
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    In: hsplit(a, 3)
    Out:
    [array([[0],
           [3],
           [6]]),
     array([[1],
           [4],
           [7]]),
     array([[2],
           [5],
           [8]])]
    

    Compare it with a call of the split() function, with extra parameter axis=1:

    In: split(a, 3, axis=1)
    Out:
    [array([[0],
           [3],
           [6]]),
     array([[1],
           [4],
           [7]]),
     array([[2],
           [5],
           [8]])]
    
  2. Vertical splitting: vsplit() splits along the vertical axis:
    In: vsplit(a, 3)
    Out: [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
    

    The split() function, with axis=0, also splits along the ...

Get NumPy : Beginner's Guide - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.