Exemplo n.º 1
0
def svd_wrapper(X, rank=None):
    """
    Computes the (possibly partial) SVD of a matrix.

    Parameters
    ----------
    X: either dense or sparse
    rank: rank of the desired SVD (required for sparse matrices)

    Output
    ------
    U, D, V
    the columns of U are the left singular vectors
    the COLUMNS of V are the left singular vectors

    """

    if rank is not None and rank > min(X.shape):
        raise ValueError(
            'rank must be <= the smallest dimension of X. rank= {} was passed in while X.shape = {}'
            .format(rank, X.shape))

    if rank is None or rank == min(X.shape):
        U, D, V = full_svd(X, full_matrices=False)
        V = V.T
    else:
        scipy_svds = svds(X, rank)
        U, D, V = fix_scipy_svds(scipy_svds)

    return U, D, V
Exemplo n.º 2
0
def svd_wrapper(X, rank=None):
    """
    Computes the (possibly partial) SVD of a matrix. Handles the case where
    X is either dense or sparse.

    Parameters
    ----------
    X: array-like,  shape (N, D)

    rank: int, None
        rank of the desired SVD.
        If None, will compute the largest min(X.shape) singular value/vectors.

    Output
    ------
    U, D, V

    U: array-like, shape (N, rank)
        Orthonormal matrix of left singular vectors.

    D: list, shape (rank, )
        Singular values in non-increasing order (e.g. D[0] is the largest).

    V: array-like, shape (D, rank)
        Orthonormal matrix of right singular vectors

    """
    # TODO: give user option to compute randomized SVD

    if rank is None:
        rank = min(X.shape)

    rank = int(rank)
    assert 1 <= rank and rank <= min(X.shape)

    if rank <= min(X.shape) - 1:
        scipy_svds = svds(X, rank)
        U, D, V = fix_scipy_svds(scipy_svds)

    else:
        assert not issparse(X)

        U, D, V = full_svd(X, full_matrices=False)
        V = V.T

        if rank:
            U = U[:, :rank]
            D = D[:rank]
            V = V[:, :rank]

    # enfoce deterministic output
    U, V = svd_flip(U, V.T)
    V = V.T

    return U, D, V
Exemplo n.º 3
0
def svd_wrapper(X, rank=None):
    """
    Computes the (possibly partial) SVD of a matrix. Handles the case where
    X is either dense or sparse.

    Parameters
    ----------
    X: array-like,  shape (N, D)

    rank: rank of the desired SVD (required for sparse matrices)

    Output
    ------
    U, D, V

    U: array-like, shape (N, rank)
        Orthonormal matrix of left singular vectors.

    D: list, shape (rank, )
        Singular values in non-increasing order (e.g. D[0] is the largest).

    V: array-like, shape (D, rank)
        Orthonormal matrix of right singular vectors

    """
    full = False
    if rank is None or rank == min(X.shape):
        full = True

    if isinstance(X, LinearOperator):
        scipy_svds = svds(convert2scipy(X), rank)
        U, D, V = fix_scipy_svds(scipy_svds)

    elif issparse(X) or not full:
        assert rank <= min(X.shape) - 1  # svds cannot compute the full svd
        scipy_svds = svds(X, rank)
        U, D, V = fix_scipy_svds(scipy_svds)

    else:
        U, D, V = full_svd(X, full_matrices=False)
        V = V.T

        if rank:
            U = U[:, :rank]
            D = D[:rank]
            V = V[:, :rank]

    return U, D, V
Exemplo n.º 4
0
def svd_wrapper(X, rank=None):
    r"""
    Computes the full or partial SVD of a matrix. Handles the case where
    X is either dense or sparse.

    Parameters
    ----------
    X: array-like
        - X shape: shape(N, D)

    rank: int
        rank of the desired SVD

    Returns
    -------
    U: array-like
        - U shape: shape(N, rank)
        Orthonormal matrix of left singular vectors.

    D: list
        - (rank,)
        Singular values in decreasing order

    V: array-like
        - V shape: (D, rank)
        Orthonormal matrix of right singular vectors

    """
    full = False
    if rank is None or rank == min(X.shape):
        full = True

    if issparse(X) or not full:
        assert rank <= min(X.shape) - 1  # svds cannot compute the full svd
        scipy_svds = svds(X, rank)
        U, D, V = fix_scipy_svds(scipy_svds)

    else:
        U, D, V = full_svd(X, full_matrices=False)
        V = V.T

        if rank:
            U = U[:, :rank]
            D = D[:rank]
            V = V[:, :rank]

    return U, D, V
Exemplo n.º 5
0
Arquivo: linalg.py Projeto: idc9/mvmm
def safe_tsym_svd(X, rank=None, full=False):
    """
    Safely computes the SVD of Tsym.
    Computing the low rank SVD of Tsym sometimes results in arpack errors
    so we may have to resort to computing full SVD as back up.

    Parameters
    ----------
    X: array-like, (n_rows, n_cols)
        The data matrix.

    rank: int, None
        The rank of the SVD to compute.

    full: bool
        If True, computes the "full" singular vectors i.e. both singular vectors are orthonormal matrices.

    Output
    ------
    U, D, V

    """
    # SVD of Tsym
    Tsym = get_Tsym(X)

    if full:
        assert rank is None
        U, D, V = full_svd(Tsym, full_matrices=True)
        V = V.T

    else:
        try:
            # for some reason ArpackNoConvergence sometimes fails to converge
            # for low rank
            U, D, V = svd_wrapper(Tsym, rank=rank)

        except (ArpackNoConvergence, TypeError, RuntimeError, ArpackError):

            U, D, V = svd_wrapper(Tsym, rank=None)

            U = U[:, 0:rank]
            D = D[0:rank]
            V = V[:, 0:rank]

    return U, D, V