Time for action – manipulating array shapes

We already learned about the reshape() function. Another recurring task is flattening of arrays. When we flatten multidimensional NumPy arrays, the result is a one-dimensional array with the same data.

  1. Ravel: Accomplish this with the ravel() function:
    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.ravel()
    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])
    
  2. Flatten: The appropriately named function, flatten() does the same as ravel(), but flatten() always allocates new memory whereas ravel() might return a view of the array. A view is a way to share an array, but you need to be careful with views because modifying the view affects the underlying array, and therefore this impacts other views. An array copy is safer; however, it uses more memory:
    In: b.flatten()
    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])
    
  3. Setting the shape with a tuple: Besides the reshape() function, we can also set the shape directly with a tuple, which is shown here:
    In: b.shape = (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]])
    

    As you can see, this changes the array directly. Now, we have a six-by-four array.

  4. Transpose: In linear algebra, it is common to transpose matrices.

    Note

    Linear algebra is a branch of mathematics dealing among others with matrices. Matrices are the two-dimensional equivalent of vectors and contain numbers in a rectangular or square grid. Transposing a matrix entails flipping the matrix in such a manner that the matrix rows become the matrix columns and vice versa. Khan Academy has a course on linear algebra, which includes transposing matrices at https://www.khanacademy.org/math/linear-algebra/matrix_transformations/matrix_transpose/v/linear-algebra-transpose-of-a-matrix.

    We can do this too using the following code:

    In: b.transpose()
    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]])
    
  5. Resize: The resize() method works just like the reshape() function, but modifies the array it operates on:
    In: b.resize((2,12))
    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]])
    

What just happened?

We manipulated the shapes of NumPy arrays using the ravel() function, the flatten() function, the reshape() function, and the resize() method, as explained in the following table:

Function

Description

ravel()

This function returns a one-dimensional array with the same data as the input array and doesn't always return a copy

flatten()

This is a method of ndarra y, which flattens arrays and always returns a copy of the array

reshape()

This function modifies the shape of an array

resize()

This function changes the shape of an array and adds copies of the input array if necessary

The code for this example is in the shapemanipulation.py file in this book's code bundle.

Stacking

Arrays can be stacked horizontally, depth wise, or vertically. We can use, for that purpose, the vstack(), dstack(), hstack(), column_stack(), row_stack(), and concatenate() functions.

..................Content has been hidden....................

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