×

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 the first axis.

axis2 : It signifies the second axis.

Return

This function returns the view of parameter ‘a’ if ‘a’ is a ndarray, else a new array is created.

Example 1

# Python Program explaining
# numpy.swapaxes() function
import numpy as np
array = np.arange(8).reshape(2,2,2)
print ('Original array:')
print (array,"\n")
# swaping the numbers between axis 1  and axis 2 (along width)
print ('The array after applying the swapaxes() function:')
print (np.swapaxes(array, 2, 1))

Output

Original array:
[[[0 1]
  [2 3]]
[[4 5]
  [6 7]]]
The array after applying the swapaxes() function:
[[[0 2]
  [1 3]]
[[4 6]
  [5 7]]]

Related Topics

numpy.full_like() in Python

numpy.full_like() in Python The full_like() method of Python numpy class returns a full array with the same shape and type as a given array. Syntax numpy.full_like(a, fill_value, dtype=None, order='K', subok=True) Parameter shape :This parameter represents the number of rowsorder...

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.eye() in Python

numpy.eye() in Python: The eye() method of Python numpy class returns a 2-D array with ones on the diagonal and zeros elsewhere. Syntax numpy.eye(N, M=None, k=0, dtype=<class 'float'>, order='C') Parameters The numpy.eye()  method consists of five parameters, which...

1 minute 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.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.

numpy.rollaxis() in Python

numpy.rollaxis() in Python The numpy.rollaxis() function rolls the given axis backwards until it lies in the specified position. Syntax numpy.rollaxis(a, axis, start=0) Parameter The numpy. rollaxis() method consists of three parameters, which are as follows: a : It represents an...

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.zeros_like() in Python

Numpy.zeros_like() in Python The zeros_like() method of Python numpy class returna an array of zeros with the same shape and type as that of specified array. Syntax numpy.zeros_like(a, dtype=None, order='K', subok=True) Parameter The numpy.zeros_like() method consists of four...

1 minute read.

numpy.append() in Python

numpy.append() in Python The numpy.append() function appends the values to the end of an array. Syntax numpy.append(arr, values, axis=None) Parameter The numpy.append() function consists of three parameters, which are as follows: arr : This parameter represents the values that are...

1 minute read.

numpy.broadcast_arrays() in Python

numpy.broadcast_arrays() in Python The numpy.broadcast_arrays() function broadcasts any number of arrays against each other. Syntax numpy.broadcast_arrays(*args, **kwargs) Parameter  The numpy.broadcast_arrays() function has two parameters which are as follows: `*args`: This parameter represents the arrays to broadcast. subok: It is an optional parameter...

1 minute read.

numpy.unique() in Python

numpy.unique() in Python The numpy.unique() function finds the unique elements of an array and returns the sorted unique elements for the specified array.  Syntax numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None) Parameter ar : This parameter returns an Input array.If the aaray is...

2 minutes read.

numpy.ones() in Python

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

1 minute read.

numpy.diag() in Python

numpy.diag() in Python The diag() function of Python numpy class extracts and construct a diagonal array. Syntax numpy.diag(v, k=0) Parameter a: It represents the array_like. k: It represents the diagonal value that we require. It is an...

1 minute read.

numpy.repeat() in Python

numpy.repeat() in Python The numpy.repeat() function repeats the elements of an array. Syntax numpy.repeat(a, repeats, axis=None) Parameter The numpy.repeat() function consists of three parameters, which are as follows: a: This parameter represents the input array. repeats: This parameter represents the...

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.geomspace() in Python

numpy.geomspace() in Python The geomspace() function of Python numpy class returns the numbers spaced equally on a log scale (a geometric progression). This method is similar to numpy.logspace() but with endpoints specified directly. Syntax numpy.geomspace(start, stop, num=50, endpoint=True, dtype=None, axis=0) Parameter start: This...

1 minute read.

numpy.asscalar() in Python

The numpy.asscalar() function converts an array of size 1 to its scalar equivalent. Syntax numpy.asscalar(a) Parameter a: This parameter represents an input array of size 1. Return This function returns a scalar representation of parameter ‘a’. The output...

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.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.