Beispiel #1
0
 def test_extended_laplacian_shortcut(self):
     """
     Demo commutativity of SVD and summation under certain conditions.
     """
     p = 100
     n = 7
     k = 3
     # define sets of arbitrary row indices
     first_rows = set((3, 5, 9))
     second_rows = set((12, 13, 2))
     # make a random data matrix
     X = np.random.random((p, n))
     # get a matrix that we are treating like the laplacian
     HDH = util.get_doubly_centered_matrix(np.corrcoef(X) ** 2)
     L = np.linalg.pinv(HDH)
     L_summed = util.sum_arbitrary_rows_and_columns(L, first_rows)
     L_summed = util.sum_arbitrary_rows_and_columns(L_summed, second_rows)
     U, S, VT = np.linalg.svd(L_summed, full_matrices=0)
     expected_fiedler = get_fiedler_vector(U, S)
     for f in (data_to_laplacian_sqrt, data_to_reduced_laplacian_sqrt):
         # get the square root of the laplacian without using pxp operations
         U, S = f(X)
         A = U*S
         # sum rows of the summed laplacian sqrt
         B = util.sum_arbitrary_rows(A, first_rows)
         # get the first criterion matrix
         U, S_array, VT = np.linalg.svd(B, full_matrices=0)
         QB = U*S_array
         # sum rows of the first criterion matrix
         QB_summed = util.sum_arbitrary_rows(QB, second_rows)
         # sum more rows of the summed laplacian sqrt
         C = util.sum_arbitrary_rows(B, second_rows)
         # get the second criterion matrix directly from the summed laplacian sqrt
         U, S_array, VT = np.linalg.svd(C, full_matrices=0)
         QC_direct_fiedler = get_fiedler_vector(U, S_array)
         QC_direct = U*S_array
         # get the second criterion matrix from the summed first criterion matrix
         U, S_array, VT = np.linalg.svd(QB_summed, full_matrices=0)
         QC_indirect_fiedler = get_fiedler_vector(U, S_array)
         QC_indirect = U*S_array
         # check the equivalence of the matrix squares
         self.assertAllClose(np.dot(QC_direct, QC_direct.T), L_summed)
         self.assertAllClose(np.dot(QC_indirect, QC_indirect.T), L_summed)
         self.assertAllClose(expected_fiedler, QC_direct_fiedler)
         self.assertAllClose(expected_fiedler, QC_indirect_fiedler)
Beispiel #2
0
 def test_laplacian_summation_shortcut(self):
     p = 100
     n = 7
     k = 3
     X = np.random.random((p, n))
     # get a matrix that we are treating like the laplacian
     HDH = util.get_doubly_centered_matrix(np.corrcoef(X) ** 2)
     L = np.linalg.pinv(HDH)
     L_summed = util.sum_last_rows_and_columns(L, k)
     U, S, VT = np.linalg.svd(L_summed, full_matrices=0)
     expected_fiedler = get_fiedler_vector(U, S)
     # get the square root of the summed laplacian without using pxp operations
     U, S = data_to_laplacian_sqrt(X)
     B = util.sum_last_rows(U*S, k)
     self.assertAllClose(np.dot(B, B.T), L_summed)
     # get another square root
     U, S = remove_small_vectors(U, S)
     B = util.sum_last_rows(U*S, k)
     U, S, VT = np.linalg.svd(B, full_matrices=0)
     observed_fiedler = get_fiedler_vector(U, S)
     self.assertAllClose(np.dot(U*S, (U*S).T), L_summed)
     self.assertAllClose(expected_fiedler, observed_fiedler)
Beispiel #3
0
 def test_fiedler(self):
     p = 100
     n = 7
     X = np.random.random((p, n))
     # get a matrix that we are treating like the laplacian
     HDH = util.get_doubly_centered_matrix(np.corrcoef(X) ** 2)
     L = np.linalg.pinv(HDH)
     U, S, VT = np.linalg.svd(L, full_matrices=0)
     expected_fiedler = get_fiedler_vector(U, S)
     # get the square root of the summed laplacian without using pxp operations
     U, S = data_to_laplacian_sqrt(X)
     self.assertAllClose(np.dot(U*S, (U*S).T), L)
     observed_fiedler = get_fiedler_vector(U, S)
     self.assertAllClose(expected_fiedler, observed_fiedler)
     # get another square root of the summed laplacian
     U, S = remove_small_vectors(U, S)
     self.assertAllClose(np.dot(U*S, (U*S).T), L)
     self.assertAllClose(expected_fiedler, observed_fiedler)
     # get yet another square root of the summed laplacian
     U, S, VT = np.linalg.svd(U*S, full_matrices=0)
     self.assertAllClose(np.dot(U*S, (U*S).T), L)
     self.assertAllClose(expected_fiedler, observed_fiedler)