-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
PLS returns components in an inconsistent (and often incorrect) order #11645
Copy link
Copy link
Closed
Description
The current implementation of PLS-SVD sometimes returns components in (correct) decreasing order of singular values and sometimes in (incorrect) increasing order of singular values. This can be fixed easily.
Caused by svd vs. svds
The issue is in lines 825-830
if self.n_components >= np.min(C.shape):
U, s, V = svd(C, full_matrices=False)
else:
U, s, V = svds(C, k=self.n_components)
# Deterministic output
U, V = svd_flip(U, V)where svd is from scipy.linalg and svds is from scipy.sparse.linalg. Note that svd returns singular values in decreasing order while svds returns singular values in increasing order. Hence the inconsistence, sometimes incorrect behavior.
Easy fix
The solution is to simply reverse the ordering of the singular vectors (e.g. see line 518 of the PCA class). For example, the code below should fix the issue
if self.n_components >= np.min(C.shape):
U, s, V = svd(C, full_matrices=False)
U, V = svd_flip(U, V)
else:
U, s, V = svds(C, k=self.n_components)
# svds returns singular vectors in the wrong order
U, V = svd_flip(U[:, ::-1], V[::-1]) Reactions are currently unavailable