Skip to content
Open
Show file tree
Hide file tree
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
26 changes: 15 additions & 11 deletions fancyimpute/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,21 @@ def fit_transform(self, X, y=None):
"Expected %s.fill() to return NumPy array but got %s" % (
self.__class__.__name__,
type(X_filled)))

X_result = self.solve(X_filled, missing_mask)
if not isinstance(X_result, np.ndarray):
raise TypeError(
"Expected %s.solve() to return NumPy array but got %s" % (
self.__class__.__name__,
type(X_result)))

X_result = self.project_result(X=X_result)
X_result[observed_mask] = X_original[observed_mask]
return X_result
# check if there is any missing data
if ((missing_mask == True).any()):
X_result = self.solve(X_filled, missing_mask)

if not isinstance(X_result, np.ndarray):
raise TypeError(
"Expected %s.solve() to return NumPy array but got %s" % (
self.__class__.__name__,
type(X_result)))

X_result = self.project_result(X=X_result)
X_result[observed_mask] = X_original[observed_mask]
return X_result
else:
return X_filled

def fit(self, X, y=None):
"""
Expand Down
12 changes: 11 additions & 1 deletion test/test_soft_impute.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from low_rank_data import XY, XY_incomplete, missing_mask
from common import reconstruction_error

import numpy as np
from fancyimpute import SoftImpute


def test_soft_impute_with_low_rank_random_matrix():
solver = SoftImpute()
XY_completed = solver.fit_transform(XY_incomplete)
Expand All @@ -13,5 +14,14 @@ def test_soft_impute_with_low_rank_random_matrix():
name="SoftImpute")
assert missing_mae < 0.1, "Error too high!"


# test if the solver for a submodel is running for a numpy array without any missing data
def check_for_no_missing_data():
X = np.ones((5, 5))
Xf = SoftImpute().fit_transform(X)
assert (Xf.all() == X.all())


if __name__ == "__main__":
test_soft_impute_with_low_rank_random_matrix()
check_for_no_missing_data()