-
Notifications
You must be signed in to change notification settings - Fork 1
/
solver.py
225 lines (194 loc) · 7.75 KB
/
solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import warnings
import numpy as np
from sklearn.utils import check_array
from common import generate_random_column_samples
class Solver(object):
def __init__(
self,
fill_method="zero",
min_value=None,
max_value=None,
normalizer=None):
self.fill_method = fill_method
self.min_value = min_value
self.max_value = max_value
self.normalizer = normalizer
def __repr__(self):
return str(self)
def __str__(self):
field_list = []
for (k, v) in sorted(self.__dict__.items()):
if v is None or isinstance(v, (float, int)):
field_list.append("%s=%s" % (k, v))
elif isinstance(v, str):
field_list.append("%s='%s'" % (k, v))
return "%s(%s)" % (
self.__class__.__name__,
", ".join(field_list))
def _check_input(self, X):
if len(X.shape) != 2:
raise ValueError("Expected 2d matrix, got %s array" % (X.shape,))
def _check_missing_value_mask(self, missing):
if not missing.any():
warnings.simplefilter("always")
warnings.warn("Input matrix is not missing any values")
if missing.all():
raise ValueError("Input matrix must have some non-missing values")
def _fill_columns_with_fn(self, X, missing_mask, col_fn):
for col_idx in range(X.shape[1]):
missing_col = missing_mask[:, col_idx]
n_missing = missing_col.sum()
if n_missing == 0:
continue
col_data = X[:, col_idx]
fill_values = col_fn(col_data)
if np.all(np.isnan(fill_values)):
fill_values = 0
X[missing_col, col_idx] = fill_values
def fill(
self,
X,
missing_mask,
fill_method=None,
inplace=False):
"""
Parameters
----------
X : np.array
Data array containing NaN entries
missing_mask : np.array
Boolean array indicating where NaN entries are
fill_method : str
"zero": fill missing entries with zeros
"mean": fill with column means
"median" : fill with column medians
"min": fill with min value per column
"random": fill with gaussian samples according to mean/std of column
inplace : bool
Modify matrix or fill a copy
"""
X = check_array(X, force_all_finite=False)
if not inplace:
X = X.copy()
if not fill_method:
fill_method = self.fill_method
if fill_method not in ("zero", "mean", "median", "min", "random"):
raise ValueError("Invalid fill method: '%s'" % (fill_method))
elif fill_method == "zero":
# replace NaN's with 0
X[missing_mask] = 0
elif fill_method == "mean":
self._fill_columns_with_fn(X, missing_mask, np.nanmean)
elif fill_method == "median":
self._fill_columns_with_fn(X, missing_mask, np.nanmedian)
elif fill_method == "min":
self._fill_columns_with_fn(X, missing_mask, np.nanmin)
elif fill_method == "random":
self._fill_columns_with_fn(
X,
missing_mask,
col_fn=generate_random_column_samples)
return X
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 = check_array(X, force_all_finite=False)
if X.dtype != "f" and X.dtype != "d":
X = X.astype(float)
self._check_input(X)
missing_mask = np.isnan(X)
self._check_missing_value_mask(missing_mask)
return X, missing_mask
def clip(self, X):
"""
Clip values to fall within any global or column-wise min/max constraints
"""
X = np.asarray(X)
if self.min_value is not None:
X[X < self.min_value] = self.min_value
if self.max_value is not None:
X[X > self.max_value] = self.max_value
return X
def project_result(self, X):
"""
First undo normalization and then clip to the user-specified min/max
range.
"""
X = np.asarray(X)
if self.normalizer is not None:
X = self.normalizer.inverse_transform(X)
return self.clip(X)
def solve(self, X, missing_mask):
"""
Given an initialized matrix X and a mask of where its missing values
had been, return a completion of X.
"""
raise ValueError("%s.solve not yet implemented!" % (
self.__class__.__name__,))
def fit_transform(self, X, y=None):
"""
Fit the imputer and then transform input `X`
Note: all imputations should have a `fit_transform` method,
but only some (like IterativeImputer in sklearn) also support inductive
mode using `fit` or `fit_transform` on `X_train` and then `transform`
on new `X_test`.
"""
X_original, missing_mask = self.prepare_input_data(X)
observed_mask = ~missing_mask
X = X_original.copy()
if self.normalizer is not None:
X = self.normalizer.fit_transform(X)
X_filled = self.fill(X, missing_mask, inplace=True)
if not isinstance(X_filled, np.ndarray):
raise TypeError(
"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
def fit(self, X, y=None):
"""
Fit the imputer on input `X`.
Note: all imputations should have a `fit_transform` method,
but only some (like IterativeImputer in sklearn) also support inductive
mode using `fit` or `fit_transform` on `X_train` and then `transform`
on new `X_test`.
"""
raise ValueError(
"%s.fit not implemented! This imputation algorithm likely "
"doesn't support inductive mode. Only fit_transform is "
"supported at this time." % (
self.__class__.__name__,))
def transform(self, X, y=None):
"""
Transform input `X`.
Note: all imputations should have a `fit_transform` method,
but only some (like IterativeImputer in sklearn) also support inductive
mode using `fit` or `fit_transform` on `X_train` and then `transform`
on new `X_test`.
"""
raise ValueError(
"%s.transform not implemented! This imputation algorithm likely "
"doesn't support inductive mode. Only %s.fit_transform is "
"supported at this time." % (
self.__class__.__name__, self.__class__.__name__))