The reshape()
function in NumPy is a powerful tool which allows us to restructure an array according to the arguments we specify.
The function takes in an array and a new shape as required arguments. The new shape must exactly contain all the elements from the input array. For example, we could reshape an array with 8
elements to (4, 2)
, but not (4, 3)
.
-1
is a special argument which can only be used once as a dimension. The -1
dimension will take on the value necessary to allow the new shape to contain all the elements of the array.
NumPy also has a variety of other shaping functions, such as flatten
or transpose
.