numpy.reshape() in Python
The numpy.reshape() function gives a new shape to an array without changing its data.
Syntax
1 2 3 |
numpy.reshape(a, newshape, order='C') |
Parameter
The numpy.reshape() method consists of three parameters, which are as follows:
array : It represents our input array
shape : This represents int value or tuples of int.
order : This parameter can be either C_contiguous or F_contiguous where C order operates row-rise on the array and F order operates column-wise operations.
Return
This function returns an array which is reshaped without changing the data.
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Python Program explaining # numpy.reshape() function import numpy as np array = np.arange(6) print("Array Value: \n", array) # array reshaped with 3 rows and 2 columns array = np.arange(6).reshape(3, 2) print("Array reshaped with 3 rows and 2 columns : \n", array) # array reshaped with 2 rows and 3 columns array = np.arange(6).reshape(2 ,3) print("Array reshaped with 2 rows and 3 columns : \n", array) |
Output
1 2 3 4 5 6 7 8 9 10 11 |
Array Value: [0 1 2 3 4 5] Array reshaped with 3 rows and 2 columns : [[0 1] [2 3] [4 5]] Array reshaped with 2 rows and 3 columns : [[0 1 2] [3 4 5]] |