Skip to content

Commit

Permalink
Explicitly pass a matrix to sklearn decompositions
Browse files Browse the repository at this point in the history
  • Loading branch information
tgsmith61591 committed Nov 12, 2016
1 parent 2d6cdf6 commit e4dc29a
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions skutil/decomposition/decompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from skutil.base import *
from skutil.base import overrides
from ..utils import *
from ..utils.fixes import _cols_if_none
from ..utils.fixes import _cols_if_none, _as_numpy

__all__ = [
'SelectivePCA',
Expand Down Expand Up @@ -187,7 +187,7 @@ def fit(self, X, y=None):
# fails thru if names don't exist:
self.pca_ = PCA(
n_components=self.n_components,
whiten=self.whiten).fit(X[cols])
whiten=self.whiten).fit(X[cols].as_matrix())

return self

Expand Down Expand Up @@ -216,7 +216,7 @@ def transform(self, X):
cols = _cols_if_none(X, self.cols)

other_nms = [nm for nm in X.columns if nm not in cols]
transform = self.pca_.transform(X[cols])
transform = self.pca_.transform(X[cols].as_matrix())

# do weighting if necessary
if self.weight:
Expand Down Expand Up @@ -281,7 +281,7 @@ def score(self, X, y=None):
X, _ = validate_is_pd(X, self.cols)
cols = X.columns if not self.cols else self.cols

ll = self.pca_.score(X[cols], y)
ll = self.pca_.score(X[cols].as_matrix(), _as_numpy(y))
return ll


Expand Down Expand Up @@ -377,7 +377,7 @@ def fit(self, X, y=None):
self.svd_ = TruncatedSVD(
n_components=self.n_components,
algorithm=self.algorithm,
n_iter=self.n_iter).fit(X[cols])
n_iter=self.n_iter).fit(X[cols].as_matrix())

return self

Expand Down Expand Up @@ -406,9 +406,12 @@ def transform(self, X):
cols = _cols_if_none(X, self.cols)

other_nms = [nm for nm in X.columns if nm not in cols]
transform = self.svd_.transform(X[cols])
transform = self.svd_.transform(X[cols].as_matrix())
left = pd.DataFrame.from_records(data=transform,
columns=[('Concept%i' % (i + 1)) for i in range(transform.shape[1])])
columns=[
('Concept%i' % (i + 1))
for i in range(transform.shape[1])
])

# concat if needed
x = pd.concat([left, X[other_nms]], axis=1) if other_nms else left
Expand Down

0 comments on commit e4dc29a

Please sign in to comment.