You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
KernelPCA: fit_transform and transform methods are inconsistent in case of zero eigenvalues #12141
In current implementation of KernelPCA, when there are zero eigenvalues that are not removed (remove_zero_eig=False), then fit_transform and fit + transform methods lead to inconsistent results.
when fit_transform is run, there is an optimization code that does not recompute the gram matrix. Therefore the transformed X is X_transformed = self.alphas_ * np.sqrt(self.lambdas_)here
when transform is run, of course it cannot use the same shortcut and therefore the gram matrix is recomputed and a dot product is performed here. Because the eigenvectors self.alphas_ are not saved in a scaled version (I do not know why), they are scaled just here, by dividing by the square root of the eigenvalues np.sqrt(self.lambdas_). When eigenvalues are zero, this leads to infinite values in the eigenvectors which after the dot product may result in infinite values or nan values.
To fix this issue, I guess that we should not scale the eigenvectors when the eigenvalue is zero.
There are two ways to do this:
either do this in the transform method for example by
deftransform(self, X):
"""Transform X. Parameters ---------- X : array-like, shape (n_samples, n_features) Returns ------- X_new : array-like, shape (n_samples, n_components) """check_is_fitted(self, 'X_fit_')
# Compute centered gram matrix between X and training data X_fit_K=self._centerer.transform(self._get_kernel(X, self.X_fit_))
# scale eigenvectorsscaled_alphas=self.alphas_/np.sqrt(self.lambdas_)
# properly take null-space into account for the dot productscaled_alphas[:, self.lambdas_==0] =0# Project by doing a scalar product between K and the scaled eigenvectsreturnnp.dot(K, scaled_alphas)
or we could scale the self.alphas_ directly when they are created (at the end of _fit). In which case we would need to adapt fit_transform and transform.
Actually doing the second option may be dangerous only in one case: if self.alphas_ is directly referenced by some application (or stored in a persistent kPCA model from another version and read back in the newest version). Do we prefer maintaining legacy compatibility for internal attributes, or improving readability and (slightly) performance ?
In the PR I will therefore go for the "legacy compliant" option (the first one).
AssertionError: Arrays are not almost equal to 6 decimals
x and y nan location mismatch:
x: array([[0.707107, nan],
[ 0.707107, nan]])
y: array([[0.707107, 0. ],
[ 0.707107, 0. ]])
Activity
smarie commentedon Sep 24, 2018
(previous comment edited for clarity)
Actually doing the second option may be dangerous only in one case: if
self.alphas_is directly referenced by some application (or stored in a persistent kPCA model from another version and read back in the newest version). Do we prefer maintaining legacy compatibility for internal attributes, or improving readability and (slightly) performance ?In the PR I will therefore go for the "legacy compliant" option (the first one).
Fixed scikit-learn#12141.
NicolasHug commentedon Oct 3, 2018
Could you please add a minimal code example illustrating the issue?
smarie commentedon Oct 8, 2018
Sure :
(EDIT: this example is now ok)
Yields:
AssertionError: Arrays are not almost equal to 6 decimals x and y nan location mismatch: x: array([[0.707107, nan], [ 0.707107, nan]]) y: array([[0.707107, 0. ], [ 0.707107, 0. ]])I will add the corresponding unit test.
Added test corresponding to issue scikit-learn#12141