NUMPY AND THE RESHAPE() METHOD
NumPy arrays support the reshape() method, which allows you to restructure the dimensions of an array of numbers. In general, if a NumPy array contains m elements, where m is a positive integer, then that array can be restructured as an m1 x m2 NumPy array, where m1 and m2 are positive integers such that m1*m2 = m.
Listing 2.17 displays the content of numpy_reshape.py that illustrates how to use the reshape() method on a NumPy array.
LISTING 2.17: numpy_reshape.py
import numpy as np
x = np.array([[2, 3], [4, 5], [6, 7]])
print(x.shape) # (3, 2)
x = x.reshape((2, 3))
print(x.shape) # (2, 3)
print('x1:',x)
x = x.reshape((-1))
print(x.shape) # (6,)
print('x2:',x)
x = x.reshape((6, -1))
print(x.shape) # (6, 1)
print('x3:',x)
x = x.reshape((-1, 6))
print(x.shape) # (1, 6)
print('x4:',x)
Listing 2.17 contains a NumPy array called x whose dimensions are 3x2, followed by a set of invocations of the reshape() method...