The squeeze()
function in NumPy is used to remove an axis of length 1
from an input array.
Axes in NumPy are defined for arrays having more than one dimension. For example, a 2-D
array has two corresponding axes: the axes running vertically downward across rows (this is axis 0
), and the axes running horizontally across columns (this is axis 1
).
numpy.squeeze(a, axis=None)
The squeeze()
function takes the following parameter values.
a
: This is the input array. It is a required parameter.axis
: This selects a subset of the length in the given shape. It is an optional parameter.The squeeze()
function returns the input array, a
, but with the subset of the dimension with length 1
removed.
import numpy as np# creating an input arraya = np.array([[[1], [2], [3], [4]]])# getting the length of aprint(a.shape)# removing the dimensions with length 1b = np.squeeze(a)# obtaining the shape of the new arrayprint(b.shape)
numpy
module.a
, using the array()
function.a
using the shape
attribute.1
from the input array, a
, using the squeeze()
function. The result is assigned to a variable, b
.b
, with the dimensions of length1
removed.