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 vertical axis:

    In: split(a, 3, axis=0)
    Out: [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
    
  3. Depth-wise splitting: The dsplit() function, unsurprisingly, splits depth-wise. Create an array of rank 3 first before splitting:
    In: c = arange(27).reshape(3, 3, 3)
    In: c
    Out:
    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]]])
    In: dsplit(c, 3)
    Out:
    [array([[[ 0],
            [ 3],
            [ 6]],
           [[ 9],
            [12],
            [15]],
           [[18],
            [21],
            [24]]]),
     array([[[ 1],
            [ 4],
            [ 7]],
           [[10],
            [13],
            [16]],
           [[19],
            [22],
            [25]]]),
     array([[[ 2],
            [ 5],
            [ 8]],
           [[11],
            [14],
            [17]],
           [[20],
            [23],
            [26]]])]
    

What just happened?

We split arrays using the hsplit(), vsplit(), dsplit(), and split() functions. These functions differ in the axis along which the split occurs. The code for this example is in the splitting.py file in this book's code bundle.

Array attributes

Besides the shape and dtype attributes, ndarray has a number of other attributes, as shown in the following list:

  • The ndim attribute gives the number of dimensions:
    In: b
    Out:
    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]])
    In: b.ndim
    Out: 2
    
  • The size attribute contains the number of elements. This is shown as follows:
    In: b.size
    Out: 24
    
  • The itemsize attribute gives the number of bytes for each element in the array:
    In: b.itemsize
    Out: 8
    
  • If you want the total number of bytes the array requires, you can have a look at nbytes. This is just a product of the itemsize and size attributes:
    In: b.nbytes
    Out: 192
    In: b.size * b.itemsize
    Out: 192
    
  • The T attribute has the same effect as the transpose() function, which is shown as follows:
    In: b.resize(6,4)
    In: b
    Out:
    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]])
    In: b.T
    Out:
    array([[ 0,  4,  8, 12, 16, 20],
           [ 1,  5,  9, 13, 17, 21],
           [ 2,  6, 10, 14, 18, 22],
           [ 3,  7, 11, 15, 19, 23]])
    
  • If the array has a rank lower than 2, we will just get a view of the array:
    In: b.ndim
    Out: 1
    In: b.T
    Out: array([0, 1, 2, 3, 4])
    

    Complex numbers in NumPy are represented by j. For example, create an array with complex numbers as in the following code:

    In: b = array([1.j + 1, 2.j + 3])
    In: b
    Out: array([ 1.+1.j,  3.+2.j])
    
  • The real attribute gives us the real part of the array, or the array itself if it only contains real numbers:
    In: b.real
    Out: array([ 1.,  3.])
    
  • The imag attribute contains the imaginary part of the array:
    In: b.imag
    Out: array([ 1.,  2.])
    
  • If the array contains complex numbers, then the data type is automatically also complex:
    In: b.dtype
    Out: dtype('complex128')
    In: b.dtype.str
    Out: '<c16'
    
  • The flat attribute returns a numpy.flatiter object. This is the only way to acquire a flatiter—we do not have access to a flatiter constructor. The flat iterator enables us to loop through an array as if it is a flat array, as shown in the following example:
    In: b = arange(4).reshape(2,2)
    In: b
    Out:
    array([[0, 1],
           [2, 3]])
    In: f = b.flat
    In: f
    Out: <numpy.flatiter object at 0x103013e00>
    In: for item in f: print item
       .....:
    0
    1
    2
    3
    

    It is possible to get an element directly with the flatiter object:

    In: b.flat[2]
    Out: 2
    

    And, it is also possible to directly get multiple elements:

    In: b.flat[[1,3]]
    Out: array([1, 3])
    

    The flat attribute is settable. Setting the value of the flat attribute leads to overwriting the values of the whole array:

    In: b.flat = 7
    In: b
    Out:
    array([[7, 7],
           [7, 7]])
    

    Or, it can also lead to overwriting the values of selected elements:

    In: b.flat[[1,3]] = 1
    In: b
    Out:
    array([[7, 1],
           [7, 1]])
    

    The following diagram shows the different types of attributes of the ndarray class:

    Array attributes
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset