# Numpy (.T) – Obtain the Transpose of a Matrix When considering complex computations in scientific computing, data analysis, and manipulation, matrices play a very important role in storing data and performing certain calculations. The properties of a matrix play a significant role in this process. One such property of a matrix is its Transpose.

To define in simple terms, the transpose of a matrix is another matrix with interchanged rows and columns. If you change the positions of the rows and columns of a matrix, you get its transpose. Although this sounds simple, it is a bit messy process to compute a transpose for higher dimensional matrices.

Luckily, we have a function in the Numpy library that computes the transpose of a given matrix or array.

Let us dive deep into the transpose of a matrix and the numpy library!

## The Numpy Library

The numpy library is the most basic library of the Python language and is often considered as the building block of other libraries. Libraries for example Pandas, leverage the power of the numpy library to carry out data-related tasks. The fundamental unit of the numpy library is the `ndarr`ay, which stands for an n-dimensional array. With numpy, we can store matrices of any order in the form of ndarray.

## What Is the Transpose of a Matrix?

Consider a nxn matrix or in this case, a ndarray. Now if you interchange the rows and columns of the array, you get the transpose. All we need to do is write the elements of a row in the form of a column and vice versa.

The transpose can be obtained for both square matrices and rectangular matrices.

Let us understand with the help of an example.

Observe the colors of the original matrix. Compare the rows of the original matrix with the transpose. The colors present in a row are now represented by the columns. That is what we do in a transpose matrix. We take the elements of a row and spread them across a column.

Related : Cholesky Decomposition of a matrix.

Now that we have understood what is a transpose matrix, let us learn its syntax in the numpy library and look at some examples.

But before that, we need to be clear with a few terms.

## Square Matrix and Irregular Matrix

A square matrix is a matrix that has an equal number of rows and columns.

An example of a square matrix is shown below.

```[[1 2 3 ],
[4 5 6],
[7 8 9]]
```

Whereas an irregular matrix doesn’t have equal rows and columns.

```Example:
[[1,2,3,4],
[5,6,7,8],
[11,13,14,20]]
```

## Exploring the .T Syntax

Under the .T method, we have two properties. One for ndarrays and one for matrices. Let us see the syntaxes for both.

### 1.ndarray.T

This function is used to view the transpose of a given ndarray.

The syntax of this function is fairly simple. If the array you are considering is x, this function follows the given syntax.

```transpose = x.T
```

Let us take a simple square matrix to understand the transpose of a ndarray.

```import numpy as np
x = np.array([[1,2,3],[3,4,5],[11,2,4]])
print("The original matrix is:\n",x)
tr = x.T
print("The transpose of the matrix is:\n",tr)
```

In the first line, we import the numpy library with its alias name np. `x` is the original 3×3 matrix we wish to calculate the transpose of. In the third line, we are printing the original matrix. `tr` is the transpose of the matrix.

The output is given below.

### 2.matrix.T

This is a property of a matrix that returns the transpose of the matrix. There are two ways to compute the transpose of a matrix.

### get.T()

According to the official documentation, the transpose of a matrix can be computed using the getT function. The syntax is as followed.

```x.getT()
```

Let us see an example of this approach. Let us take an irregular matrix and check if we can compute its transpose.

```m = np.matrix([[1,2,3],[11,4,5],[12,8,9],[10,5,6]])
print("The original matrix is:\n",m)
tr = m.getT()
print("The transpose of the matrix is:\n",tr)
```

`m` is the original matrix of order 3×4. Now if we compute the transpose of the matrix, it should result in a matrix of order 4×3 because we are interchanging the number of rows and columns.

### matrix.T

The second method is to just use the .T attribute. Let us take the same example.

```import numpy as np
m = np.matrix([[1,2,3],[11,4,5],[12,8,9],[10,5,6]])
print("The original matrix is:\n",m)
tr = m.T
print("The transpose of the matrix is:\n",tr)
```

We are taking the same matrix m and computing its transpose. The transpose of the matrix is stored in the variable tr.

Let us see the output.

## Applications of Transposition/ Transpose Matrix

Now that we have understood what a transpose is, let us see where we actually use the transposition of a matrix. The most used real-life application of transposition is `auto-rotate` option in our mobile phones.

• Image Processing: Transposition in image processing is used to rotate images, reshape the image data, and feature extraction because after all, an image is a collection of pixels stored in the form of a matrix
• Statistical analysis: Transposition is incorporated in statistical programming for tasks like data manipulation, model fitting, and statistical analysis. It is used for reshaping datasets, converting between wide and long formats, and rearranging variables. Transposing matrices can help in preparing data for statistical modeling, conducting multivariate analyses, or performing operations like matrix factorization or dimensionality reduction
• Solving system of linear equations: Transposition of matrices can be used to solve a system of linear equations using techniques like Gaussian or factorization
• Machine Learning: In machine learning, especially genetic algorithms, we use transposition techniques to mimic crossover and mutation operations

## Conclusion

To conclude, we have learned about the transposition of a matrix and its definition. We have learned about the numpy library, which assists the transpose function in Python.

We have seen the two types of .T, one for ndarrays and one for matrices. We have understood each method with examples and also observed how transpose is computed for square and irregular matrices.

Lastly, we have observed a few real-life uses of transposition in data analysis, image processing, and machine learning.

## References

Know more about the transpose of a numpy array here

Matrix transpose documentation