There may be various reasons to reshape arrays. One of these reasons could be when you want to add an array to another - they need to be of the same size. Recall that the shape of an array is the number of elements in each dimension.
Before reshaping arrays, we have to make sure that the existing array size can indeed match its new size. This means that a 1D array with 11 elements cannot be reshaped into 2D array with 2 rows because 11 cannot be divided by 2 (1 row will have 6 and another row will have 5 elements).
To reshape arrays, we can use the .reshape
.
Let's create a 3 by 3 array and reshape it into a 1 by 9 array.
Within the parentheses of .reshape
, specify the new row size followed by column size.
.reshape
has a special feature where it automatically detects the column number. This means that we do not actually have to specify a column number, we can simply just replace the column number with -1
.
You are expected to comply with University policies and guidelines namely, Appropriate Use of Information Resources Policy, IT Usage Policy and Social Media Policy. Users will be personally liable for any infringement of Copyright and Licensing laws. Unless otherwise stated, all guide content is licensed by CC BY-NC 4.0.