Skip to content

[WIP] Add param default_value to Imputer to keep and control unknown data - #10678

Closed
nok wants to merge 6 commits into
scikit-learn:masterfrom
nok:bugfix/imputer-handle-unknown
Closed

nok wants to merge 6 commits into
scikit-learn:masterfrom
nok:bugfix/imputer-handle-unknown

Conversation

@nok

@nok nok commented Feb 22, 2018

Copy link
Copy Markdown
Contributor

Reference Issues/PRs

Fixes #10671.

What does this implement/fix? Explain your changes.

It added a new param handle_unknown to the Imputer to enhance the possibilities to handle unknown data, because today unknown data in matrices will be dropped.

Any other comments?

It's still in progress and just a suggestion to discuss.

@nok nok changed the title [WIP] Add param handle_unknown to change unknown data [WIP] Add param handle_unknown to Imputer to keep and control unknown data Feb 22, 2018

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that is an appropriate name.. perhaps default_value

@@ -119,10 +119,11 @@ class Imputer(BaseEstimator, TransformerMixin):
contain missing values).
"""
def __init__(self, missing_values="NaN", strategy="mean",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please document the new parameter above

Comment thread sklearn/preprocessing/imputation.py Outdated
is_col_nan = np.all(_isnan(X), axis=0)
if np.any(is_col_nan):
nan_indexes = np.where(is_col_nan)[0]
X[:, nan_indexes] = self.default_value

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're modifying the input! How about we just modify the calculated statistics below?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good hint, because the data structure of the calculated statistics is simpler.

Comment thread sklearn/preprocessing/imputation.py Outdated
@staticmethod
def _isnan(x):
"""Check whether x is nan or not."""
return np.isnan(x) if isinstance(x, type(np.nan)) else False

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't np.isnan just work?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because by using np.isnan I got tests which failed. I guess there were some side effects. By using the statistics np.isnan works well.

[5],
[5]
]
inputer = Imputer()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*inputer -> imputer

inputer = Imputer()
assert_array_almost_equal(inputer.fit_transform(X), X_default)

X_replaced = [

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 and True are the same when the output is an int or float

]
inputer = Imputer(default_value=False)
assert_array_almost_equal(inputer.fit_transform(X_complex), X_replaced)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to check this works when a column is all -1 and missing_values=-1

@nok nok changed the title [WIP] Add param handle_unknown to Imputer to keep and control unknown data [WIP] Add param default_value to Imputer to keep and control unknown data Feb 23, 2018
@nok

nok commented Feb 23, 2018

Copy link
Copy Markdown
Contributor Author

Thanks for your feedback @jnothman .

How can I fix the failed doc test in 345213719#L2868-L2888? I assumed that the added docs 2e7c4c3 solved that error.

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The failure is due to the automatic __repr__ including the new param.

The file you need to update is https://github.com/scikit-learn/scikit-learn/blob/master/doc/modules/preprocessing.rst.

However, you should really be modifying the new SimpleImputer in sklearn/impute.py. Your current changes affect a deprecated class.

@nok

nok commented Feb 27, 2018

Copy link
Copy Markdown
Contributor Author

Should we take this PR back? Andreas commented on the involved issue: #10671 (comment)

How do you like the ColumnTransformer solution? It also allows different strategies for imputation and scaling on the categorical and numerical features, which I think is something you usually want.

What do you think @jnothman ?

@jnothman

jnothman commented Feb 27, 2018 via email

Copy link
Copy Markdown
Member

@nok

nok commented Feb 27, 2018

Copy link
Copy Markdown
Contributor Author

Yes, I see and understand it. Thanks for your help.

@nok nok closed this Feb 27, 2018
@nok
nok deleted the bugfix/imputer-handle-unknown branch February 27, 2018 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Imputer can change the number of features

2 participants