Repository: numpy/numpy

Issue:
'<' not supported between instances of 'int' and 'str' Type Error attributed to a print statement
I am quite positive that the following is a bug, but please enlighten me if it isn't. A solution would be very much appreciated.

I am doing a standard one hot encoding through SCKlearn, and obviously using Numpy in the process. It is all fine when I leave the print option as default, but when I use `numpy.set_printoptions(threshold='nan')` function to print the whole of the one hot encoding array (instead of the Numpy summary) I get the error in the issue title. Here is the code and the corresponding error/Traceback report:

```
import numpy
from numpy import array
from numpy import argmax
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder

data = "+++++++++QVQLVQSGGGVVQPGRSLRLSCAASGFTFSSHRMHWVRQAPGKGLEWVAAVSNDGSNEYYADSVKGRFTISRDKSTSTLYLQMDSLRPEDTAVYYCARERCVSSSCWARALDYWGQGSLVTVCS++++++++++"
seq_string = list(data)
print(seq_string)
values = array(seq_string)
print(values)
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(values)
print(integer_encoded)
onehot_encoder = OneHotEncoder(sparse=False)
integer_encoded = integer_encoded.reshape(len(integer_encoded), 1)
onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
numpy.set_printoptions(threshold='nan')
print(onehot_encoded)
inverted = label_encoder.inverse_transform([argmax(onehot_encoded[1, :])])
print(inverted)
```
```
> ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-43683b44d2e3> in <module>()
     24 onehot_encoded = onehot_encoder.fit_transform(integer_encoded)
     25 numpy.set_printoptions(threshold='nan')
---> 26 print(onehot_encoded)
     27 # invert first example
     28 inverted = label_encoder.inverse_transform([argmax(onehot_encoded[1, :])])

/d/harpy1/s/python/v3-5.1.0/lib/python3.6/site-packages/numpy/core/arrayprint.py in array_str(a, max_line_width, precision, suppress_small)
   1400         return str(a[()])
   1401 
-> 1402     return array2string(a, max_line_width, precision, suppress_small, ' ', "")
   1403 
   1404 def set_string_function(f, repr=True):

/d/harpy1/s/python/v3-5.1.0/lib/python3.6/site-packages/numpy/core/arrayprint.py in array2string(a, max_line_width, precision, suppress_small, separator, prefix, style, formatter, threshold, edgeitems, sign, floatmode, suffix, **kwarg)
    620         return "[]"
    621 
--> 622     return _array2string(a, options, separator, prefix)
    623 
    624 

/d/harpy1/s/python/v3-5.1.0/lib/python3.6/site-packages/numpy/core/arrayprint.py in wrapper(self, *args, **kwargs)
    420             repr_running.add(key)
    421             try:
--> 422                 return f(self, *args, **kwargs)
    423             finally:
    424                 repr_running.discard(key)

/d/harpy1/s/python/v3-5.1.0/lib/python3.6/site-packages/numpy/core/arrayprint.py in _array2string(a, options, separator, prefix)
    435     data = asarray(a)
    436 
--> 437     if a.size > options['threshold']:
    438         summary_insert = "..."
    439         data = _leading_trailing(data, options['edgeitems'])

TypeError: '>' not supported between instances of 'int' and 'str'
```


Discussion:
Try `threshold=sys.maxsize` instead, [`threshold`](http://www.numpy.org/devdocs/reference/generated/numpy.set_printoptions.html) is documented as an `int`.
Perhaps we should start throwing an exception in 1.16 when people pass the string "nan", to prepare them for python 3?

Unfortunately stackoverflow recommends passing 'nan'.
Link to SO misinformation: https://stackoverflow.com/questions/1987694/how-to-print-the-full-numpy-array

Resolving patch:
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py
--- a/numpy/core/arrayprint.py
+++ b/numpy/core/arrayprint.py
@@ -26,6 +26,7 @@
 
 import sys
 import functools
+import numbers
 if sys.version_info[0] >= 3:
     try:
         from _thread import get_ident
@@ -86,7 +87,11 @@ def _make_options_dict(precision=None, threshold=None, edgeitems=None,
     if legacy not in [None, False, '1.13']:
         warnings.warn("legacy printing option can currently only be '1.13' or "
                       "`False`", stacklevel=3)
-
+    if threshold is not None:
+        # forbid the bad threshold arg suggested by stack overflow, gh-12351
+        if not isinstance(threshold, numbers.Number) or np.isnan(threshold):
+            raise ValueError("threshold must be numeric and non-NAN, try "
+                             "sys.maxsize for untruncated representation")
     return options
 
 


Test patch:
diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py
--- a/numpy/core/tests/test_arrayprint.py
+++ b/numpy/core/tests/test_arrayprint.py
@@ -842,6 +842,10 @@ def test_edgeitems(self):
                     [[ 0.]]]])""")
         )
 
+    def test_bad_args(self):
+        assert_raises(ValueError, np.set_printoptions, threshold='nan')
+        assert_raises(ValueError, np.set_printoptions, threshold=u'1')
+        assert_raises(ValueError, np.set_printoptions, threshold=b'1')
 
 def test_unicode_object_array():
     import sys