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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

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 Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.