Skip to main content

linalg.safe_svd

linalg.safe_svd(X, remove_mean=True)

Singular value decomposition without occasional LinAlgError crashes.

The default lapack_driver of scipy.linalg.svd is 'gesdd', which occasionally crashes even if the input matrix is not singular. This function automatically handles the LinAlgError when it’s raised and switches to the 'gesvd' driver in this case.

The input matrix X is factorized as U @ np.diag(s) @ Vt.

Parameters

Name Type Description Default
X ndarray of shape (M, N) The matrix to be decomposed in NumPy array format. required
remove_mean bool Whether to subtract the mean of each column before the actual SVD (True) or not (False). Setting remove_mean=True is helpful when the SVD is used to perform PCA. True

Returns

Name Type Description
U ndarray of shape (M, K) Unitary matrix.
s ndarray of shape (K,) The singular values.
Vt ndarray of shape (K, N) Unitary matrix.