• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / How to do QR Decomposition in Python with Numpy

How to do QR Decomposition in Python with Numpy

December 28, 2022 by cmdlinetips

QR decomposition, also known as QR factorization, is a method for decomposing a matrix into the product of an orthogonal matrix and an upper-triangular matrix. It is a useful tool for solving systems of linear equations, computing the inverse of a matrix, and computing the singular value decomposition (SVD) of a matrix.

In this blog post, we will learn how to perform QR decomposition using the NumPy library in Python. We will start with by introducing the basic concept of QR decomposition and its applications, and then show how to use Numpy’s linaalg.qr() function factorize/decompse with an example.

What is QR decomposition?

Given a matrix A, the QR decomposition is a way of decomposing or factorizing the matrix A into the product of two matrices Q and R such that:

A = QR

Here Q is an orthogonal matrix and R is an upper-triangular matrix.

An orthogonal matrix is a matrix whose columns and rows are orthonormal, which means that they are unit vectors (vectors with a length of 1) that are perpendicular to each other. An upper-triangular matrix is a matrix that has all of its elements below the main diagonal equal to zero.

QR decomposition has several important properties that make it a useful tool for various matrix operations. For example, Q is an invertible matrix, which means that it has a well-defined inverse. This property makes QR decomposition useful for solving systems of linear equations.

NumPy’s linalg module provides a function called qr() that can be used to perform QR decomposition on a given matrix.

Here is an example of how to use the linalg.qr() function to decompose a matrix A into Q and R:

import numpy as np

Here is the basic syntax of linalg.qr() function in Numpy. In addition to the input matrix, linalg.qr() function takes the argument mode. The mode argument determines what information will be returned by the function and by default it is set to “reduced”.

linalg.qr(a, mode='reduced')

Decomposing a 3×3 matrix with QR decomposition in Python

Let us consider an example matrix to decompose using QR factorization method.

A = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

We will is Numpy’s linalg.qr() function to decompose the matrix A into the matrices Q and R. The qr() function returns the matrices Q and R as a tuple, so we can unpack them into separate variables using tuple unpacking.

The Q matrix returned by the qr() function is an orthogonal matrix, and the R matrix is an upper-triangular matrix. We can verify this by printing the matrices and checking that the rows of Q are orthonormal and that the R matrix has all of its elements below the main diagonal equal to zero.

# QR decomposition in Python
Q, R = np.linalg.qr(A)
print(Q)

[[-0.12309149  0.90453403  0.40824829]
 [-0.49236596  0.30151134 -0.81649658]
 [-0.86164044 -0.30151134  0.40824829]]

Verifying Q’s orthogonal property

The matrix Q is an orthogonal matrix. An orthogonal matrix has a number of special properties. Here we check one such property, i.e. dot product of an orthogonal matrix and its transpose give us identity matrix with ones along the diagonal and zeros everywhere else.

np.dot(Q, Q.T)

array([[1.00000000e+00, 1.70541764e-16, 1.71911902e-16],
       [1.70541764e-16, 1.00000000e+00, 8.19505222e-17],
       [1.71911902e-16, 8.19505222e-17, 1.00000000e+00]])
print(R)

[[-8.12403840e+00 -9.60113630e+00 -1.10782342e+01]
 [ 0.00000000e+00  9.04534034e-01  1.80906807e+00]
 [ 0.00000000e+00  0.00000000e+00 -8.88178420e-16]]

And we can also verify the product of Q and R give us the original matrix A.

np.dot(Q,R)

array([[1., 2., 3.],
       [4., 5., 6.],
       [7., 8., 9.]])

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to do Cholesky Matrix Decomposition with Numpy Default Thumbnail9 Basic Linear Algebra Operations with NumPy Default ThumbnailHow to Compute Matrix inverse with Numpy Default ThumbnailHow to solve system of linear equations with Numpy

Filed Under: Numpy Tips, Python, Python Tips, QR Decomposition Tagged With: Numpy linalg.qr()

Reader Interactions

Trackbacks

  1. Linear Regression with Matrix Decomposition Methods - Python and R Tips says:
    January 2, 2023 at 11:04 am

    […] QR decomposition, also known as QR factorization, is one of the methods for decomposing a matrix A into the product of two matrices Q and R, where Q is an orthogonal matrix (meaning that its columns are orthogonal and have unit length) and R is an upper triangular matrix (meaning that all entries below the main diagonal are zero). […]

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version