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

Squeeze the size of a matrix in 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.

1. Setup

Ensure you have NumPy installed:

pip install numpy

Then, in your Python script or Jupyter notebook:

import numpy as np

2. Basic Usage of 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.

3. Specifying Axis

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.

4. Using squeeze() as a Method

In 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)

5. When to Use 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.

Conclusion

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.

1. Squeeze the size of a matrix with Python and Numpy:

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)

2. How to use numpy.squeeze for matrix size reduction:

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)

3. Numpy matrix squeezing example code:

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)

4. Python numpy squeeze matrix dimensions:

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)

5. Sample code for squeezing matrices in numpy:

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)

6. Squeezing multi-dimensional arrays with numpy:

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)

7. Squeeze vs flatten in numpy for matrices:

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)

8. Python numpy squeeze usage for matrix manipulation:

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)