2017-06-24 41 views
-1

我是Python 3新手。如何在Python 3中打印numpy 2d数组?

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

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' 
+2

您显示的代码运行良好。你确定这是什么产生了这个错误?我没有看到任何连接。 – hpaulj

+2

这有帮助。你有没有改变numpy打印选项?它看起来像阈值是一个字符串,而不是一个数字。 – hpaulj

回答

-2

看起来好像你还没有包含import函数。

尝试了这一点:

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

希望工程:)

-2

这工作。

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

O/P: - [0.09311771 -1.24023957 0.78956088 -0.2740184 -1.24509638 0.04739989 -0.4824386 0.57507391 -0.73449684 -0.75584307]

-2

更改打印选项将使这个数组去away.Looks像你已更改numpy中的 默认阈值设置。

np.set_printoptions(threshold=5) 
0

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

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