Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy print a 1d array as a column

I have an array that is 1d, and I would like to print it as a column.

r1 = np.array([54,14,-11,2])
print r1

gives me this:

 [ 54  14 -11   2]

and

 print r1.shape

gives me this:

(4L,)

Is there something I can plug into np.reshape() so that

print r1.shape

gives me this?

(,4L)

And the printed output looks something like

 54
 14
-11
 2
like image 487
Jimmy Avatar asked Feb 10 '18 19:02

Jimmy


2 Answers

No, you can't do that unless you create a vertical version of your array. But if you just want to print your items in that format you can use set_printoptions() function to set a printing format for your intended types:

In [43]: np.set_printoptions(formatter={'int':lambda x: '{}\n'.format(x)})

In [44]: print(r1)
[54
 14
 -11
 2
]

NOTE: If you want to apply this function to all types you can use 'all' keyword that applies the function on all the types.

formatter = {'all':lambda x: '{}\n'.format(x)}
like image 38
Mazdak Avatar answered Sep 27 '22 19:09

Mazdak


This will work:

import numpy as np

r1 = np.array([54,14,-11,2])

r1[:, None]

# array([[ 54],
#        [ 14],
#        [-11],
#        [  2]])
like image 84
jpp Avatar answered Sep 27 '22 19:09

jpp