
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
NumPy - Transposing Arrays
Transposing NumPy Array
By transposing an array in NumPy, we mean to rearrange the dimensions of an array to access its data along different axes.
For a 2-dimensional array (matrix), transposing means flipping the array along its diagonal. This swaps the rows and columns. If you have an array "A" with shape "(m, n)", the transpose "A.T" will have shape "(n, m)", where each element at position "(i, j)" in A will be at position "(j, i)" in A.T.
For arrays with more than two dimensions, transposing involves reordering the axes according to a specified order.
Transposing Arrays Using transpose() Function
The transpose() function in NumPy is used to rearrange the dimensions of an array. It returns a view of the array with its axes rearranged in a specified order.
If the order is not specified, the shape of the returned array is the same as the original array's shape, but with the dimensions permuted in reverse order. Following is the syntax −
numpy.transpose(a, axes=None)
Where,
a − It is the array-like object to be transposed.
axes (Optional) − It specifies the new order of axes. If not provided, it defaults to reversing the dimensions of the array.
Example: Transposing a 2D Array
In the following example, we are transposing a 2D array "arr" using the numpy.transpose() function with default parameters −
import numpy as np # 2D array arr = np.array([[1, 2, 3], [4, 5, 6]]) # Transposing the array transposed_arr = np.transpose(arr) print("Original Array:") print(arr) print("\nTransposed Array:") print(transposed_arr)
This swaps the rows and columns of the array as shown in the output below −
Original Array: [[1 2 3] [4 5 6]] Transposed Array: [[1 4] [2 5] [3 6]]
Example: Transposing a 3D Array
In here, we are transposing a 3D array "arr_3d" using the numpy.transpose() function with default parameters −
import numpy as np # 3D array arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Transposing a 3D array transposed_arr_3d = np.transpose(arr_3d) print("Original 3D Array:") print(arr_3d) print("\nTransposed 3D Array:") print(transposed_arr_3d)
This changes the order of dimensions, effectively rearranging the depth and height of the array as shown in the output below −
Original 3D Array: [[[1 2] [3 4]] [[5 6] [7 8]]] Transposed 3D Array: [[[1 5] [3 7]] [[2 6] [4 8]]]
Example: Transposing with Specified Axes
In the below example, we are rearranging the axes of a 3D array such that the first dimension (axis 0) remains unchanged, while axes "1" and "2" are swapped using the numpy.transpose() function −
import numpy as np # 3D array arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) # Transposing transposed_arr = np.transpose(arr, axes=(0, 2, 1)) print("Original 3D Array:") print(arr) print("\nTransposed 3D Array:",transposed_arr)
Following is the output obtained −
Original 3D Array: [[[1 2] [3 4]] [[5 6] [7 8]]] Transposed 3D Array: [[[1 3] [2 4]] [[5 7] [6 8]]]
Transposing Arrays Using "ndarray.T" Object
NumPy arrays have a convenient attribute ".T" that provides a quick way to transpose arrays without needing to call the transpose() function explicitly. In other words, it reverse the axes of multi-dimensional arrays without any additional arguments.
Example
In this example, we are using the .T attribute in NumPy to transpose the array "arr" −
import numpy as np # Creating a 2D array arr = np.array([[1, 2, 3], [4, 5, 6]]) # Transpose the array transposed_arr = arr.T print("Original Array:") print(arr) print("\nTransposed Array using .T:") print(transposed_arr)
The result produced is as follows −
Original Array: [[1 2 3] [4 5 6]] Transposed Array using .T: [[1 4] [2 5] [3 6]]