打印固定大小的 Python 数组

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

是否可以显示固定大小的数组。例如,我的数组大小为 28x82,但当我打印它时,它以 13x60+4

的格式显示

My code

python arrays numpy mnist
1个回答
0
投票

Numpy 不再知道你的数组的大小,因为你已经重塑了它。你给它的形状是

60000, 784
,然后得到它的第二个元素,它的形状是
784
.

如果您希望 numpy 将数组打印为 28x28 网格,您可以将形状指定为

60000, 28, 28

此外,您可能希望指定 numpy 输出的最大线宽,以便数组的每一行实际占用一行输出而不是溢出。

import numpy as np

...  # Define your array in the first place

np.set_printoptions(linewidth=200)  # Doesn't have to be 200, just anything that fits one row of the array
X_train.reshape(60000, 28, 28)[1]  # Reshape to 28x28
© www.soinside.com 2019 - 2024. All rights reserved.