What is the numpy.squeeze() in NumPy?

Learn via video courses
Topics Covered

NumPy is a very powerful library provided by Python, which helps us to perform complex and high-computational calculations. There are a lot of functions that help us to manipulate arrays and use them according to our needs. One such function is the NumPy squeeze() function.

The NumPy squeeze() function helps us to remove the one-dimensional entry from the original shape of the array. In other ways, the squeeze() function is used to remove an axis of length 1 from the original array.

If the dimensions of an input array are (1, 2, 3), then the squeeze() function will remove the one-dimension entry. Hence the new shape would be (2, 3).

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Syntax

The syntax for NumPy squeeze() is:

Parameters

The parameters that NumPy squeeze() takes are:

  • arr: This mandatory parameter represents the input array.
  • axis: This optional parameter selects a subset of the entries in the shape of an array. The default value for this parameter is None.
Sharpen Your Fundamentals with Free Learning

Return Type

The return type of this function is an array, with all the subsets of the dimension of unit length removed.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Examples

  • Basic example of NumPy squeeze()

    In this example, we will take a three-dimensional array having 1 as one of its dimensions. With the help of the NumPy squeeze() function, we will remove the single-dimension entries from the original shape of the array.

    Output

  • Using NumPy squeeze() on an array created using arange() and reshape()

    In this example, we will use the NumPy squeeze() function on an array created using arange() and reshape() methods. These methods will help us to generate arrays with continuous values with specified dimensions.

    Output

  • Using NumPy squeeze() when ValueError occurs

As discussed earlier, there are two parameters for squeeze(); arr and axis (optional). If an axis is selected with the entry of shape being greater or equal to 1, an error is raised, called ValueError.

Output

Conclusion

  • In this article, we learned about the NumPy squeeze() function, a method used to remove the one-dimensional entry from the original shape of the array.
  • With the help of some examples, we were able to understand the different use cases of NumPy squeeze() function.
  • We looked at a special scenario in which, on increasing the axis value more than or equal to 1, we get a ValueError.