×

numpy.squeeze() in Python

numpy.squeeze() in Python

The numpy.squeeze() function removes single-dimensional entries from the shape of an array.

Syntax

numpy.squeeze(a, axis=None)

Parameter

The numpy.squeeze() function has three parameters which are as follows:

a: It represents the Input data.

axis: This parameter signifies an int or tuple of int values. It selects a subset of the single-dimensional entries in the shape. If an axis is selected with shape entry greater than one, an error is raised.

Return

This function returns an input array, but with all or a subset of the dimensions of length 1 removed.

Example 1

#Python Program explaining
#numpy.squeeze() function
import numpy as np
in_array = np.array([[[12, 42, 22], [9, 12, 2]]])
print ("Input array : ", in_array) 
out_array = np.squeeze(in_array) 
print ("output squeezed array : ", out_array)

Output

Input array :  [[[12 42 22]
  [ 9 12  2]]]
output squeezed array :  [[12 42 22]
[ 9 12  2]]

Example 2

#Python Program explaining
#numpy.squeeze() function
import numpy as np
in_array = np.arange(8).reshape(1, 4, 2) 
print ("Input array : ", in_array)   
out_array = np.squeeze(in_array, axis = 0) 
print ("output array : ", out_array)

Output

Input array :  [[[0 1]
  [2 3]
  [4 5]
  [6 7]]]
output array :  [[0 1]
[2 3]
[4 5]
[6 7]]

Related Topics

numpy.arrange() in Python

Python numpy.arrange() The arrange() function of Python numpy class returns an array with equally spaced elements as per the interval where the interval mentioned is half opened, i.e. [Start, Stop). Syntax numpy.arange([start, ]stop, [step, ]dtype=None) Parameter start :It...

1 minute read.

numpy.ndarray.T in Python

numpy.ndarray.T in Python The numpy.ndarray.T attribute makes a Transpose of an array having a dimension greater than or equal to 2. Syntax ndarray.T() Parameter NA Return This attribute returns the transpose of an array Example 1 # Python Program explaining # numpy.ndarray.T()...

1 minute read.

Python NumPy Tutorial

NumPy is a scientific library in Python Programming Language. It provides objects and routines for fast operations on arrays, random simulations, statistical operations, sorting, etc. Numpy has an ndarray object which...

2 minutes read.

numpy.atleast_2d() in Python

numpy.atleast_2d() in Python The numpy.atleast_2d() function converts the inputs as arrays with at least two dimensions. Syntax numpy.atleast_2d(*arys) Parameter arys1, arys2, … : This parameter represents one or more array-like sequences where the non-array inputs are converted...

1 minute read.

numpy.asarray_chkfinite() in Python

numpy.asarray_chkfinite() in Python The numpy.asarray_chkfinite() function converts the input to an array, checking for NaNs or Infs. Syntax numpy.asarray_chkfinite(a, dtype=None, order=None) Parameter a : This parameter represents an input data, which can be in the form of lists, tuples,...

2 minutes read.

numpy.vstack() in Python

numpy.vstack() in Python The numpy.vstack() function stacks the arrays in a sequence vertically (row wise). Syntax numpy.vstack(tup) Parameter tup: This parameter represents the sequence of ‘ndarrays’ where the arrays must have the same shape along all...

1 minute read.

numpy.meshgrid() in Python

numpy.meshgrid() in Python The meshgrid() function of Python numpy class returns the coordinate matrices from coordinate vectors. Syntax numpy.meshgrid(*xi, **kwargs) Parameter The numpy.meshgrid() function consists of four parameters which are as follow: x1, x2,…, xn: This parameter signifies...

2 minutes read.

numpy.ones_like() in Python

numpy.ones_like() in Python The one_like() method of Python numpy class returns an array of ones with the same shape and type as the specified array. Syntax numpy.ones_like(a, dtype=None, order='K', subok=True) Parameter The numpy.ones_like() method consists of four parameters,...

1 minute read.

numpy.tri() in Python

numpy.tri() in Python The tri() function of Python numpy class returns an array with ones at and below the given diagonal(k value) and zeros elsewhere. Syntax numpy.tri(N, M=None, k=0, dtype=<class 'float'>) Parameter R : It represents the number...

1 minute read.

numpy.concatenate() in Python

numpy.concatenate() in Python The numpy.concatenate() function joins a sequence of arrays along an existing axis. Syntax numpy.concatenate((a1, a2, ...), axis=0, out=None) Parameter a1, a2, … : This parameter represents the sequence of the array where they must have the same shape,...

1 minute read.

numpy.swapaxes() in Python

numpy.swapaxes() in Python The numpy.swapaxes() function interchanges the two specified axes of the given array. Syntax numpy.swapaxes(a, axis1, axis2) Parameter The numpy.swapaxes() method consists of three parameters, which are as follows: a : This parameter represents the Input array. axis1 : It represents...

1 minute read.

numpy.frombuffer() in Python

numpy.frombuffer() in Python The frombuffer()  function of Python numpy class creates an array by using the given buffer. Syntax numpy.frombuffer(buffer, dtype=float, count=-1, offset=0) Parameter The numpy.frombuffer() method consists of four parameters, which are as follows: buffer: This parameter...

1 minute read.

numpy.ndarray.flat() in Python

numpy.ndarray.flat() in Python The numpy.ndarray.flat() returns a 1-D iterator over the array. This function is not a subclass of, Python’s built-in iterator object, otherwise it will act the same as a...

1 minute read.

numpy.tril() in Python

numpy.tril() in Python The tril() function of Python numpy class returns a copy of an array with the elements above the k-th diagonal zeroed. Syntax numpy.tril(m, k=0) Parameter a: It represents the input array k: This parameter represents...

1 minute read.

numpy.asarray() in Python

numpy.asarray() in Python The numpy.asarray()function is used to convert the input to an array. Input includes lists, lists of tuples, tuples, tuples of tuples,etc,. Syntax numpy.asarray(a, dtype=None, order=None) Parameter arr : This parameter includes theinput data, in any...

1 minute read.

numpy.squeeze() in Python

numpy.squeeze() in Python The numpy.squeeze() function removes single-dimensional entries from the shape of an array. Syntax numpy.squeeze(a, axis=None) Parameter The numpy.squeeze() function has three parameters which are as follows: a: It represents the Input data. axis: This parameter signifies an int or...

1 minute read.

numpy.fromiter() in Python

numpy.fromiter() in Python The fromiter() function of Python numpy class creates a ndarray by using an iterable object. It returns a one-dimensional ndarray object. Syntax numpy.fromiter(iterable, dtype, count=-1) Parameter The numpy.frombuffer() method consists of three parameters, which...

1 minute read.

numpy.zeros in Python

numpy.zeros in Python The zeros() method of Python numpy class returns a new array of given shape and type, filled with zeros. Syntax numpy.zeros(shape, dtype=float, order='C') Parameter The numpy.zeros() method consists of three parameters, which are as...

1 minute read.

numpy.trim_zeros() in Python

numpy.trim_zeros() in Python The numpy.trim_zeros() trims the leading and/or trailing zeros from a 1-D array or sequence. Syntax numpy.trim_zeros(filt, trim='fb') Parameter The numpy.trim_zeros() function consists of two parameters, which are as follows: filt : This parameter represents the 1-D...

1 minute read.

numpy.identity() in Python

numpy.identity() in Python The identity() method of Python numpy class returns an identity matrix i.e., a square matrix with ones on the main diagonal. Syntax numpy.identity(n, dtype=None) Parameters The numpy. identity()  method consists of two parameters,...

1 minute read.