How to create random orthonormal matrix in python numpy

前端 未结 6 1426
南方客
南方客 2020-12-29 23:25

Is there a method that I can call to create a random orthonormal matrix in python? Possibly using numpy? Or is there a way to create a orthonormal matrix using multiple nump

6条回答
  •  误落风尘
    2020-12-29 23:55

    An easy way to create any shape (n x m) orthogonal matrix:

    import numpy as np
    
    n, m = 3, 5
    
    H = np.random.rand(n, m)
    u, s, vh = np.linalg.svd(H, full_matrices=False)
    mat = u @ vh
    
    print(mat @ mat.T) # -> eye(n)
    

    Note that if n > m, it would obtain mat.T @ mat = eye(m).

提交回复
热议问题