Python Create a Random Orthogonal Matrix: A Beginner Guide – Python Tutorial

By | November 27, 2019

Orthogonal matrix is an important matrix in linear algebra, it is also widely used in machine learning. In this tutorial, we will dicuss what it is and how to create a random orthogonal matrix with pyhton.

What is Orthogonal Matrix?

If a matrix A is an orthogonal matrix, it shoud be n*n.

The feature of an orthogonal matrix A.

orthogonal matrix feature

The euclidean length of A.

orthogonal matrix euclidean length

How to create a random orthogonal matrix?

The simplest orthogonal matrix is one-hot encoding, such as:

[[1, 0, 0]
[0, 1, 0]
[0, 0, 1]]

However, the value in it is not random. How to create a random orthogonal matrix?

Here is an simple example, we will use python scipy to implement it.

from scipy.stats import ortho_group # Requires version 0.18 of scipy
import numpy as np

m = ortho_group.rvs(dim=5)

Here we will create a 5 * 5 random orthogonal matrix, it is:

[[-0.04861857 -0.44507735 -0.38079495  0.31292116 -0.74606833]
 [-0.20933804  0.4058631   0.35547015 -0.52018465 -0.62809365]
 [ 0.53353666  0.63968878 -0.53749448  0.05881791 -0.11737561]
 [ 0.45728819  0.08815114  0.66040851  0.55928113 -0.18488401]
 [ 0.67826246 -0.46926426  0.05997047 -0.56145645 -0.03035287]]

We check it is an orthogonal matrix or not.

l1 = np.matmul(m, m.T)
l2 = np.matmul(m.T, m)

The result is:

[[ 1.00000000e+00  7.84994566e-17  1.65829696e-16 -1.31158853e-16  -9.57636165e-18]
 [ 7.84994566e-17  1.00000000e+00 -1.98313914e-16  1.25646971e-16  5.00488907e-17]
 [ 1.65829696e-16 -1.98313914e-16  1.00000000e+00 -9.72148193e-17  -2.25065344e-17]
 [-1.31158853e-16  1.25646971e-16 -9.72148193e-17  1.00000000e+00  9.59854042e-17]
 [-9.57636165e-18  5.00488907e-17 -2.25065344e-17  9.59854042e-17  1.00000000e+00]]
[[ 1.00000000e+00  8.75665129e-17 -1.39245100e-16  1.47515708e-16  6.06719417e-17]
 [ 8.75665129e-17  1.00000000e+00 -4.07319982e-17  1.76818401e-17  -5.21988496e-17]
 [-1.39245100e-16 -4.07319982e-17  1.00000000e+00 -1.89462221e-16  2.42141102e-17]
 [ 1.47515708e-16  1.76818401e-17 -1.89462221e-16  1.00000000e+00  1.42728087e-16]
 [ 6.06719417e-17 -5.21988496e-17  2.42141102e-17  1.42728087e-16  1.00000000e+00]]

From the result, we will find the matrix m is a random orthogonal matrix.

2 thoughts on “Python Create a Random Orthogonal Matrix: A Beginner Guide – Python Tutorial

  1. Khan R

    the result is NOT orthogonal, the dot product between the two vectors is NOT 0

    1. admin Post author

      values, such as 6.06719417e-17 -5.21988496e-17 2.42141102e-17 1.42728087e-16, approach 0.


Leave a Reply

Your email address will not be published. Required fields are marked *