# Remove axes of length one from an array in Numpy

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

Squeeze the Array shape using the numpy.squeeze() method in Python Numpy. This will remove axes of length one from an array. The function returns the input array, but with all or a subset of the dimensions of length 1 removed. This is always a itself or a view into the input array. If all axes are squeezed, the result is a 0d array and not a scalar.

The axis selects a subset of the entries of length one in the shape. If an axis is selected with shape entry greater than one, an error is raised.

## Steps

At first, import the required library −

import numpy as np

Creating a numpy array using the array() method. We have added elements of int type −

arr = np.array([[[20, 36, 57, 78], [32, 54, 69, 84]]])


Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)


Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

Display the shape of array −

print("\nArray Shape...\n",arr.shape)


Squeeze the Array shape using the numpy.squeeze() method −

print("\nSqueeze the shape of Array...\n",np.squeeze(arr).shape)

## Example

import numpy as np

# Creating a numpy array using the array() method
# We have added elements of int type
arr = np.array([[[20, 36, 57, 78], [32, 54, 69, 84]]])

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Display the shape of array
print("\nArray Shape...\n",arr.shape)

# Squeeze the Array shape using the numpy.squeeze() method
print("\nSqueeze the shape of Array...\n",np.squeeze(arr).shape)

## Output

Our Array...
[[[20 36 57 78]
[32 54 69 84]]]

Dimensions of our Array...
3

Datatype of our Array object...
int64

Array Shape...
(1, 2, 4)

Squeeze the shape of Array...
(2, 4)