NumPy Diagonal Matrix: How to Diagonalize a Matrix in NumPy?

This recipe on NumPy Diagonal Matrix offers practical examples, tips, and tricks to efficiently diagonalize matrices using NumPy. | ProjectPro

NumPy provides versatile tools for working with matrices. Diagonalization is a fundamental concept in linear algebra, allowing us to simplify the representation of matrices. This numpy code example helps you understand how to create and diagonalize matrices using NumPy, focusing on practical examples and Python code. 

How to Find the Diagonal Elements of a Matrix Using NumPy?  

Check below the step-by-step instructions on how to find the diagonal elements of a matrix using NumPy - 

Step 1 - Import the library

    import numpy as np

We have only imported numpy which is needed.

Step 2 - Creating a matrix

We have created a matrix on which we will perform the operation.

    matrix = np.array([[1, 2, 3, 23],

                       [4, 5, 6, 25],

                       [7, 8, 9, 28],

                       [10, 11, 12, 41]])

Step 3 - Finding elements

We can find diagonal elements by the function diagonal and by using the sum function we can find the sum of the elements.

    print(matrix.diagonal())    

    print(matrix.diagonal().sum())

So the output comes as

[ 1  5  9 41]

56

NumPy Diagonal Matrix Example 

Creating a diagonal matrix is a common operation in linear algebra. A diagonal matrix is a special type of square matrix where all off-diagonal elements are zero, and only the elements on the main diagonal have non-zero values. NumPy simplifies the creation of diagonal matrices with the numpy.diag() function.

NumPy Diagonal Matrix Example

How to Diagonalize a Matrix in NumPy? 

Diagonalizing a matrix involves finding a diagonal matrix that is similar to the original matrix. A square matrix A if there exists an invertible matrix P such that P-1AP is a diagonal matrix. NumPy provides the numpy.linalg.eig() function to compute eigenvalues and eigenvectors, which is essential for diagonalization.

NumPy Diagonalize Matrix Example 

For example, we create a 2x2 matrix, compute its eigenvalues and eigenvectors using numpy.linalg.eig(), construct a diagonal matrix using the eigenvalues, and finally diagonalize the original matrix. The resulting output will display the original matrix, eigenvalues, eigenvectors, diagonal matrix, and the diagonalized matrix.

NumPy Diagonalize Matrix

Showing the output

NumPy Diagonalize matrix example

Practice Matrix Operations in NumPy with ProjectPro! 

Diagonalizing a matrix is a crucial skill for anyone entering into the field of data science and beyond. The theoretical knowledge gained from this exploration is undeniably valuable, but the true expertise comes from practical application. Embracing real-world projects not only reinforces your understanding of NumPy and matrix operations but also equips you with the hands-on experience needed to excel in the field. ProjectPro provides a platform where you can find more projects to practice and enhance your data science skills further. It offers a diverse repository of over 270+ projects rooted in data science and big data.  

Download Materials

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

Build Piecewise and Spline Regression Models in Python
In this Regression Project, you will learn how to build a piecewise and spline regression model from scratch in Python to predict the points scored by a sports team.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.