May 2019
Beginner
528 pages
29h 51m
English
We’ve used array method reshape to produce two-dimensional arrays from one-dimensional ranges. NumPy provides various other ways to reshape arrays.
reshape vs. resize The array methods reshape and resize both enable you to change an array’s dimensions. Method reshape returns a view (shallow copy) of the original array with the new dimensions. It does not modify the original array:
In [1]: import numpy as npIn [2]: grades = np.array([[87, 96, 70], [100, 87, 90]])In [3]: gradesOut[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]: gradesOut[5]:array([[ 87, 96, 70],[100, 87, 90]])
Method resize modifies the original array’s shape:
Read now
Unlock full access