如何用python打印三角阵列_如何打印完整的NumPy阵列?

回答(13)

2 years ago

import numpy as np

np.set_printoptions(threshold=np.inf)

我建议使用 np.inf 而不是其他人建议的 np.nan . 它们都可以用于您的目的,但是通过将阈值设置为"infinity",每个人都可以阅读您的代码,这是显而易见的 . 有一个"not a number"的门槛对我来说似乎有点模糊 .

2 years ago

您可以使用 array2string 函数 - docs .

a = numpy.arange(10000).reshape(250,40)

print(numpy.array2string(a, threshold=numpy.nan, max_line_width=numpy.nan))

# [Big output]

2 years ago

如果数组太大而无法打印,NumPy会自动跳过数组的中心部分并仅打印角:要禁用此行为并强制NumPy打印整个数组,可以使用 set_printoptions 更改打印选项 .

>>> np.set_printoptions(threshold='nan')

or

>>> np.set_printoptions(edgeitems=3,infstr='inf',

... linewidth=75, nanstr='nan', precision=8,

... suppress=False, threshold=1000, formatter=None)

2 years ago

澄清里德的回复

import numpy

numpy.set_printoptions(threshold=numpy.nan)

请注意,上面给出的回复适用于初始 from numpy import * ,这是不可取的 .

2 years ago

假设你有一个numpy数组

arr = numpy.arange(10000).reshape(250,40)

如果你想以一次性的方式打印完整的数组(没有切换np.set_printoptions),但想要比上下文管理器更简单(更少的代码),只需要做

for row in arr:

print row

2 years ago

NumPy 1.15或更新

如果您使用NumPy 1.15(2018-07-23发布)或更新版本,则可以使用 printoptions 上下文管理器:

with numpy.printoptions(threshold=numpy.inf):

print(arr)

(当然,如果您导入 numpy 的话,请将 numpy 替换为 np )

使用上下文管理器( with -block)可确保在上下文管理器完成后,打印选项将恢复为块启动之前的任何内容 . 它确保设置是临时的,并且仅应用于块内的代码 .

2 years ago

从最大列数(用 numpy.set_printoptions(threshold=numpy.nan) 固定)补充此answer,还有一个字符限制要显示 . 在某些环境中,比如从bash(而不是交互式会话)调用python时,可以通过设置参数 linewidth 来修复此问题 .

import numpy as np

np.set_printoptions(linewidth=2000) # default = 75

Mat = np.arange(20000,20150).reshape(2,75) # 150 elements (75 columns)

print(Mat)

在这种情况下,您的窗口应限制换行的字符数 .

对于那些使用sublime文本并希望在输出窗口中查看结果的人,您应该将构建选项 "word_wrap": false 添加到sublime-build文件[source] .

2 years ago

使用上下文管理器作为Paul Price sugggested

import numpy as np

class fullprint:

'context manager for printing full numpy arrays'

def __init__(self, **kwargs):

if 'threshold' not in kwargs:

kwargs['threshold'] = np.nan

self.opt = kwargs

def __enter__(self):

self._opt = np.get_printoptions()

np.set_printoptions(**self.opt)

def __exit__(self, type, value, traceback):

np.set_printoptions(**self._opt)

a = np.arange(1001)

with fullprint():

print(a)

print(a)

with fullprint(threshold=None, edgeitems=10):

print(a)

2 years ago

这是一个小小的修改(删除了将附加参数传递给neok的neok答案的选项 .

它显示了如何使用contextlib.contextmanager轻松创建具有较少代码行的上下文管理器:

import numpy as np

from contextlib import contextmanager

@contextmanager

def show_complete_array():

oldoptions = np.get_printoptions()

np.set_printoptions(threshold=np.inf)

try:

yield

finally:

np.set_printoptions(**oldoptions)

在您的代码中,它可以像这样使用:

a = np.arange(1001)

print(a) # shows the truncated array

with show_complete_array():

print(a) # shows the complete array

print(a) # shows the truncated array (again)

2 years ago

这听起来像你正在使用numpy .

如果是这种情况,您可以添加:

import numpy as np

np.set_printoptions(threshold=np.nan)

这将禁用角落打印 . 有关更多信息,请参阅NumPy Tutorial .

2 years ago

之前的答案是正确的,但作为一个较弱的替代方案,您可以转换为列表:

>>> numpy.arange(100).reshape(25,4).tolist()

[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21,

22, 23], [24, 25, 26, 27], [28, 29, 30, 31], [32, 33, 34, 35], [36, 37, 38, 39], [40, 41,

42, 43], [44, 45, 46, 47], [48, 49, 50, 51], [52, 53, 54, 55], [56, 57, 58, 59], [60, 61,

62, 63], [64, 65, 66, 67], [68, 69, 70, 71], [72, 73, 74, 75], [76, 77, 78, 79], [80, 81,

82, 83], [84, 85, 86, 87], [88, 89, 90, 91], [92, 93, 94, 95], [96, 97, 98, 99]]

2 years ago

numpy.savetxt

numpy.savetxt(sys.stdout, numpy.arange(10000))

或者如果你需要一个字符串:

import StringIO

sio = StringIO.StringIO()

numpy.savetxt(sio, numpy.arange(10000))

s = sio.getvalue()

print s

默认输出格式为:

0.000000000000000000e+00

1.000000000000000000e+00

2.000000000000000000e+00

3.000000000000000000e+00

...

并且可以使用其他参数进行配置 .

在Python 2.7.12上测试,numpy 1.11.1 .

2 years ago

这是一种一次性的方法,如果您不想更改默认设置,这将非常有用:

def fullprint(*args, **kwargs):

from pprint import pprint

import numpy

opt = numpy.get_printoptions()

numpy.set_printoptions(threshold='nan')

pprint(*args, **kwargs)

numpy.set_printoptions(**opt)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值