可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I use Python and NumPy and have some problems with "transpose":
a=array([5,4]) print a print a.T
Invoking a.T
is not transposing the array. If a
is for example [[],[]]
then it transposes correctly, but I need the transpose of [...,...,...]
.
回答1:
It's working exactly as it's supposed to. The transpose of a 1D array is still a 1D array! (If you're used to matlab, it fundamentally doesn't have a concept of a 1D array. Matlab's "1D" arrays are 2D.)
If you want to turn your 1D vector into a 2D array and then transpose it, just slice it with np.newaxis
(or None
, they're the same, newaxis
is just more readable).
import numpy as np a = np.array([5,4])[np.newaxis] print a print a.T
Generally speaking though, you don't ever need to worry about this. Adding the extra dimension is usually not what you want, if you're just doing it out of habit. Numpy will automatically broadcast a 1D array when doing various calculations. There's usually no need to distinguish between a row vector and a column vector (neither of which are vectors. They're both 2D!) when you just want a vector.
回答2:
Use two bracket pairs instead of one. This creates a 2D array, which can be transposed, unlike the 1D array you create if you use one bracket pair.
import numpy as np a = np.array([[5, 4]]) a.T
More thorough example:
>>> a = [3,6,9] >>> b = np.array(a) >>> b.T array([3, 6, 9]) #Here it didn't transpose because 'a' is 1 dimensional >>> b = np.array([a]) >>> b.T array([[3], #Here it did transpose because a is 2 dimensional [6], [9]])
Use numpy's shape
method to see what is going on here:
>>> b = np.array([10,20,30]) >>> b.shape (3,) >>> b = np.array([[10,20,30]]) >>> b.shape (1, 3)
回答3:
A little late to the party I see, but here's my favorite trick:
a = np.array([1, 2, 3, 4]) a = a.reshape((-1, 1)) #
Once you get that the -1 inside the tuple basically says "as many rows as needed", I find this to be the most readable way of "transposing" an array.
回答4:
You can convert an existing vector into a matrix by wrapping it in an extra set of square brackets...
from numpy import * v=array([5,4]) ## create a numpy vector array([v]).T ## transpose a vector into a matrix
numpy also has a matrix
class (see array vs. matrix)...
matrix(v).T ## transpose a vector into a matrix
回答5:
numpy 1D array --> column/row matrix:
>>> a=np.array([1,2,4]) >>> a[:, None] # col array([[1], [2], [4]]) >>> a[None, :] # row, or faster `a[None]` array([[1, 2, 4]])
And as @joe-kington said, you can replace None
with np.newaxis
for readability.
回答6:
You can only transpose a 2D array. You can use numpy.matrix
to create a 2D array. This is three years late, but I am just adding to the possible set of solutions:
import numpy as np m = np.matrix([2, 3]) m.T
回答7:
To 'transpose' a 1d array to a 2d column, you can use numpy.vstack
:
>>> numpy.vstack(numpy.array([1,2,3])) array([[1], [2], [3]])
It also works for vanilla lists:
>>> numpy.vstack([1,2,3]) array([[1], [2], [3]])
回答8:
The transpose of
x = [[0 1], [2 3]]
is
xT = [[0 2], [1 3]]
well the code is:
x = array([[0, 1],[2, 3]]); np.transpose(x)
this a link for more information:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.transpose.html
回答9:
As some of the comments above mentioned, the transpose of 1D arrays are 1D arrays, so one way to transpose a 1D array would be to convert the array to a matrix like so:
np.transpose(a.reshape(len(a), 1))
回答10:
The name of the function in numpy
is column_stack.
>>>a=np.array([5,4]) >>>np.column_stack(a) array([[5, 4]])
回答11:
Another solution.... :-)
import numpy as np a = [1,2,4]
[1, 2, 4]
b = np.array([a]).T
array([[1], [2], [4]])
回答12:
If you must use the transpose a column in a row, or vice versa, you can use the reshape()
method from your array.
To transpose a column in a row you can do this:
import numpy as np my_array = np.array([1, 2, 3, 4])
my_array = array([[1], [2], [3], [4]])
my_array = my_array.reshape(1, -1)
my_array = [1, 2, 3, 4]
To transpose a row back into a column you just need to change the numbers in the reshape()
method
my_array = my_array.reshape(-1, 1)
my_array = array([[1], [2], [3], [4]])
回答13:
Transpose of [5,4] is [5,4], well actually
[5, 4]
PS: There is a simple way to transpose a 2D matrix -
zip(*a)