【问题标题】:How do I get wider numpy array print output?如何获得更宽的 numpy 数组打印输出?
【发布时间】:2012-11-26 06:42:14
【问题描述】:

当一行中有足够的空间时,它会在 5 列之后自动换行。 例如:

print array
==========================restart=========================================
([32235, 2323424, 2342342
3525324, 234234])
([234234, 23423, 543535,
76572, 23424])

使用 python Idle,我尝试更改初始窗口大小首选项。重新启动栏一直延伸,但不是 numpy 数组输出。

搜索后似乎也找不到答案。 我病了,非常感谢任何帮助。 谢谢!

【问题讨论】:

  • 对不起,我无法复制这个问题(诚然没有使用 IDLE,这可能是问题所在)。数组是如何定义的?
  • 它来自一个 csv 文件,转换为列表列表,然后转换为 numpy 数组。我觉得精度设置得比 printarray 通常为每个单元分配的要高。数字大概这么长 5.22474679 5.18417234

标签: python arrays printing numpy


【解决方案1】:

不需要牺牲精度的问题的实际答案如下:

 import numpy as np
 large_width = 400
 np.set_printoptions(linewidth=large_width)

【讨论】:

  • 感谢您从 2020 年开始~!我为此搜索了高低,大多数答案都集中在增加单元格的宽度或扩大格式上,而不是增加数组中的列数,您已经巧妙地解决了。
【解决方案2】:

尝试使用np.vectorize:

printer = np.vectorize(lambda x:'{0:5}'.format(x,))
print printer(b).astype(object)

或者尝试使用np.set_printoptions:

一如IDLE所示:

>>> import numpy as np
>>> x=np.random.random(10)
>>> x
array([ 0.72239823,  0.69938461,  0.85466846,  0.03294278,  0.06698482,
        0.04137562,  0.4223521 ,  0.81317235,  0.62221494,  0.6205595 ])
>>> np.set_printoptions(precision=3)
>>> print(x)
[ 0.722  0.699  0.855  0.033  0.067  0.041  0.422  0.813  0.622  0.621]

【讨论】:

  • 是的!谢谢!抱歉花了一段时间我后来崩溃了。这是一个很好的解决方法,我很惊讶它基本上是默认问题,几乎没有文档。希望你周末愉快!
猜你喜欢
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-10
相关资源
最近更新 更多