Repository: scikit-learn/scikit-learn
Pull request: [MRG] More informative error message in OneHotEncoder(categories=None) with negative integer values
<!--
Thanks for contributing a pull request! Please ensure you have taken a look at
the contribution guidelines: https://github.com/scikit-learn/scikit-learn/blob/master/CONTRIBUTING.md#pull-request-checklist
-->

#### Reference Issues/PRs
Fixes #12179 - Update the error message when negative inputs are provided to OneHotEncoder to show how to negative inputs can be supported by instantiating OneHotEncoder with categories='auto'
<!--
Example: Fixes #1234. See also #3456.
Please use keywords (e.g., Fixes) to create link to the issues or pull requests
you resolved, so that they will automatically be closed when your pull request
is merged. See https://github.com/blog/1506-closing-issues-via-pull-requests
-->


#### What does this implement/fix? Explain your changes.
OneHotEncoder didn't support negative integer inputs. This behavior is 
deprecated. The legacy behaviour currently raises ValueError("X needs 
to contain only non-negative integers."). This error message is now 
extended to show how to activate the new behavior by instantiating the 
OneHotEncoder with categories='auto'.

#### Any other comments?


<!--
Please be aware that we are a loose team of volunteers so patience is
necessary; assistance handling other issues is very welcome. We value
all user contributions, no matter how minor they are. If we are slow to
review, either the pull request needs some benchmarking, tinkering,
convincing, etc. or more likely the reviewers are simply busy. In either
case, we ask for your understanding during the review process.
For more information, see our FAQ on this topic:
http://scikit-learn.org/dev/faq.html#why-is-my-pull-request-not-getting-any-attention.

Thanks for contributing!
-->


Problem statement:
OneHotEncoder "only non-negative integers" message should suggest using categories='auto'
before  version 0.20, `OneHotEncoder` did not support negative integers in input. Now it supports that case if the "new behaviour" is requested by instantiating it with `categories='auto'`. The legacy behaviour currently raises `ValueError("X needs to contain only non-negative integers.")`. This error message should be extended to mention how the new behaviour can be activated.

Hints from issue discussion:
Can I take this issue?
Sure, thanks!

Commit message: Fix Issue #12179

    OneHotEncoder "only non-negative integers" message should suggest using
    categories='auto'

Patch to review:
diff --git a/sklearn/preprocessing/_encoders.py b/sklearn/preprocessing/_encoders.py
index b2dee7d926e06..8feb1f2ba9d6a 100644
--- a/sklearn/preprocessing/_encoders.py
+++ b/sklearn/preprocessing/_encoders.py
@@ -421,7 +421,10 @@ def _legacy_fit_transform(self, X):
         dtype = getattr(X, 'dtype', None)
         X = check_array(X, dtype=np.int)
         if np.any(X < 0):
-            raise ValueError("X needs to contain only non-negative integers.")
+            raise ValueError("X needs to contain only non-negative integers,"
+                             "this behavior is now deprecated. New behaviour"
+                             "supports negative input. Instantiate using"
+                             "`categories='auto'`.")
         n_samples, n_features = X.shape
         if (isinstance(self.n_values, six.string_types) and
                 self.n_values == 'auto'):
@@ -504,7 +507,10 @@ def _legacy_transform(self, X):
         """Assumes X contains only categorical features."""
         X = check_array(X, dtype=np.int)
         if np.any(X < 0):
-            raise ValueError("X needs to contain only non-negative integers.")
+            raise ValueError("X needs to contain only non-negative integers,"
+                             "this behavior is now deprecated. New behaviour"
+                             "supports negative input. Instantiate using"
+                             "`categories='auto'`.")
         n_samples, n_features = X.shape
 
         indices = self._feature_indices_


Code review comments:
diff_hunk: @@ -421,7 +421,10 @@ def _legacy_fit_transform(self, X):
         dtype = getattr(X, 'dtype', None)
         X = check_array(X, dtype=np.int)
         if np.any(X < 0):
-            raise ValueError("X needs to contain only non-negative integers.")
+            raise ValueError("X needs to contain only non-negative integers,"
path: sklearn/preprocessing/_encoders.py
text: @user1:
No need to say the behaviour is deprecated. The user should get a warning anyway. Maybe just "Instantiate using categories='auto' to handle negative integer categories."

@author:
Sure, thanks

@author:
Fixed.