Using ndarray.squeeze() method in NumPy (4 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Introduction

Numpy is a cornerstone library in the Python ecosystem widely used for numerical computations. One of the library’s commonly used features is the ndarray.squeeze() method, which is designed to remove single-dimensional entries from the shape of an array. This tutorial explores ndarray.squeeze() through a series of examples, demonstrating its use and versatility in data manipulation and cleaning processes.

What is ndarray.squeeze() Used for?

The ndarray.squeeze() method in NumPy is used to remove axes of length one from an N-dimensional array. A ‘squeezed’ array will retain the same data as the original array, but its shape may be altered. This is particularly useful in scenarios where an array might have redundant dimensions that do not contribute to the data’s significance but complicate array handling and operations. The method returns a new view of the original array whenever possible.

Basic Example: Squeezing a Single-axis Array

First, to demonstrate the basic use of squeeze(), let’s start with a simple example. Imagine you have an array that represents some measured data along a single dimension, but this data is encapsulated within two unnecessary dimensions.

import numpy as np

# Creating a 3D array with a single data point
array_3d = np.array([[[1]]])
print("Original shape:", array_3d.shape)

# Using squeeze to reduce dimensions
squeezed_array = array_3d.squeeze()
print("Squeezed shape:", squeezed_array.shape)
print("Squeezed array:", squeezed_array)

The output will be:

Original shape: (1, 1, 1)
Squeezed shape: ()
Squeezed array: 1

This example demonstrates how an array with shape (1, 1, 1) can be ‘squeezed’ to a shapeless scalar value, simplifying the representation of singular data points.

Intermediate Example: Squeezing Multi-dimensional Arrays

Moving onto a more complex example, consider a scenario where you have a 3D array representing some time-series data across different sensors. Suppose many of these time points only collect data from a single sensor, leading to unnecessary additional dimensions.

import numpy as np

# Generating a 3D array representing time-series data
array_3d = np.random.rand(10, 1, 5) # 10 time points, 1 sensor, 5 data features
print("Original shape:", array_3d.shape)

# Using squeeze to remove the single-dimensional entry
squeezed_array = array_3d.squeeze()
print("After squeezing:", squeezed_array.shape)

The output:

Original shape: (10, 1, 5)
After squeezing: (10, 5)

This example illustrates how squeezing can be used to streamline the representation of time-series data by removing unnecessary dimensions, making the data easier to handle and process.

Advanced Uses of ndarray.squeeze()

As we delve deeper, the versatility of ndarray.squeeze() is further revealed in manipulation and analysis beyond basic dimension reduction. An interesting application is selectively squeezing dimensions. By passing an axis parameter, you can specify which dimensions to squeeze, offering fine control over the reshaped array.

import numpy as np

# Creating a complex 4D array
complex_4d = np.random.rand(10, 1, 5, 1)

# Squeezing specifically the second and fourth dimensions
squeezed_complex = complex_4d.squeeze(axis=(1, 3))
print("After selective squeezing:", squeezed_complex.shape)

Output:

After selective squeezing: (10, 5)

This advanced example demonstrates selective squeezing, where only specified dimensions are removed. Such flexibility is crucial when working with complex, high-dimensional data, allowing you to tailor the shape of your arrays for specific analytical needs without losing essential information.

Handling Squeeze Exceptions

While using the squeeze method, it’s important to handle cases where squeezing a specified axis would not reduce the array’s dimensionality. Attempting to squeeze a dimension that is not of length one will raise a ValueError, alerting you to the improper use of the method.

import numpy as np

try:
  # Attempting to squeeze a dimension of length > 1
  unreliable_array = np.random.rand(10, 2, 5)
  squeezed_unreliable = unreliable_array.squeeze(axis=1)
except ValueError as e:
  print("Error:", e)

This cautionary example underscores the importance of understanding the data and array dimensions before applying squeeze operations to prevent errors and data loss.

Conclusion

The ndarray.squeeze() method in NumPy is a powerful tool for dimensional manipulation, simplifying data handling by removing redundant dimensions. From cleaning singular data points to processing complex, high-dimensional datasets, squeeze offers flexibility and control. Understanding how and when to use this method will significantly enhance your data manipulation and analysis tasks.