我很肯定以下是一个错误,但如果不是,请赐教。 一个解决方案将不胜感激。
我正在通过 SCKlearn 进行标准的单热编码,并且显然在此过程中使用了 Numpy。 当我将打印选项保留为默认值时一切都很好,但是当我使用numpy.set_printoptions(threshold='nan')
函数打印整个一个热编码数组(而不是 Numpy 摘要)时,我在问题标题中得到了错误。 这是代码和相应的错误/回溯报告:
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'
改为尝试threshold=sys.maxsize
, threshold
被记录为int
。
当人们传递字符串“nan”时,也许我们应该在 1.16 中开始抛出异常,以便为 Python 3 做好准备?
不幸的是,stackoverflow 建议传递“nan”。
链接到 SO 错误信息: https :
改为尝试
threshold=sys.maxsize
,threshold
被记录为int
。
谢谢! 这工作得很好。
实际上,问题是通过遵循提到的 SO 链接而出现的。
最有用的评论
改为尝试
threshold=sys.maxsize
,threshold
被记录为int
。