Numpy: '

Erstellt am 8. Nov. 2018  ·  4Kommentare  ·  Quelle: numpy/numpy

Ich bin mir ziemlich sicher, dass das Folgende ein Fehler ist, aber bitte klärt mich auf, wenn dies nicht der Fall ist. Eine Lösung wäre sehr dankbar.

Ich mache eine Standard-Eins-Hot-Codierung über SCKlearn und verwende dabei offensichtlich Numpy. Es ist alles in Ordnung, wenn ich die Druckoption als Standard belasse, aber wenn ich die numpy.set_printoptions(threshold='nan') Funktion verwende, um das gesamte heiße Encoding-Array (anstelle der Numpy-Zusammenfassung) zu drucken, erhalte ich den Fehler im Titel des Problems. Hier der Code und der dazugehörige Fehler-/Traceback-Bericht:

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'

Hilfreichster Kommentar

Versuchen Sie es stattdessen mit threshold=sys.maxsize , threshold ist als int dokumentiert.

Alle 4 Kommentare

Versuchen Sie es stattdessen mit threshold=sys.maxsize , threshold ist als int dokumentiert.

Vielleicht sollten wir in 1.16 eine Ausnahme auslösen, wenn Leute die Zeichenfolge "nan" übergeben, um sie auf Python 3 vorzubereiten?

Leider empfiehlt Stackoverflow die Übergabe von 'nan'.

Versuchen Sie es stattdessen mit threshold=sys.maxsize , threshold ist als int dokumentiert.

Dankeschön! das hat einwandfrei funktioniert.
Tatsächlich entstand das Problem, indem man dem erwähnten SO-Link folgte.

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen