Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printing sub-array in numpy as Matlab does

How can you print sub-arrays in numpy the same way Matlab does? I have a 3 by 10000 array and I want to view the first 20 columns. In Matlab you can write

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

However in 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.]]

As you can see numpy prints the first row, then the second row, then the third row. I would like it to maintain the column structure and not the row structure

Thank you very much

PS: One solution would be for example

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.]]

but, would consume a lot more of space on screen than desired. it would be great if numpy had this option

like image 251
gota Avatar asked Nov 11 '22 22:11

gota


1 Answers

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()))
like image 190
CT Zhu Avatar answered Nov 14 '22 22:11

CT Zhu