<s> import numpy as np <EOL> from numpy . testing import assert_almost_equal <EOL> from menpo . math import eigenvalue_decomposition , pca , ipca <EOL> cov_matrix = np . array ( [ [ <NUM_LIT:3> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:3> ] ] ) <EOL> large_samples_data_matrix = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:0.5> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:1.0> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> centered_eigenvectors_s = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ - <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> non_centered_eigenvectors_s = np . array ( [ [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ - <NUM_LIT> , <NUM_LIT> ] ] ) <EOL> mean_vector_s = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> eigenvalues_no_centre_s = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> eigenvalues_centered_s = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> centered_eigenvectors_f = np . array ( [ [ - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <EOL> - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> - <NUM_LIT> ] ] ) <EOL> non_centered_eigenvectors_f = np . array ( <EOL> [ [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> , <NUM_LIT> , <EOL> - <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , - <NUM_LIT> ] ] ) <EOL> mean_vector_f = np . array ( [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:1.> ] ) <EOL> eigenvalues_no_centre_f = np . array ( [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> eigenvalues_centered_f = np . array ( [ <NUM_LIT> ] ) <EOL> def pcd_samples_nocentre_test ( ) : <EOL> output = pca ( large_samples_data_matrix , centre = False ) <EOL> eigenvectors , eigenvalues , mean_vector = output <EOL> assert_almost_equal ( eigenvalues , eigenvalues_no_centre_s ) <EOL> assert_almost_equal ( eigenvectors , non_centered_eigenvectors_s ) <EOL> assert_almost_equal ( mean_vector , [ <NUM_LIT:0.0> , <NUM_LIT:0.0> ] ) <EOL> def pcd_samples_yescentre_test ( ) : <EOL> output = pca ( large_samples_data_matrix , centre = True ) <EOL> eigenvectors , eigenvalues , mean_vector = output <EOL> assert_almost_equal ( eigenvalues , eigenvalues_centered_s ) <EOL> assert_almost_equal ( eigenvectors , centered_eigenvectors_s ) <EOL> assert_almost_equal ( mean_vector , mean_vector_s ) <EOL> def pcd_features_nocentre_test ( ) : <EOL> output = pca ( large_samples_data_matrix . T , centre = False ) <EOL> eigenvectors , eigenvalues , mean_vector = output <EOL> assert_almost_equal ( eigenvalues , eigenvalues_no_centre_f ) <EOL> assert_almost_equal ( eigenvectors , non_centered_eigenvectors_f ) <EOL> assert_almost_equal ( mean_vector , np . zeros ( <NUM_LIT:10> ) ) <EOL> def pcd_features_nocentre_inplace_test ( ) : <EOL> output = pca ( large_samples_data_matrix . T . copy ( ) , centre = False , <EOL> inplace = True ) <EOL> eigenvectors , eigenvalues , mean_vector = output <EOL> assert_almost_equal ( eigenvalues , eigenvalues_no_centre_f ) <EOL> assert_almost_equal ( eigenvectors , non_centered_eigenvectors_f ) <EOL> assert_almost_equal ( mean_vector , np . zeros ( <NUM_LIT:10> ) ) <EOL> def pcd_features_yescentre_test ( ) : <EOL> output = pca ( large_samples_data_matrix . T , centre = True ) <EOL> eigenvectors , eigenvalues , mean_vector = output <EOL> assert_almost_equal ( eigenvalues , eigenvalues_centered_f ) <EOL> assert_almost_equal ( eigenvectors , centered_eigenvectors_f ) <EOL> assert_almost_equal ( mean_vector , mean_vector_f ) <EOL> def eigenvalue_decomposition_default_epsilon_test ( ) : <EOL> pos_eigenvectors , pos_eigenvalues = eigenvalue_decomposition ( cov_matrix ) <EOL> assert_almost_equal ( pos_eigenvalues , [ <NUM_LIT> , <NUM_LIT> ] ) <EOL> sqrt_one_over_2 = np . sqrt ( <NUM_LIT> ) / <NUM_LIT> <EOL> assert_almost_equal ( pos_eigenvectors , [ [ sqrt_one_over_2 , - sqrt_one_over_2 ] , <EOL> [ sqrt_one_over_2 , sqrt_one_over_2 ] ] ) <EOL> def eigenvalue_decomposition_large_epsilon_test ( ) : <EOL> pos_eigenvectors , pos_eigenvalues = eigenvalue_decomposition ( cov_matrix , <EOL> eps = <NUM_LIT:0.5> ) <EOL> assert_almost_equal ( pos_eigenvalues , [ <NUM_LIT> ] ) <EOL> sqrt_one_over_2 = np . sqrt ( <NUM_LIT> ) / <NUM_LIT> <EOL> assert_almost_equal ( pos_eigenvectors , <EOL> [ [ sqrt_one_over_2 ] , [ sqrt_one_over_2 ] ] ) <EOL> def ipca_samples_yescentre_test ( ) : <EOL> n_a = large_samples_data_matrix . shape [ <NUM_LIT:0> ] / <NUM_LIT:2> <EOL> A = large_samples_data_matrix [ : n_a , : ] <EOL> U_a , l_a , m_a = pca ( A , centre = True ) <EOL> B = large_samples_data_matrix [ n_a : , : ] <EOL> i_U , i_l , i_m = ipca ( B , U_a , l_a , n_a , m_a = m_a ) <EOL> b_U , b_l , b_m = pca ( large_samples_data_matrix , centre = True ) <EOL> assert_almost_equal ( np . abs ( i_U ) , np . abs ( b_U ) ) <EOL> assert_almost_equal ( i_l , b_l ) <EOL> assert_almost_equal ( i_m , b_m ) <EOL> def ipca_samples_nocentre_test ( ) : <EOL> n_a = large_samples_data_matrix . shape [ <NUM_LIT:0> ] / <NUM_LIT:2> <EOL> A = large_samples_data_matrix [ : n_a , : ] <EOL> U_a , l_a , m_a = pca ( A , centre = False ) <EOL> B = large_samples_data_matrix [ n_a : , : ] <EOL> i_U , i_l , i_m = ipca ( B , U_a , l_a , n_a , m_a = m_a ) <EOL> b_U , b_l , b_m = pca ( large_samples_data_matrix , centre = False ) <EOL> assert_almost_equal ( np . abs ( i_U ) , np . abs ( b_U ) ) <EOL> assert_almost_equal ( i_l , b_l ) <EOL> assert_almost_equal ( i_m , b_m ) <EOL> def ipca_features_yescentre_test ( ) : <EOL> C = np . vstack ( ( large_samples_data_matrix . T , large_samples_data_matrix . T ) ) <EOL> n_a = C . shape [ <NUM_LIT:0> ] / <NUM_LIT:2> <EOL> A = C [ : n_a , : ] <EOL> U_a , l_a , m_a = pca ( A , centre = True ) <EOL> B = C [ n_a : , : ] <EOL> i_U , i_l , i_m = ipca ( B , U_a , l_a , n_a , m_a = m_a ) <EOL> b_U , b_l , b_m = pca ( C , centre = True ) <EOL> assert_almost_equal ( np . abs ( i_U ) , np . abs ( b_U ) ) <EOL> assert_almost_equal ( i_l , b_l ) <EOL> assert_almost_equal ( i_m , b_m ) <EOL> def ipca_features_nocentre_test ( ) : <EOL> C = np . vstack ( ( large_samples_data_matrix . T , large_samples_data_matrix . T ) ) <EOL> n_a = C . shape [ <NUM_LIT:0> ] / <NUM_LIT:2> <EOL> A = C [ : n_a , : ] <EOL> U_a , l_a , m_a = pca ( A , centre = False ) <EOL> B = C [ n_a : , : ] <EOL> i_U , i_l , i_m = ipca ( B , U_a , l_a , n_a , m_a = m_a ) <EOL> b_U , b_l , b_m = pca ( C , centre = False ) <EOL> assert_almost_equal ( np . abs ( i_U ) , np . abs ( b_U ) ) <EOL> assert_almost_equal ( i_l , b_l ) <EOL> assert_almost_equal ( i_m , b_m ) </s>
