Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_iterative_imputer_train_test_with_low_rank_random_matrix():
XY_incomplete_train = XY_incomplete[:250]
XY_incomplete_test = XY_incomplete[250:]
XY_test = XY[250:]
imputer = IterativeImputer(n_iter=50, random_state=0)
imputer.fit(XY_incomplete_train)
XY_completed_test = imputer.transform(XY_incomplete_test)
_, missing_mae = reconstruction_error(
XY_test,
XY_completed_test,
missing_mask,
name="IterativeImputer Train/Test")
assert missing_mae < 0.1, "Error too high with IterativeImputer train/test method!"
def test_iterative_imputer_with_low_rank_random_matrix_approximate():
imputer = IterativeImputer(n_iter=50, n_nearest_features=5, random_state=0)
XY_completed = imputer.fit_transform(XY_incomplete)
_, missing_mae = reconstruction_error(
XY,
XY_completed,
missing_mask,
name="IterativeImputer with n_nearest_features=5")
assert missing_mae < 0.1, "Error too high with IterativeImputer " \
"method using n_nearest_features=5!"
def test_iterative_imputer_as_mice_with_low_rank_random_matrix_approximate():
n_imputations = 5
XY_completed = []
for i in range(n_imputations):
imputer = IterativeImputer(n_iter=5, sample_posterior=True, random_state=i)
XY_completed.append(imputer.fit_transform(XY_incomplete))
_, missing_mae = reconstruction_error(
XY,
np.mean(XY_completed, axis=0),
missing_mask,
name="IterativeImputer as MICE")
assert missing_mae < 0.1, "Error too high with IterativeImputer as MICE!"
def test_iterative_imputer_with_low_rank_random_matrix():
imputer = IterativeImputer(n_iter=50, random_state=0)
XY_completed = imputer.fit_transform(XY_incomplete)
_, missing_mae = reconstruction_error(
XY,
XY_completed,
missing_mask,
name="IterativeImputer")
assert missing_mae < 0.1, "Error too high with IterativeImputer method!"
for c in classes
}
msg = """Imputation transformer: {} imputers x {} classes""".format(
self.multiple, len(classes)
)
logger.info(msg)
for c, d in self.imputers.items():
for imp in d["impute"]:
imp.fit(X[d["mask"], :])
else:
for ix in range(self.multiple):
self.imputers.append(
IterativeImputer(
n_iter=self.n_iter,
sample_posterior=True,
random_state=ix,
**self.kwargs
)
)
msg = """Imputation transformer: {} imputers""".format(self.multiple)
logger.info(msg)
for ix in range(self.multiple):
self.imputers[ix].fit(X)
return self
start = X
y_present = y is not None
groupby_present = self.groupby is not None
self.imputers = []
if y_present or groupby_present:
assert not (groupby_present and y_present)
if y_present:
classes = np.unique(y)
gen_mask = lambda c: y == c
if groupby_present:
classes = X[self.groupby].unique()
gen_mask = lambda c: X[self.groupby] == c
self.imputers = {
c: {
"impute": [
IterativeImputer(
n_iter=self.n_iter,
sample_posterior=True,
random_state=ix,
**self.kwargs
)
for ix in range(self.multiple)
],
"mask": gen_mask(c),
}
for c in classes
}
msg = """Imputation transformer: {} imputers x {} classes""".format(
self.multiple, len(classes)
)
logger.info(msg)