Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy
In NumPy, the term "squeeze" refers to removing dimensions of size 1 from the shape of an array. The squeeze()
function is particularly useful when dealing with matrices or arrays that may have redundant dimensions of size 1, often resulting from various operations or data extraction methods.
Ensure you have NumPy installed:
pip install numpy
Then, in your Python script or Jupyter notebook:
import numpy as np
squeeze()
Let's start with a 3D array where one of the dimensions has a size of 1.
matrix = np.array([[[1, 2, 3], [4, 5, 6]]]) print("Original shape:", matrix.shape) squeezed_matrix = np.squeeze(matrix) print("Squeezed shape:", squeezed_matrix.shape)
Output:
Original shape: (1, 2, 3) Squeezed shape: (2, 3)
As seen above, the dimension with size 1 has been removed.
You can also specify which axis or axes should be squeezed using the axis
parameter:
matrix = np.array([[[1], [2]], [[3], [4]]]) print("Original shape:", matrix.shape) squeezed_matrix = np.squeeze(matrix, axis=2) print("Squeezed shape:", squeezed_matrix.shape)
Output:
Original shape: (2, 2, 1) Squeezed shape: (2, 2)
If you try to squeeze an axis that does not have a size of 1, a ValueError will be raised.
squeeze()
as a MethodIn addition to the function form np.squeeze()
, arrays have the squeeze()
method:
matrix = np.array([[[1, 2], [3, 4]]]) print("Original shape:", matrix.shape) squeezed_matrix = matrix.squeeze() print("Squeezed shape:", squeezed_matrix.shape)
Output:
Original shape: (1, 2, 2) Squeezed shape: (2, 2)
squeeze()
The squeeze()
function is often useful when:
You're extracting a single row or column from a 2D array, resulting in a redundant dimension.
Performing operations that introduce extra dimensions, e.g., when using functions that inherently return results with specific shapes.
Preprocessing data for libraries or functions that are sensitive to input shape, such as machine learning libraries.
The squeeze()
function in NumPy is a simple yet handy tool for adjusting the shape of your arrays. By understanding and utilizing this function, you can ensure that your data structures are as concise and efficient as possible, making further operations and data processing smoother and more efficient.
Squeezing the size of a matrix involves removing dimensions with size 1.
import numpy as np # Create a 2D NumPy array (matrix) with a singleton dimension matrix = np.array([[[1, 2, 3]]]) # Squeeze the matrix using numpy.squeeze() squeezed_matrix = np.squeeze(matrix) print("Original Matrix:") print(matrix) print("\nSqueezed Matrix:") print(squeezed_matrix)
Use numpy.squeeze()
to reduce the size of a matrix by removing singleton dimensions.
# Assuming 'matrix' is already defined # Squeeze the matrix using numpy.squeeze() squeezed_matrix = np.squeeze(matrix) print("Original Matrix:") print(matrix) print("\nSqueezed Matrix:") print(squeezed_matrix)
Example code demonstrating the squeezing of a matrix using NumPy's numpy.squeeze()
.
# Assuming 'matrix' is already defined # Squeeze the matrix using numpy.squeeze() squeezed_matrix = np.squeeze(matrix) print("Original Matrix:") print(matrix) print("\nSqueezed Matrix:") print(squeezed_matrix)
Reduce the dimensions of a matrix in Python using the numpy.squeeze()
function.
# Assuming 'matrix' is already defined # Squeeze the matrix using numpy.squeeze() squeezed_matrix = np.squeeze(matrix) print("Original Matrix:") print(matrix) print("\nSqueezed Matrix:") print(squeezed_matrix)
Sample code illustrating the squeezing of matrices using NumPy's numpy.squeeze()
.
# Assuming 'matrix' is already defined # Squeeze the matrix using numpy.squeeze() squeezed_matrix = np.squeeze(matrix) print("Original Matrix:") print(matrix) print("\nSqueezed Matrix:") print(squeezed_matrix)
Extend the concept of squeezing to multi-dimensional arrays using NumPy.
# Assuming 'matrix' is already defined # Squeeze the matrix using numpy.squeeze() squeezed_matrix = np.squeeze(matrix) print("Original Matrix:") print(matrix) print("\nSqueezed Matrix:") print(squeezed_matrix)
Understand the differences between squeezing and flattening matrices in NumPy.
# Assuming 'matrix' is already defined # Squeeze the matrix using numpy.squeeze() squeezed_matrix = np.squeeze(matrix) # Flatten the matrix using numpy.flatten() flattened_matrix = matrix.flatten() print("Original Matrix:") print(matrix) print("\nSqueezed Matrix:") print(squeezed_matrix) print("\nFlattened Matrix:") print(flattened_matrix)
Use the numpy.squeeze()
function in NumPy for manipulating the dimensions of a matrix.
# Assuming 'matrix' is already defined # Squeeze the matrix using numpy.squeeze() squeezed_matrix = np.squeeze(matrix) print("Original Matrix:") print(matrix) print("\nSqueezed Matrix:") print(squeezed_matrix)