5

如何像 Matlab 一样在 numpy 中打印子数组?我有一个 3 x 10000 数组,我想查看前 20 列。在 Matlab 中你可以写

a=zeros(3,10000);
a(:,1:20)
  Columns 1 through 15

 0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
 0     0     0     0     0     0     0     0     0     0     0     0     0     0     0

  Columns 16 through 20

 0     0     0     0     0
 0     0     0     0     0
 0     0     0     0     0

然而在 Numpy

import numpy as np
set_printoptions(threshold=nan)
a=np.zeros((3,10000))
print a[:,0:20]
[[  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.   0.
    0.   0.   0.   0.   0.   0.   0.   0.]]

如您所见,numpy 打印第一行,然后是第二行,然后是第三行。我希望它保持列结构而不是行结构

非常感谢

PS:例如,一种解决方案是

print a[:,0:20].T
[[  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]
 [  0.   0.   0.]]

但是,会比预期消耗更多的屏幕空间。如果 numpy 有这个选项会很棒

4

1 回答 1

1

Does this give what you want?

>>> for item in a[:,0:20].T:
    print '\t'.join(map(str,item.tolist()))

Or this?

>>> for item in a[:,0:20]:
    print '\t'.join(map(str,item.tolist()))
于 2013-09-09T15:07:20.047 回答