reshape vs. resize¶reshape returns a view (shallow copy) of the original array with new dimensionsarrayimport numpy as np
grades = np.array([[87, 96, 70], [100, 87, 90]])
grades
grades.reshape(1, 6)
grades
resize modifies the original array’s shapegrades.resize(1, 6)
grades
flatten vs. ravel¶flatten and ravelflatten deep copies the original array’s datagrades = np.array([[87, 96, 70], [100, 87, 90]])
grades
flattened = grades.flatten()
flattened
grades
flattened[0] = 100
flattened
grades
ravel produces a view of the original array, which shares the grades array’s dataraveled = grades.ravel()
raveled
grades
raveled[0] = 100
raveled
grades
array’s rows and columnsarray, so the rows become the columns and the columns become the rowsT attribute returns a transposed view (shallow copy) of the arraygrades.T
grades
grades2 = np.array([[94, 77, 90], [100, 81, 82]])
grades and grades2 with NumPy’s hstack (horizontal stack) function by passing a tuple containing the arrays to combinehstack expects one argumentnp.hstack((grades, grades2))
grades and grades2 with NumPy’s vstack (vertical stack) functionnp.vstack((grades, grades2))
©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.