如何打印完整的 NumPy 数组而不截断?
- 2024-12-18 08:39:00
- admin 原创
- 168
问题描述:
当我打印一个 numpy 数组时,我得到了一个截断的表示,但我想要完整的数组。
>>> numpy.arange(10000)
array([ 0, 1, 2, ..., 9997, 9998, 9999])
>>> numpy.arange(10000).reshape(250,40)
array([[ 0, 1, 2, ..., 37, 38, 39],
[ 40, 41, 42, ..., 77, 78, 79],
[ 80, 81, 82, ..., 117, 118, 119],
...,
[9880, 9881, 9882, ..., 9917, 9918, 9919],
[9920, 9921, 9922, ..., 9957, 9958, 9959],
[9960, 9961, 9962, ..., 9997, 9998, 9999]])
解决方案 1:
使用numpy.set_printoptions
:
import sys
import numpy
numpy.set_printoptions(threshold=sys.maxsize)
解决方案 2:
import numpy as np
np.set_printoptions(threshold=np.inf)
我建议使用np.inf
而不是np.nan
,这是其他人建议的。它们都可以满足您的目的,但是通过将阈值设置为“无穷大”,每个阅读代码的人都可以清楚地看到您的意思。对我来说,将阈值设置为“不是数字”似乎有点模糊。
解决方案 3:
临时设置
您可以使用printoptions
上下文管理器:
with numpy.printoptions(threshold=numpy.inf):
print(arr)
(当然,如果你是这样导入的,请替换numpy
为)np
`numpy`
使用上下文管理器(with
-block)可确保在上下文管理器完成后,打印选项将恢复到块启动之前的状态。它确保设置是临时的,并且仅适用于块内的代码。
有关上下文管理器的详细信息及其支持的其他参数,请参阅numpy.printoptions
文档。它是在 NumPy 1.15(发布于 2018-07-23)中引入的。
解决方案 4:
前面的答案是正确的,但作为一种较弱的替代方案,您可以将其转换为列表:
>>> 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]]
解决方案 5:
这是一种一次性的方法,如果您不想更改默认设置,这种方法很有用:
def fullprint(*args, **kwargs):
from pprint import pprint
import numpy
opt = numpy.get_printoptions()
numpy.set_printoptions(threshold=numpy.inf)
pprint(*args, **kwargs)
numpy.set_printoptions(**opt)
解决方案 6:
这听起来就像你正在使用 numpy。
如果是这种情况,您可以添加:
import numpy as np
import sys
np.set_printoptions(threshold=sys.maxsize)
这将禁用角落打印。有关更多信息,请参阅此NumPy 教程。
解决方案 7:
按照Paul Price 的建议使用上下文管理器
import numpy as np
class fullprint:
'context manager for printing full numpy arrays'
def __init__(self, **kwargs):
kwargs.setdefault('threshold', np.inf)
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)
if __name__ == '__main__':
a = np.arange(1001)
with fullprint():
print(a)
print(a)
with fullprint(threshold=None, edgeitems=10):
print(a)
解决方案 8:
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
...
并且可以用进一步的参数进行配置。
特别注意,这也不会显示方括号,并且允许进行大量自定义,如上所述:如何打印没有括号的 Numpy 数组?
在 Python 2.7.12、numpy 1.11.1 上测试。
解决方案 9:
稍微修改一下:(因为您要打印一个巨大的列表)
import numpy as np
np.set_printoptions(threshold=np.inf, linewidth=200)
x = np.arange(1000)
print(x)
这将增加每行的字符数(默认行宽为 75)。使用适合您的编码环境的任意行宽值。通过增加每行的字符数,您将不必处理大量的输出行。
解决方案 10:
set_printoptions)
这是一个小小的修改(删除了向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)
解决方案 11:
with np.printoptions(edgeitems=50):
print(x)
将 50 改为你想看的行数
来源:这里
解决方案 12:
与此答案的最大列数 (用 固定numpy.set_printoptions(threshold=numpy.nan)
) 相辅相成的是,显示的字符数也存在限制。在某些环境中,例如从 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 Text 并希望在输出窗口中查看结果的人来说,你应该将构建选项添加"word_wrap": false
到 Sublime-build 文件 [ source ] 中。
解决方案 13:
关闭并返回正常模式
np.set_printoptions(threshold=False)
解决方案 14:
自 NumPy 1.16 版本起,更多详细信息请参阅GitHub ticket 12251。
from sys import maxsize
from numpy import set_printoptions
set_printoptions(threshold=maxsize)
解决方案 15:
matreprmax_rows
将打印具有禁用和限制的整个数组max_cols
:
from matrepr import mprint
a = numpy.arange(10000).reshape(250,40)
mprint(a, max_rows=None, max_cols=None)
结果的前几行:
<250×40, 10000 'int64' elements, array>
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
┌ ┐
0 │ 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 │
1 │ 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 │
2 │ 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 │
3 │ 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 │
4 │ 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 │
5 │ 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 │
6 │ 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 │
7 │ 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 │
8 │ 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 │
9 │ 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 │
10 │ 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 │
11 │ 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 │
解决方案 16:
假设你有一个 numpy 数组
arr = numpy.arange(10000).reshape(250,40)
如果你想一次性打印完整数组(不切换 np.set_printoptions),但想要比上下文管理器更简单(更少代码)的东西,只需这样做
for row in arr:
print row
解决方案 17:
如果您使用的是 jupyter 笔记本,我发现这是解决一次性案例的最简单的解决方案。基本上将 numpy 数组转换为列表,然后转换为字符串,然后打印。这样做的好处是保留数组中的逗号分隔符,而使用则numpyp.printoptions(threshold=np.inf)
不会:
import numpy as np
print(str(np.arange(10000).reshape(250,40).tolist()))
解决方案 18:
这是最棘手的解决方案,它甚至可以像 numpy 一样很好地打印出来:
import numpy as np
a = np.arange(10000).reshape(250,40)
b = [str(row) for row in a.tolist()]
print('
'.join(b))
出去:
解决方案 19:
您可以使用array2string
函数-docs。
a = numpy.arange(10000).reshape(250,40)
print(numpy.array2string(a, threshold=numpy.nan, max_line_width=numpy.nan))
# [Big output]
解决方案 20:
您并不总是希望打印所有项目,尤其是对于大型阵列。
显示更多项目的简单方法:
In [349]: ar
Out[349]: array([1, 1, 1, ..., 0, 0, 0])
In [350]: ar[:100]
Out[350]:
array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1,
1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])
默认情况下,当切片数组 < 1000 时,它可以正常工作。
解决方案 21:
如果您使用的是 Jupyter,请尝试变量检查器扩展。您可以单击每个变量来查看整个数组。
解决方案 22:
如果数组太大而无法打印,NumPy 会自动跳过数组的中心部分并仅打印角落:要禁用此行为并强制 NumPy 打印整个数组,您可以使用更改打印选项set_printoptions
。
>>> np.set_printoptions(threshold='nan')
或者
>>> np.set_printoptions(edgeitems=3,infstr='inf',
... linewidth=75, nanstr='nan', precision=8,
... suppress=False, threshold=1000, formatter=None)
您还可以参考numpy 文档 numpy 文档中的“或部分”以获得更多帮助。
解决方案 23:
如果你有熊猫,
numpy.arange(10000).reshape(250,40)
print(pandas.DataFrame(a).to_string(header=False, index=False))
避免了需要重置的副作用numpy.set_printoptions(threshold=sys.maxsize)
,并且您不会得到 numpy.array 和括号。我发现这对于将宽数组转储到日志文件中很方便
扫码咨询,免费领取项目管理大礼包!