#! /usr/bin/env python3 # def pca_eigen_6x4 ( ): #*****************************************************************************80 # ## pca_eigen_6x4 uses an eigenvalue approach for PCA of a random 6x4 matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 24 June 2019 # # Author: # # John Burkardt # import matplotlib.pyplot as plt import numpy as np import platform print ( '' ) print ( 'pca_eigen_6x4:' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Create a random 6x4 matrix.' ) print ( ' Get the eigenvalue factorization of A\' A' ) print ( ' Get the SVD factorization of A.' ) m = 6 n = 4 A = np.random.randn ( 6, 4 ) print ( '' ) print ( ' Matrix A:' ) print ( A ) # # Compute A'A. # ATA = np.matmul ( A.T, A ) print ( ' Matrix ATA:' ) print ( ATA ) # # Get the eigenvalue decomposition of A' A. # L, X = np.linalg.eig ( ATA ) # # Check that A' A * X1 = L1 * X1. # prod = np.matmul ( ATA, X[:,0] ) - L[0] * X[:,0] diff = np.linalg.norm ( prod ) print ( ' Norm of A\'*A*X1 - L1 * X1 = %g' % ( diff ) ) # # Check that A' A * X = X * L. # prod = np.matmul ( ATA, X ) - np.matmul ( X, np.diag ( L ) ) diff = np.linalg.norm ( prod ) print ( ' Norm of A\'*A*X - L * X = %g' % ( diff ) ) # # Terminate. # print ( '' ) print ( 'pca_eigen_6x4:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): pca_eigen_6x4 ( )