How to flatten only some dimensions of a numpy array How to flatten only some dimensions of a numpy array numpy numpy

How to flatten only some dimensions of a numpy array


Take a look at numpy.reshape .

>>> arr = numpy.zeros((50,100,25))>>> arr.shape# (50, 100, 25)>>> new_arr = arr.reshape(5000,25)>>> new_arr.shape   # (5000, 25)# One shape dimension can be -1. # In this case, the value is inferred from # the length of the array and remaining dimensions.>>> another_arr = arr.reshape(-1, arr.shape[-1])>>> another_arr.shape# (5000, 25)


A slight generalization to Alexander's answer - np.reshape can take -1 as an argument, meaning "total array size divided by product of all other listed dimensions":

e.g. to flatten all but the last dimension:

>>> arr = numpy.zeros((50,100,25))>>> new_arr = arr.reshape(-1, arr.shape[-1])>>> new_arr.shape# (5000, 25)


A slight generalization to Peter's answer -- you can specify a range over the original array's shape if you want to go beyond three dimensional arrays.

e.g. to flatten all but the last two dimensions:

arr = numpy.zeros((3, 4, 5, 6))new_arr = arr.reshape(-1, *arr.shape[-2:])new_arr.shape# (12, 5, 6)

EDIT: A slight generalization to my earlier answer -- you can, of course, also specify a range at the beginning of the of the reshape too:

arr = numpy.zeros((3, 4, 5, 6, 7, 8))new_arr = arr.reshape(*arr.shape[:2], -1, *arr.shape[-2:])new_arr.shape# (3, 4, 30, 7, 8)