Numpy Diag Method Implementation in Python

Numpy Diag Method Implementation in Python featured imageNumpy Diag Method Implementation in Python featured image

Diagonal Matrix helps you to find the solution to any matrix easily. In fact, It’s very useful in the determination of eigenvectors and eigenvalues of a (square) matrix. Eigenvectors and Eigenvalues help in understanding the geometry of the underlying linear transformation. In this entire tutorial, you will know how to get diagonal matrix elements using the NumPy diag method.

But before going to the coding demonstration part, first learn the syntax of the numpy.diag() method.

numpy.diag(v, k=0)

Here you will use only two parameters. The one is the v to get your input array. And the second is k. It allows you to get elements above or below the main diagonal matrix

Implementation of the Numpy Diag Method

Example 1: Finding the main diagonal elements

In this example, I will take an input matrix. After that will numpy.diag() to find the main diagonal elements. To do so you have to just pass your input array. Execute the lines of code given below to get the elements.

import numpy as np
array = np.arange(16).reshape((4,4))
print("Original Matrix \n")
print(array)
print("\nMain Diagonal Matrix \n")
print(np.diag(array))

Output

Main Diagonal Matrix
Main Diagonal Matrix

Example 2: Upper Elements using NumPy diag method

The numpy diag method is useful in finding the upper elements of a matrix. In this example, I will show you how to find the elements above diagonal elements.  To do so you have to pass an extra parameter and it is k =1. Just execute the below lines of code and see the output.

import numpy as np
array = np.arange(16).reshape((4,4))
print("Original Matrix \n")
print(array)
print("\nUpper Elements \n")
print(np.diag(array,k=1))

Here you can see I am passing the array and k=1 inside the same np. diag() method. It will find the elements above the main diagonal matrix. You can see the output.

Output

Upper Elements above the Main Diagonal
Upper Elements above the Main Diagonal

Example 3: Find elements below the main diagonal

In the above example, I have used k =1 to find the upper elements. Here to find elements below the main diagonal I will use k = -1.

import numpy as np
array = np.arange(16).reshape((4,4))
print("Original Matrix \n")
print(array)
print("\nLower Elements \n")
print(np.diag(array,k=-1))

Output

Lower Elements above the Main Diagonal
Lower Elements above the Main Diagonal

In examples 2 and 3, you can play with the value of k. It allows you to select the column from where you want the diagonal elements.

That’s all for now. These are examples I have compiled for you to implement numpy diag() method. If you are not able to understand it then you can contact us for more help.

Source:

Official Diag Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner