Numpy: '

Créé le 8 nov. 2018  ·  4Commentaires  ·  Source: numpy/numpy

Je suis assez certain que ce qui suit est un bug, mais merci de m'éclairer si ce n'est pas le cas. Une solution serait très appréciée.

Je fais un encodage standard à chaud via SCKlearn, et j'utilise évidemment Numpy dans le processus. Tout va bien lorsque je laisse l'option d'impression par défaut, mais lorsque j'utilise la fonction numpy.set_printoptions(threshold='nan') pour imprimer l'ensemble du tableau d'encodage à chaud (au lieu du résumé Numpy), j'obtiens l'erreur dans le titre du problème. Voici le code et le rapport d'erreur/Traceback correspondant :

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'

Commentaire le plus utile

Essayez plutôt threshold=sys.maxsize , threshold est documenté comme un int .

Tous les 4 commentaires

Essayez plutôt threshold=sys.maxsize , threshold est documenté comme un int .

Peut-être devrions-nous commencer à lancer une exception dans la 1.16 lorsque les gens passent la chaîne "nan", pour les préparer pour python 3 ?

Malheureusement, stackoverflow recommande de passer 'nan'.

Essayez plutôt threshold=sys.maxsize , threshold est documenté comme un int .

Merci! cela a parfaitement fonctionné.
En effet, le problème s'est posé en suivant le lien SO mentionné.

Cette page vous a été utile?
0 / 5 - 0 notes

Questions connexes

kevinzhai80 picture kevinzhai80  ·  4Commentaires

astrofrog picture astrofrog  ·  4Commentaires

qualiaa picture qualiaa  ·  3Commentaires

Kreol64 picture Kreol64  ·  3Commentaires

thouis picture thouis  ·  4Commentaires