Skip to content

Attempt at implementing eigh algorithm for truncatedSVD #2579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions sklearn/decomposition/truncated_svd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
except ImportError:
from ..utils.arpack import svds

from scipy.linalg import eigh
from ..base import BaseEstimator, TransformerMixin
from ..utils import (array2d, as_float_array, atleast2d_or_csr,
check_random_state, deprecated)
Expand Down Expand Up @@ -117,11 +118,16 @@ def fit_transform(self, X, y=None):
X_new : array, shape (n_samples, n_components)
Reduced version of X. This will always be a dense array.
"""
U, Sigma, VT = self._fit(X)
Sigma = np.diag(Sigma)
if self.algorithm == 'eigh':
VT = self._fit(X)
return np.dot(X, VT.T)

# or (X * VT.T).T, whichever takes fewer operations...
return np.dot(U, Sigma.T)
else:
U, Sigma, VT = self._fit(X)
Sigma = np.diag(Sigma)

# or (X * VT.T).T, whichever takes fewer operations...
return np.dot(U, Sigma.T)

def _fit(self, X):
X = as_float_array(X, copy=False)
Expand All @@ -143,6 +149,13 @@ def _fit(self, X):
U, Sigma, VT = randomized_svd(X, self.n_components,
n_iter=self.n_iter,
random_state=random_state)

elif self.algorithm == "eigh":
k = self.n_components
_, VT_ = eigh(np.dot(X.T, X))
self.components_ = VT_[:k]
return self.components_

else:
raise ValueError("unknown algorithm %r" % self.algorithm)

Expand Down