How to Calculate the Rank of Matrix in Python using NumPy?

Learn to compute matrix rank effortlessly in Python with NumPy. Discover step-by-step instructions on calculating matrix rank. | ProjectPro

Understanding the rank of a matrix is fundamental in data science projects. It informs decisions related to dimensionality reduction, model construction, and the solvability of linear systems, ultimately contributing to the accuracy and reliability of data analysis and predictive modeling tasks. The rank essentially represents the maximum number of linearly independent rows or columns in the matrix. This tutorial provides practical examples and a process for computing a matrix's rank using NumPy

How to Find the Rank of a Matrix in Python? 

NumPy provides a simple method numpy.linalg.matrix_rank(), to compute the rank of a matrix. This function will output the rank of the matrix. However, installing NumPy is crucial if you haven't already (pip install numpy). 

Example - Get Rank of a Matrix Using NumPy 

Let’s now understand with an example - 

Step 1 - Loading Library

We have imported numpy which is needed.

    import numpy as np

Step 2 - Creating a Matrix

We have created a matrix using an np.array with different values.

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

                       [4, 5, 6, 25],

                       [7, 8, 9, 28],

                       [10, 11, 12, 41]])

Step 3 - Calculating the Rank of a Matrix in NumPy  

We have calculated the rank of the matrix by using the numpy function np.linalg.matrix_rank and passing the matrix through it.

    print("The Rank of a Matrix: ", np.linalg.matrix_rank(matrixA))

So, the output comes as

The Rank of a Matrix:  3 

Practice more NumPy Operations with ProjectPro! 

Learning to find matrix rank with NumPy in Python is essential for folks getting into data analysis. NumPy makes it easy with its operations.  But to get good at it, you must practice with real projects. That's where ProjectPro comes in handy. They've got over 250 projects on data science, big data, and data engineering. Working on these projects will help you learn by doing, which is the best way to get good at something. So, exploring ProjectPro's projects is an excellent place to start if you want to enhance your data analysis skills.

Download Materials

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

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

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.