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

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

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 Customer Propensity to Purchase Model in Python
In this machine learning project, you will learn to build a machine learning model to estimate customer propensity to purchase.

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.