Last updated: Apr 12, 2024
Reading timeยท3 min
Use the numpy.reshape()
method to flatten only some dimensions of a NumPy
array.
The method will flatten the array, giving it a new shape, without changing its data.
import numpy as np arr = np.zeros((2, 4, 2)) print(arr) print('-' * 50) new_arr = arr.reshape(8, 2) print(new_arr)
Running the code sample produces the following output.
[[[0. 0.] [0. 0.] [0. 0.] [0. 0.]] [[0. 0.] [0. 0.] [0. 0.] [0. 0.]]] -------------------------------------------------- [[0. 0.] [0. 0.] [0. 0.] [0. 0.] [0. 0.] [0. 0.] [0. 0.] [0. 0.]]
The numpy.reshape() method gives a new shape to an array without changing its data.
The only arguments we passed to the reshape()
method are 2 integers that
represent the new shape.
The new shape should be compatible with the original shape.
import numpy as np arr = np.zeros((2, 4, 2)) new_arr = arr.reshape(8, 2) # [[0. 0.] # [0. 0.] # [0. 0.] # [0. 0.] # [0. 0.] # [0. 0.] # [0. 0.] # [0. 0.]] print(new_arr)
-1
as a shape dimension when flattening the arrayOne shape dimension can be -1
.
When a shape dimension is -1
, its value is inferred from the length of the
array and remaining dimensions.
import numpy as np arr = np.zeros((2, 4, 2)) new_arr = arr.reshape(-1, arr.shape[-1]) # [[0. 0.] # [0. 0.] # [0. 0.] # [0. 0.] # [0. 0.] # [0. 0.] # [0. 0.] # [0. 0.]] print(new_arr)
The last shape dimension in the example is 2
, so the first shape dimension
(-1
) is inferred to be 8
.
import numpy as np arr = np.zeros((2, 4, 2)) print(arr.shape[-1]) # ๐๏ธ 2
This is calculated by dividing the total size of the array (16
) by the product
of all other specified dimensions (2
) = 8
.
import numpy as np arr = np.zeros((2, 4, 2)) print(arr.size) # ๐๏ธ 16 print(arr.shape[-1]) # ๐๏ธ 2 print(arr.size // arr.shape[-1]) # ๐๏ธ 8
The code sample flattens all but the last dimension.
You can use a similar approach to flatten all but the last two dimensions.
import numpy as np arr = np.zeros((2, 4, 2, 4)) new_arr = arr.reshape(-1, *arr.shape[-2:]) print(new_arr)
Running the code sample produces the following output.
[[[0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.]] [[0. 0. 0. 0.] [0. 0. 0. 0.]]]
You should always make sure that the new shape is compatible with the original shape.
numpy.vstack()
You can also use the numpy.vstack() method to flatten only some dimensions of a NumPy array.
The method stacks the arrays in a sequence vertically (row-wise).
import numpy as np arr = np.zeros((2, 4, 2)) print(arr) print('-' * 50) new_arr = np.vstack(arr) print(new_arr) print('-' * 50) print(new_arr.shape)
Running the code sample produces the following output.
[[[0. 0.] [0. 0.] [0. 0.] [0. 0.]] [[0. 0.] [0. 0.] [0. 0.] [0. 0.]]] -------------------------------------------------- [[0. 0.] [0. 0.] [0. 0.] [0. 0.] [0. 0.] [0. 0.] [0. 0.] [0. 0.]] -------------------------------------------------- (8, 2)
The numpy.vstack()
method is equivalent to concatenating along the first axis
after 1-D arrays of shape (N,) have been reshaped to (1, N).
You can learn more about the related topics by checking out the following tutorials: