def remove_small_vectors(U, S): """ Remove negligible values in S and corresponding columns in U. The inputs are not directly modified, but the results are returned. @param U: columns are eigenvectors @param S: conformant singular values @return: U_reduced, S_reduced """ columns = [U.T[i] for i, w in enumerate(S) if not util.is_small(w)] U_reduced = np.vstack(columns).T S_reduced = [w for w in S if not util.is_small(w)] return U_reduced, S_reduced
def get_fiedler_vector(U, S): """ The first element of the output vector is forced to be nonnegative. @param U: columns are eigenvectors @param S: conformant singular values @return: the eigenvector corresponding to the smallest nonsmall eigenvalue """ best_w, best_i = min((w, i) for i, w in enumerate(S) if not util.is_small(w)) v = U.T[best_i] if v[0] < 0: return -v else: return v
def data_to_laplacian_sqrt(X): """ If the output is U, S then (U*S)(U*S)' is like a Laplacian. @param X: a data matrix @return: U, S """ logging.debug('data_to_laplacian_sqrt: creating the standardized matrix') Z = get_standardized_matrix(X) logging.debug('data_to_laplacian_sqrt: creating the augmented matrix') Q = standardized_to_augmented_C(Z) logging.debug('data_to_laplacian_sqrt: creating the column centered matrix') W = util.get_column_centered_matrix(Q) logging.debug('data_to_laplacian_sqrt: manually cleaning up old matrices') del Z del Q logging.debug('data_to_laplacian_sqrt: doing a singular value decomposition') U, S_array, VT = np.linalg.svd(W, full_matrices=0) S_pinv_array = np.array([0 if util.is_small(x) else 1/x for x in S_array]) return U, S_pinv_array