(A, n_components, n_oversamples=10, n_iter="auto",
                   flip_sign=True, random_state=0)
return U[:, :n_components], s[:n_components], V[:n_components, :]
Compute the randomized PCA decomposition of a given matrix.

    This method differs from the scikit-learn implementation in that it supports
    and handles sparse matrices well.
Compute the randomized PCA decomposition of a given matrix.
Compute the randomized PCA decomposition of a given matrix .
def randomized_pca(A, n_components, n_oversamples=10, n_iter="auto",
                   flip_sign=True, random_state=0):
    """Compute the randomized PCA decomposition of a given matrix.

    This method differs from the scikit-learn implementation in that it supports
    and handles sparse matrices well.

    """
    if n_iter == "auto":
        # Checks if the number of iterations is explicitly specified
        # Adjust n_iter. 7 was found a good compromise for PCA. See sklearn #5299
        n_iter = 7 if n_components < .1 * min(A.shape) else 4

    n_samples, n_features = A.shape

    c = np.atleast_2d(ut.nanmean(A, axis=0))

    if n_samples >= n_features:
        Q = random_state.normal(size=(n_features, n_components + n_oversamples))
        if A.dtype.kind == "f":
            Q = Q.astype(A.dtype, copy=False)

        Q = safe_sparse_dot(A, Q) - safe_sparse_dot(c, Q)

        # Normalized power iterations
        for _ in range(n_iter):
            Q = safe_sparse_dot(A.T, Q) - safe_sparse_dot(c.T, Q.sum(axis=0)[None, :])
            Q, _ = lu(Q, permute_l=True)
            Q = safe_sparse_dot(A, Q) - safe_sparse_dot(c, Q)
            Q, _ = lu(Q, permute_l=True)

        Q, _ = qr(Q, mode="economic")

        QA = safe_sparse_dot(A.T, Q) - safe_sparse_dot(c.T, Q.sum(axis=0)[None, :])
        R, s, V = svd(QA.T, full_matrices=False)
        U = Q.dot(R)

    else:  # n_features > n_samples
        Q = random_state.normal(size=(n_samples, n_components + n_oversamples))
        if A.dtype.kind == "f":
            Q = Q.astype(A.dtype, copy=False)

        Q = safe_sparse_dot(A.T, Q) - safe_sparse_dot(c.T, Q.sum(axis=0)[None, :])

        # Normalized power iterations
        for _ in range(n_iter):
            Q = safe_sparse_dot(A, Q) - safe_sparse_dot(c, Q)
            Q, _ = lu(Q, permute_l=True)
            Q = safe_sparse_dot(A.T, Q) - safe_sparse_dot(c.T, Q.sum(axis=0)[None, :])
            Q, _ = lu(Q, permute_l=True)

        Q, _ = qr(Q, mode="economic")

        QA = safe_sparse_dot(A, Q) - safe_sparse_dot(c, Q)
        U, s, R = svd(QA, full_matrices=False)
        V = R.dot(Q.T)

    if flip_sign:
        U, V = svd_flip(U, V)

    return U[:, :n_components], s[:n_components], V[:n_components, :]
def randomized_pca ( A , n_components , n_oversamples = 10 , n_iter = "auto" , flip_sign = True , random_state = 0 ) : if n_iter == "auto" : # Checks if the number of iterations is explicitly specified # Adjust n_iter. 7 was found a good compromise for PCA. See sklearn #5299 n_iter = 7 if n_components < .1 * min ( A . shape ) else 4 n_samples , n_features = A . shape c = np . atleast_2d ( ut . nanmean ( A , axis = 0 ) ) if n_samples >= n_features : Q = random_state . normal ( size = ( n_features , n_components + n_oversamples ) ) if A . dtype . kind == "f" : Q = Q . astype ( A . dtype , copy = False ) Q = safe_sparse_dot ( A , Q ) - safe_sparse_dot ( c , Q ) # Normalized power iterations for _ in range ( n_iter ) : Q = safe_sparse_dot ( A . T , Q ) - safe_sparse_dot ( c . T , Q . sum ( axis = 0 ) [ None , : ] ) Q , _ = lu ( Q , permute_l = True ) Q = safe_sparse_dot ( A , Q ) - safe_sparse_dot ( c , Q ) Q , _ = lu ( Q , permute_l = True ) Q , _ = qr ( Q , mode = "economic" ) QA = safe_sparse_dot ( A . T , Q ) - safe_sparse_dot ( c . T , Q . sum ( axis = 0 ) [ None , : ] ) R , s , V = svd ( QA . T , full_matrices = False ) U = Q . dot ( R ) else : # n_features > n_samples Q = random_state . normal ( size = ( n_samples , n_components + n_oversamples ) ) if A . dtype . kind == "f" : Q = Q . astype ( A . dtype , copy = False ) Q = safe_sparse_dot ( A . T , Q ) - safe_sparse_dot ( c . T , Q . sum ( axis = 0 ) [ None , : ] ) # Normalized power iterations for _ in range ( n_iter ) : Q = safe_sparse_dot ( A , Q ) - safe_sparse_dot ( c , Q ) Q , _ = lu ( Q , permute_l = True ) Q = safe_sparse_dot ( A . T , Q ) - safe_sparse_dot ( c . T , Q . sum ( axis = 0 ) [ None , : ] ) Q , _ = lu ( Q , permute_l = True ) Q , _ = qr ( Q , mode = "economic" ) QA = safe_sparse_dot ( A , Q ) - safe_sparse_dot ( c , Q ) U , s , R = svd ( QA , full_matrices = False ) V = R . dot ( Q . T ) if flip_sign : U , V = svd_flip ( U , V ) return U [ : , : n_components ] , s [ : n_components ] , V [ : n_components , : ]