NumPy’s reshape() function allows you to change the shape of an array. It takes two required arguments: the array you want to reshape and the new shape you want to give it.
Here is the basic syntax of Numpy’s reshape() function.
np.reshape(a, newshape, order='C')
It takes three arguments, the first is the array we want to reshape and the second is dimension of the new shape we want, and the third is how we want to reshape. By default, the third argument order is set to C-like index order.
Reshaping 1d array to 2d array with Numpy’s reshape()
Often you may need to change the shape of an array to fit a specific requirement. For example, you may have a one-dimensional array of 10 elements that you need to reshape into a two-dimensional array of 5 rows and 2 columns. You can use the reshape function for this purpose:
Here’s an example of reshaping 1d Numpy array into to 2d array using reshape() function.
Let us create a one-dimensional array.
my_array = np.arange(10)
Our array contains sequence of numbers from 0 to 9.
my_array array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
To reshape this one-dimensional array into 2d-array with 5 rows and 2 columns, we specify the new shape as tuple to the argument newshape
new_array = np.reshape(a = my_array, newshape=(5, 2))
Our new reshaped array will be of the dimension 5×2. Note that the original array is not modified.
new_array array([[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]])
Numpy reshape() function: Example 2
Here is another example where we reshape a one-dimensional array into 2d arrray containing 3 rows and 3 columns.
my_array = np.arange(9) new_array = my_array.reshape(3, 3) new_array array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]
Numpy reshape() function from 2d array to 1d-array: Example 2
Numpy’s reshape function can also be used in the reverse direction compared to the previous example. We can reshape a 2d-array into 1d-array by using -1 as argument to reshape() function.
In the example below, we use the 3×3 matrix from the previous example to convert into 1d-array.
new_array.reshape(-1) array([0, 1, 2, 3, 4, 5, 6, 7, 8])
Here is the addition notes on the third argument of Numpy’s reshape() function.
‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘F’ means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the ‘C’ and ‘F’ options take no account of the memory layout of the underlying array, and only refer to the order of indexing. ‘A’ means to read / write the elements in Fortran-like index order if a is Fortran contiguous in memory, C-like order otherwise.
Here is an example to understand what happens when we choose the different options for the third argument “order”.
First, we reshape using the default order= C argument.
a = np.arange(12).reshape((3, 4)) a array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])
Note the differences in index order between C-like and F-like options we have
np.reshape(np.arange(12), (3, 4), order='F') array([[ 0, 3, 6, 9], [ 1, 4, 7, 10], [ 2, 5, 8, 11]])