如何在Python 3中打印numpy 2d数组?

时间:2017-06-24 05:22:44

标签: python-3.x numpy

我是Python 3的新手。

我的代码如下,我不知道如何解决这个问题。

test = np.random.randn(10)
print (test)

TypeError: '>' not supported between instances of 'int' and 'str'

(+) 我已经导入了numpy,并且正在跟踪详细的错误信息。

    TypeError                            Traceback (most recent call last)
<ipython-input-13-3e506a0fbc4a> in <module>()
      1 test=np.random.randn(10)
----> 2 print(test)

/usr/local/lib/python3.6/site-packages/numpy/core/numeric.py in array_str(a, max_line_width, precision, suppress_small)
   1937 
   1938     """
-> 1939     return array2string(a, max_line_width, precision, suppress_small, ' ', "", str)
   1940 
   1941 

/usr/local/lib/python3.6/site-packages/numpy/core/arrayprint.py in wrapper(self, *args, **kwargs)
    386             repr_running.add(key)
    387             try:
--> 388                 return f(self, *args, **kwargs)
    389             finally:
    390                 repr_running.discard(key)

/usr/local/lib/python3.6/site-packages/numpy/core/arrayprint.py in array2string(a, max_line_width, precision, suppress_small, separator, prefix, style, formatter)
    521     else:
    522         lst = _array2string(a, max_line_width, precision, suppress_small,
--> 523                             separator, prefix, formatter=formatter)
    524     return lst
    525 

/usr/local/lib/python3.6/site-packages/numpy/core/arrayprint.py in _array2string(a, max_line_width, precision, suppress_small, separator, prefix, formatter)
    344                   prefix="", formatter=None):
    345 
--> 346     if a.size > _summaryThreshold:
    347         summary_insert = "..., "
    348         data = _leading_trailing(a)

TypeError: '>' not supported between instances of 'int' and 'str'

3 个答案:

答案 0 :(得分:0)

在Python3中,在比较元素时决定更严格。因此,比较&#39; int&#39;和&#39; str&#39;将引发错误(此错误很难重现)。 有很多方法可以解决这个问题,我使用的一个选项是numpy中的.tolist()方法。

import numpy as np
test = np.random.randn(10)
print(test.tolist())

答案 1 :(得分:-2)

看起来您还没有包含import功能。

试试这个:

import numpy as np
test = np.random.randn(10)
print (test)

希望它有效:)

答案 2 :(得分:-2)

更改打印选项会使这个数组消失。看起来像你已经改变了 numpy中的默认阈值设置。

np.set_printoptions(threshold=5)
相关问题