Reputation:
How can I transpose vector with numpy? I am trying
import numpy as np
center = np.array([1,2])
center_t = np.transpose(center)
But it doesn't work, how can I do it?
Upvotes: 1
Views: 4328
Reputation: 239
The transposition of a 1D array is itself, a 1D array. Unfortunately, it is working precisely how it is meant to.
Please see this.
import numpy as np
a = np.array([5,4])[np.newaxis]
print(a)
print(a.T)
np.newaxis
essentially just increases the dimension of the array, so that you're transposing a 2D array, as you would be in Matlab.
Upvotes: 0
Reputation: 1186
Reshape should do the trick.
center = np.array([1,2])
print(center.reshape(-1,1))
array([[1], [2]])
However, for n-dimensional arrays, this will transpose the array.
print(center.T)
For example:
a = np.array([['a','b','c'],['d','e','f'],['g','h','i']])
print(a)
array([['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']], dtype='<U1')
print(a.T)
array([['a', 'd', 'g'],
['b', 'e', 'h'],
['c', 'f', 'i']], dtype='<U1')
Upvotes: 1
Reputation: 11717
To transpose an array, a matrix, data needs to have two dimensions. Here your data is 1D.
You can use np.vstack
to obtain a two dimension vertical array/matrix from a 1D array/matrix. np.hstack
is its horizontal equivalent.
import numpy as np
center = np.array([1,2])
center_t = np.vstack(center)
Upvotes: 0