NumPy Array Squeeze

numpy
Published

September 27, 2024

np.squeeze() function in numpy efficiently removes single-dimensional entries from an array’s shape. Understanding and utilizing squeeze() can significantly improve your code’s readability and efficiency, especially when dealing with arrays resulting from operations that might inadvertently introduce unnecessary dimensions.

This post will look into the functionality of np.squeeze(), providing clear explanations and practical examples to solidify your understanding.

Understanding Singleton Dimensions

Before diving into squeeze(), let’s clarify what singleton dimensions are. A singleton dimension is a dimension with a size of 1. Consider the following examples:

  • array([[1]]): This array has shape (1, 1). Both dimensions are singleton dimensions.
  • array([[[1, 2, 3]]]): This array has shape (1, 1, 3). The first two dimensions are singleton dimensions.
  • array([1, 2, 3]): This array has shape (3,). It has no singleton dimensions.

The Power of np.squeeze()

The np.squeeze() function removes singleton dimensions from an array. It modifies the array in place if the out parameter isn’t specified. Let’s illustrate this with code:

import numpy as np

x = np.array([[1, 2, 3]])  # Shape (1, 3)
y = np.squeeze(x)         # Shape (3,)
print(f"Original array x shape: {x.shape}")
print(f"Squeezed array y shape: {y.shape}")

#Example 2: Removing multiple singleton dimensions
z = np.array([[[1]]])      #Shape (1,1,1)
w = np.squeeze(z)        #Shape () - a scalar
print(f"Original array z shape: {z.shape}")
print(f"Squeezed array w shape: {w.shape}")

a = np.array([1, 2, 3])  # Shape (3,)
b = np.squeeze(a)         # Shape (3,) - No change
print(f"Original array a shape: {a.shape}")
print(f"Squeezed array b shape: {b.shape}")


#Example 4: Specifying axis to squeeze
c = np.array([[[1,2],[3,4]]]) #Shape (1,2,2)
d = np.squeeze(c, axis=0) #Shape (2,2) - squeezes along axis 0
print(f"Original array c shape: {c.shape}")
print(f"Squeezed array d shape: {d.shape}")

e = np.squeeze(c, axis=1) #This will raise an error, as axis 1 is not a singleton dimension.

The output clearly demonstrates how squeeze() effectively removes singleton dimensions. Note that if the array has no singleton dimensions, squeeze() returns the original array without modification. Also note that specifying an axis to squeeze allows for more granular control over the dimension removal process.

When to Use squeeze()

squeeze() proves invaluable in various scenarios:

  • Cleaning up array shapes: After certain array operations, you might end up with arrays containing unnecessary singleton dimensions. squeeze() provides a clean way to remove these, improving the readability and efficiency of subsequent operations.
  • Interoperability with functions: Some functions might expect arrays of a specific shape. squeeze() ensures your arrays conform to these expectations.
  • Simplifying code: By removing unnecessary dimensions, squeeze() leads to cleaner and more concise code.

Using squeeze() strategically enhances your NumPy workflow, leading to more efficient and readable code. Mastering this simple yet powerful function will significantly improve your data manipulation skills.