Reputation: 3198
I am trying to print a vertical slice of a numpy array so it displays vertically but it always prints horizontally. Given this square array:
a = np.ones([5,5])
I've tried:
print a[:,1]
print np.reshape(a[:,1], (1,-1))
print a[:,1].T
print [a[:,1].T]
which give me:
[ 1. 1. 1. 1. 1.]
[[ 1. 1. 1. 1. 1.]]
[ 1. 1. 1. 1. 1.]
[array([ 1., 1., 1., 1., 1.])]
I want to see:
[[1],
[1],
[1],
[1],
[1]]
Upvotes: 2
Views: 4690
Reputation: 5301
You could also use np.vstack()
:
print(np.vstack(a[:,1]))
[[1.]
[1.]
[1.]
[1.]
[1.]]
Upvotes: 1
Reputation: 5389
Just an alternative, I sometimes use atleast_2d
:
np.atleast_2d(a[:, 1]).T
(there are also atleast_1d
, atleast_3d
options too)
Upvotes: -1
Reputation: 3198
Another way to add a dimension:
a[:,1:2]
Out:
array([[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.]])
Upvotes: 0
Reputation: 294278
I'd wrap the second indexer in brackets
a[:, [1]]
array([[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.]])
Upvotes: 0
Reputation:
You need to add a new axis:
a[:, 1, None]
Out:
array([[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.]])
or
a[:, 1, np.newaxis]
Out:
array([[ 1.],
[ 1.],
[ 1.],
[ 1.],
[ 1.]])
Upvotes: 4