Skip to content

[MRG] BUG: add support for non numeric values in MissingIndicator - #13046

Merged
qinhanmin2014 merged 24 commits into
scikit-learn:masterfrom
glemaitre:is/13035
Feb 3, 2019
Merged

qinhanmin2014 merged 24 commits into
scikit-learn:masterfrom
glemaitre:is/13035

Conversation

@glemaitre

@glemaitre glemaitre commented Jan 26, 2019 •

Copy link
Copy Markdown
Member

Reference Issues/PRs

closes #13035

What does this implement/fix? Explain your changes.

Add support to non-numeric values in MissingIndicator. I flag this PR as BUG since SimpleImputer is already supporting non-numeric data and that we cannot use the MissingIndicator with the SimpleImputer in 0.20. I think that it should go in 0.20.3

I added 3 tests:

  • check that we are raising an error when the numpy string type is raising an error;
  • check that we can use string using object dtype;
  • check that the missing indicator is working in conjunction with the SimpleImputer.

@glemaitre glemaitre changed the title EHN: add support for non numeric values in MissingIndicator BUG: add support for non numeric values in MissingIndicator Jan 26, 2019
@glemaitre glemaitre changed the title BUG: add support for non numeric values in MissingIndicator [MRG] BUG: add support for non numeric values in MissingIndicator Jan 26, 2019
Comment thread sklearn/utils/estimator_checks.py Outdated
msg = "argument must be a string or a number"
assert_raises_regex(TypeError, msg, estimator.fit, X, y)
else:
# If the estimator support strings passed as object dtype,

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.

Actually X[0, 0] = {'foo': 'bar'} is not a string entry, but a dict entry. So I think the test was correct. (Otherwise it would have been needed to modify it for the support of strings in the SimpleImputer).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The error is also not raised for SimpleImputer with the strategy supporting mean. The difference is that the common tests are using default parameter which is the mean strategy for the SimpleImputer.

In [9]: imputer = SimpleImputer(strategy='most_frequent') 
In [11]: X = np.array([[{'foo': 'bar'}, 'b', 'c']], dtype=object)
In [12]: imputer.fit(X)                                                              
Out[12]: 
SimpleImputer(copy=True, fill_value=None, missing_values=nan,
              strategy='most_frequent', verbose=0)

I have to check what is going wrong in check array which is the one that should raise this error.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The error is raised by np.asarray only when we make the conversion. So it means that we neeed to raise such error when dtype=object.

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 think check_array needs an update to properly deal with non-numeric input

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 think check_array needs an update to properly deal with non-numeric input

See also #12148

@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.

Should we be testing the error produced when string dtype is passed? Otherwise looks good.

Comment thread sklearn/impute.py Outdated
force_all_finite=force_all_finite)
_check_inputs_dtype(X, self.missing_values)
if X.dtype.kind not in ("i", "u", "f", "O"):
raise ValueError("Missing indicator does not support data with "

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.

Missing indicator -> MissingIndicator

ALLOW_NAN = ['Imputer', 'SimpleImputer', 'MissingIndicator',
'MaxAbsScaler', 'MinMaxScaler', 'RobustScaler', 'StandardScaler',
'PowerTransformer', 'QuantileTransformer']
SUPPORT_STRING = ['SimpleImputer', 'MissingIndicator']

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.

surely OneHotEncoder, OrdinalEncoder and meta-estimators will belong here too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Can we do that in another PR. I think that we should also factorize the input validation as well. It is quite redundant and we could have a common test then.

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.

Yes, it probably belongs in estimator tags... (perhaps post-#8022)

@glemaitre

Copy link
Copy Markdown
Member Author

Should we be testing the error produced when string dtype is passed? Otherwise looks good.

It is covered there:
https://github.com/scikit-learn/scikit-learn/pull/13046/files#diff-065d21984eda92d7583f963f704a9876R514

@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.

Fine. If you use "MissingIndicator" instead of "missing indicator" this is good to merge by me.

Comment thread sklearn/tests/test_impute.py Outdated
"'sparse' has to be a boolean or 'auto'"),
(np.array([['a', 'b'], ['c', 'a']], dtype=str),
np.array([['a', 'b'], ['c', 'a']], dtype=str),
{}, "Missing indicator does not support data with dtype")]

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.

This will break

@jorisvandenbossche jorisvandenbossche 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.

Minor comments, looks good to me

Comment thread doc/whats_new/v0.20.rst Resolved
err_msg=err_msg.format(True))
assert_ae(X_trans, X_true, err_msg=err_msg.format(True))


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.

pep8, does not need to be removed

Comment thread sklearn/tests/test_impute.py Resolved
@jorisvandenbossche

Copy link
Copy Markdown
Member

Just need to merge master for the pep8 failure

@glemaitre

Copy link
Copy Markdown
Member Author

and I mess up my merging (basically I rebase again) but this should be fine with the final squashing.

@qinhanmin2014 qinhanmin2014 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.

We need to refactor some input validations and some common tests, but I agree that we can do these things in another PR.

@qinhanmin2014 qinhanmin2014 added this to the 0.20.3 milestone Feb 3, 2019
@qinhanmin2014
qinhanmin2014 merged commit 1ae1f1d into scikit-learn:master Feb 3, 2019
thomasjpfan pushed a commit to thomasjpfan/scikit-learn that referenced this pull request Feb 6, 2019
thomasjpfan pushed a commit to thomasjpfan/scikit-learn that referenced this pull request Feb 7, 2019
jnothman pushed a commit to jnothman/scikit-learn that referenced this pull request Feb 19, 2019
@jnothman jnothman mentioned this pull request Feb 19, 2019
16 of 17 tasks
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
davidgegiya added a commit to davidgegiya/agent-orchestrator that referenced this pull request Nov 8, 2026
Pin fcec951bc9f0003d157604bb9f7003c2c397074a and its regression selectors for reproducible runs.

Source: scikit-learn/scikit-learn#13046
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.

MissingIndicator failed with non-numeric inputs