numpy数组如何在jupyter笔记本中完整显示而不换行?

问题描述 投票:0回答:1

我已经提到了“https://stackoverflow.com/questions/55466277/how-to-print-the-full-numpy-array-without-wrapping-in-jupyter-notebook”。

但是,我的数组大小是256x256。

因此,它会自动包装在 jupyter 中,如下所示。

我的数组是基于图像的,仅由0和1组成。

我只想查看带有 0 和 1 的图像形状,如下所示。

我也尝试了下面的代码。

np.set_printoptions(threshold = np.inf)
np.set_printoptions(linewidth = np.inf)

python numpy jupyter-notebook
1个回答
0
投票

这里似乎混合了两个问题:

  1. Numpy引入的换行符:

    您可以修复这些问题,正如已经评论过的和您已经尝试过的,通过

    np.set_printoptions(linewidth=np.inf)
    
  2. Jupyter引入的换行符(或者更确切地说,换行):

    您可以按照此答案通过

    解决这些问题
    display(HTML("<style>div.jp-OutputArea-output pre {white-space: pre;}</style>"))
    

我们一起

import numpy as np
from IPython.display import display, HTML

# Avoid line breaks by Jupyter (following https://stackoverflow.com/a/70433850/7395592)
display(HTML("<style>div.jp-OutputArea-output pre {white-space: pre;}</style>"))
# Avoid premature line breaks by Numpy and show all array entries
np.set_printoptions(linewidth=np.inf, threshold=np.inf)
image = np.zeros((256, 256), dtype=np.uint8)
image

对我来说,这会产生以下输出:

我发现的唯一不便之处是输出单元格没有垂直滚动条。但是,您仍然可以通过在输出单元格中单击并向右拖动来向右滚动。

© www.soinside.com 2019 - 2024. All rights reserved.