7.13 Reshaping and Transposing

reshape vs. resize

  • Method reshape returns a view (shallow copy) of the original array with new dimensions
  • Does not modify the original array
In [1]:
import numpy as np
In [2]:
grades = np.array([[87, 96, 70], [100, 87, 90]])
In [3]:
grades
Out[3]:
array([[ 87,  96,  70],
       [100,  87,  90]])
In [4]:
grades.reshape(1, 6)
Out[4]:
array([[ 87,  96,  70, 100,  87,  90]])
In [5]:
grades
Out[5]:
array([[ 87,  96,  70],
       [100,  87,  90]])
  • Method resize modifies the original array’s shape
In [6]:
grades.resize(1, 6)
In [7]:
grades
Out[7]:
array([[ 87,  96,  70, 100,  87,  90]])

flatten vs. ravel

  • Can flatten a multi-dimensonal array into a single dimension with methods flatten and ravel
  • flatten deep copies the original array’s data
In [8]:
grades = np.array([[87, 96, 70], [100, 87, 90]])
In [9]:
grades
Out[9]:
array([[ 87,  96,  70],
       [100,  87,  90]])
In [10]:
flattened = grades.flatten()
In [11]:
flattened
Out[11]:
array([ 87,  96,  70, 100,  87,  90])
In [12]:
grades
Out[12]:
array([[ 87,  96,  70],
       [100,  87,  90]])
In [13]:
flattened[0] = 100
In [14]:
flattened
Out[14]:
array([100,  96,  70, 100,  87,  90])
In [15]:
grades
Out[15]:
array([[ 87,  96,  70],
       [100,  87,  90]])
  • Method ravel produces a view of the original array, which shares the grades array’s data
In [16]:
raveled = grades.ravel()
In [17]:
raveled
Out[17]:
array([ 87,  96,  70, 100,  87,  90])
In [18]:
grades
Out[18]:
array([[ 87,  96,  70],
       [100,  87,  90]])
In [19]:
raveled[0] = 100
In [20]:
raveled
Out[20]:
array([100,  96,  70, 100,  87,  90])
In [21]:
grades
Out[21]:
array([[100,  96,  70],
       [100,  87,  90]])

Transposing Rows and Columns

  • Can quickly transpose an array’s rows and columns
    • “flips” the array, so the rows become the columns and the columns become the rows
  • T attribute returns a transposed view (shallow copy) of the array
In [22]:
grades.T
Out[22]:
array([[100, 100],
       [ 96,  87],
       [ 70,  90]])
In [23]:
grades
Out[23]:
array([[100,  96,  70],
       [100,  87,  90]])

Horizontal and Vertical Stacking

  • Can combine arrays by adding more columns or more rows—known as horizontal stacking and vertical stacking
In [24]:
grades2 = np.array([[94, 77, 90], [100, 81, 82]])
  • Combine grades and grades2 with NumPy’s hstack (horizontal stack) function by passing a tuple containing the arrays to combine
  • The extra parentheses are required because hstack expects one argument
  • Adds more columns
In [25]:
np.hstack((grades, grades2))
Out[25]:
array([[100,  96,  70,  94,  77,  90],
       [100,  87,  90, 100,  81,  82]])
  • Combine grades and grades2 with NumPy’s vstack (vertical stack) function
  • Adds more rows
In [26]:
np.vstack((grades, grades2))
Out[26]:
array([[100,  96,  70],
       [100,  87,  90],
       [ 94,  77,  90],
       [100,  81,  82]])

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.