What does -1 Mean in Numpy Reshape?


NumPy is a Python library for numerical computing that provides efficient array operations, and numpy.reshape() is a function used to change the shape of an array, with -1 indicating an inferred dimension.

When working with arrays, we frequently get into instances where we need to modify the shape of the array, but doing so requires copying the data first, then arranging it into the required shape which is time taking. Fortunately, Python has a function called reshape() that may help with this.

Example 1: Find Unknown Dimensions in Numpy

We are only allowed to have one "unknown" dimension while we are unknown of any array dimension.

This means that when using the reshape method, we do not need to provide a number for one of those dimensions. We use NumPy to calculate the unknown dimension by passing it a value of -1.

The parameter values (2, 2, -1) indicate that the desired shape of the output array is 2 rows, 2 columns, and the remaining dimension is inferred automatically based on the size of the input array.

The following program Shows the use of -1 in numpy arrays reshape() function

# importing numpy module 
import numpy as np
 
# creating a numpy array 
inputArray = np.array([15, 16, 17, 18, 19, 20, 21, 22])
 
# reshaping the input array into a 3-dimensional array with 2 rows, 2 columns, and an automatically inferred depth
outputArray = inputArray.reshape(2, 2, -1)
# printing the resultant array
print(outputArray)

Output

[[[15 16]
  [17 18]]

 [[19 20]
  [21 22]]]

Example 2: Using -1 as Unknown Number of Columns Value

The following program shows to use of -1 for unknown columns value

# importing numpy module 
import numpy as np
 
# creating a numpy array 
inputArray = np.array([15, 16, 17, 18, 19, 20, 21, 22])
 
# reshaping the input array into a 3-dimensional array with 2 rows, an automatically inferred middle dimension, and 4 columns
outputArray = inputArray.reshape(2, -1, 4)
# printing the resultant array
print(outputArray)

Output

[[[15 16 17 18]]

 [[19 20 21 22]]]

Example 3: Using -1 as Unknown Number of rows Value

In the example given below, the shape is specified as (-1, 4, 2), indicating that the first dimension is automatically inferred based on the size of the input array. The output is a 3-dimensional array with the desired shape.The reshape() function is then used to reshape the inputArray into a 3-dimensional array. It has 2 rows, an automatically inferred middle dimension (using -1), and 4 columns. The -1 parameter allows numpy to automatically calculate the size of the inferred dimension based on the size of the input array and the given dimensions.

# importing numpy module 
import numpy as np
 
# creating a numpy array 
inputArray = np.array([15, 16, 17, 18, 19, 20, 21, 22])
 
# reshaping the input array into a 3-dimensional array with an automatically inferred first dimension, 4 columns, and 2 depth elements
outputArray = inputArray.reshape(-1, 4, 2)
# printing the resultant array
print(outputArray)

Output

On executing, the above program will generate the following output

[[[15 16]
  [17 18]
  [19 20]
  [21 22]]]

Example 4: Using -1 for more than one unknown dimension

In the foexample, we used -1 for more than one unknown dimension. Hence it raised an error.

# importing numpy module 
import numpy as np
 
# creating a numpy array 
inputArray = np.array([15, 16, 17, 18, 19, 20, 21, 22])
 
# reshaping the input array into a 3-dimensional array with 2 as the first dimension and an automatically inferred second and third dimension
outputArray = inputArray.reshape(2, -1, -1)
# printing the resultant array
print(outputArray)

Output

Traceback (most recent call last):
  File "/home/cg/root/85901/main.py", line 8, in <module>
    outputArray = inputArray.reshape(2, -1, -1)
ValueError: can only specify one unknown dimension

Flattening the Numpy Arrays using reshape() Function

Flattening 3D Array to 1D Array using reshape() function

The following program returns the flattened 1-dimensional array by flattening the input 3D Array to 1D Array using reshape() function

# importing numpy module 
import numpy as np

# creating a 3D array
inputArray = np.array([[1, 3, 5],
                      [7, 9, 11], 
                      [13, 15, 17]])

# flattening 3D input array to 1-dimension using reshape() function
outputArray = inputArray.reshape(-1)
# printing the resultanat flattened array
print("Output flattened array:\n", outputArray)

Output

Output flattened array:
 [ 1  3  5  7  9 11 13 15 17]

Flattening 2D Array to 1D Array using reshape() function

The following program returns the flattened 1-dimensional array by flattening the input 2D Array to 1D Array using reshape() function

# importing numpy module 
import numpy as np
 
# creating a 2D array
inputArray = np.array([[15, 16, 17], [18, 19, 20]])
 
# flattening 2D input array to 1-dimension using reshape() function
outputArray = inputArray.reshape(-1)
# printing the resultant flattened array
print("Output flattened array:\n", outputArray)

Output

Output flattened array:
 [15 16 17 18 19 20]

Conclusion

In conclusion, the value -1 in NumPy's reshape() function is used to represent an unknown dimension. It allows NumPy to automatically calculate the size of that dimension based on the other dimensions and the size of the input array. This feature is particularly useful when reshaping arrays with known dimensions or flattening multi-dimensional arrays to one dimension. By leveraging -1, we can simplify the reshaping process and avoid the need for manual calculations.

Updated on: 17-Aug-2023

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements