Skip to content

Commit

Permalink
adding check_array everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyf committed Jul 20, 2018
1 parent ac23beb commit 64ffe48
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 5 deletions.
4 changes: 4 additions & 0 deletions fancyimpute/bayesian_ridge_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from numpy.linalg import norm, inv, multi_dot
from numpy.random import multivariate_normal

from sklearn.utils import check_array


class BayesianRidgeRegression(object):
"""
Expand Down Expand Up @@ -44,6 +46,8 @@ def __init__(self, lambda_reg=0.001, add_ones=False, normalize_lambda=True):
self.normalize_lambda = normalize_lambda

def fit(self, X, y, inverse_covariance=None):
X = check_array(X)

if self.add_ones:
X_ones = self.add_column_of_ones(X)
else:
Expand Down
3 changes: 3 additions & 0 deletions fancyimpute/iterative_svd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from __future__ import absolute_import, print_function, division

from sklearn.decomposition import TruncatedSVD
from sklearn.utils import check_array
import numpy as np

from .solver import Solver
Expand Down Expand Up @@ -53,6 +54,8 @@ def _converged(self, X_old, X_new, missing_mask):
return (ssd / old_norm_squared) < self.convergence_threshold

def solve(self, X, missing_mask):
X = check_array(X, force_all_finite=False)

observed_mask = ~missing_mask
X_filled = X
for i in range(self.max_iters):
Expand Down
3 changes: 3 additions & 0 deletions fancyimpute/knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import numpy as np

from knnimpute import knn_impute_few_observed, knn_impute_with_argpartition
from sklearn.utils import check_array

from .solver import Solver

Expand Down Expand Up @@ -83,6 +84,8 @@ def __init__(
self._impute_fn = knn_impute_few_observed

def solve(self, X, missing_mask):
X = check_array(X, force_all_finite=False)

if self.orientation == "columns":
X = X.T
missing_mask = missing_mask.T
Expand Down
4 changes: 3 additions & 1 deletion fancyimpute/matrix_factorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from keras.callbacks import EarlyStopping
from keras.layers import Input
from keras.models import Model
from sklearn.utils import shuffle
from sklearn.utils import shuffle, check_array

from .common import import_from
from .scaler import Scaler
Expand Down Expand Up @@ -68,6 +68,8 @@ def __init__(
self.verbose = verbose

def solve(self, X, missing_mask):
X = check_array(X, force_all_finite=False)

# shape data to fit into keras model
(n_samples, n_features) = X.shape
observed_mask = ~missing_mask
Expand Down
6 changes: 4 additions & 2 deletions fancyimpute/mice.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from six.moves import range
import numpy as np

from sklearn.utils import check_array

from .bayesian_ridge_regression import BayesianRidgeRegression
from .solver import Solver

Expand Down Expand Up @@ -290,7 +292,7 @@ def multiple_imputations(self, X):
belong in X.
"""
start_t = time()
X = np.asarray(X)
X = check_array(X, force_all_finite=False)
self._check_input(X)
missing_mask = np.isnan(X)
self._check_missing_value_mask(missing_mask)
Expand Down Expand Up @@ -330,7 +332,7 @@ def multiple_imputations(self, X):
def complete(self, X):
if self.verbose:
print("[MICE] Completing matrix with shape %s" % (X.shape,))
X_completed = np.array(X.copy())
X_completed = check_array(X, force_all_finite=False, copy=True)
imputed_arrays, missing_mask = self.multiple_imputations(X)
# average the imputed values for each feature
average_imputated_values = imputed_arrays.mean(axis=0)
Expand Down
3 changes: 3 additions & 0 deletions fancyimpute/nuclear_norm_minimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from .solver import Solver

from sklearn.utils import check_array

class NuclearNormMinimization(Solver):
"""
Expand Down Expand Up @@ -109,6 +110,8 @@ def _create_objective(self, m, n):
return S, objective

def solve(self, X, missing_mask):
X = check_array(X, force_all_finite=False)

m, n = X.shape
S, objective = self._create_objective(m, n)
constraints = self._constraints(
Expand Down
4 changes: 4 additions & 0 deletions fancyimpute/similarity_weighted_averaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from __future__ import absolute_import, print_function, division
from collections import defaultdict

from sklearn.utils import check_array

import numpy as np

from .dictionary_helpers import (
Expand Down Expand Up @@ -151,6 +153,8 @@ def complete_dict(
return result

def complete(self, X):
X = check_array(X, force_all_finite=False)

if self.verbose:
print(
("[SimilarityWeightedAveraging] Creating dictionary from matrix "
Expand Down
3 changes: 3 additions & 0 deletions fancyimpute/soft_impute.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from six.moves import range
import numpy as np
from sklearn.utils.extmath import randomized_svd
from sklearn.utils import check_array

from .common import masked_mae
from .solver import Solver
Expand Down Expand Up @@ -134,6 +135,8 @@ def _max_singular_value(self, X_filled):
return s[0]

def solve(self, X, missing_mask):
X = check_array(X, force_all_finite=False)

X_init = X.copy()

X_filled = X
Expand Down
8 changes: 6 additions & 2 deletions fancyimpute/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import numpy as np
from six.moves import range

from sklearn.utils import check_array

from .common import generate_random_column_samples


Expand Down Expand Up @@ -91,6 +93,8 @@ def fill(
inplace : bool
Modify matrix or fill a copy
"""
X = check_array(X, force_all_finite=False)

if not inplace:
X = X.copy()

Expand Down Expand Up @@ -120,7 +124,7 @@ def prepare_input_data(self, X):
Check to make sure that the input matrix and its mask of missing
values are valid. Returns X and missing mask.
"""
X = np.asarray(X)
X = check_array(X, force_all_finite=False)
if X.dtype != "f" and X.dtype != "d":
X = X.astype(float)

Expand All @@ -142,7 +146,7 @@ def clip(self, X):

def project_result(self, X):
"""
First undo normaliztion and then clip to the user-specified min/max
First undo normalization and then clip to the user-specified min/max
range.
"""
X = np.asarray(X)
Expand Down

0 comments on commit 64ffe48

Please sign in to comment.