Transposing a 1D NumPy array
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.
Transpose a 1-dimensional array in Numpy without casting to matrix
A 1D array is itself once transposed, contrary to Matlab where a 1D array doesn't exist and is at least 2D.
What you want is to reshape it:
my_array.reshape(-1, 1)
Or:
my_array.reshape(1, -1)
Depending on what kind of vector you want (column or row vector).
The -1
is a broadcast-like, using all possible elements, and the 1
creates the second required dimension.
Numpy transpose of 1D array not giving expected result
NumPy's transpose()
effectively reverses the shape of an array. If the array is one-dimensional, this means it has no effect.
In NumPy, the arrays
array([1, 2, 3])
and
array([1,
2,
3])
are actually the same – they only differ in whitespace. What you probably want are the corresponding two-dimensional arrays, for which transpose()
would work fine. Also consider using NumPy's matrix
type:
In [1]: numpy.matrix([1, 2, 3])
Out[1]: matrix([[1, 2, 3]])
In [2]: numpy.matrix([1, 2, 3]).T
Out[2]:
matrix([[1],
[2],
[3]])
Note that for most applications, the plain one-dimensional array would work fine as both a row or column vector, but when coming from Matlab, you might prefer using numpy.matrix
.
Fastest way to transpose a matrix stored in an 1D array in NumPy?
Consider a test case:
In [207]: N=1000
In [208]: X = np.arange(N*N)
Your code:
In [209]: Y = X.reshape(N,N).T.flatten()
In [210]: timeit Y = X.reshape(N,N).T.flatten()
5.45 ms ± 13 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
A suggested alternative:
In [211]: Z = X.reshape(N,N).flatten('F')
In [212]: np.allclose(Y,Z)
Out[212]: True
In [213]: timeit Z = X.reshape(N,N).flatten('F')
5.46 ms ± 39.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
No real difference. reshape
and transpose
are views.
Transpose Numpy Array (Vector)
You can use numpy's broadcasting for this:
import numpy as np
a = np.array([0,1,2])
b = np.array([3,4,5,6,7])
In [3]: a[:,None]*b
Out[3]:
array([[ 0, 0, 0, 0, 0],
[ 3, 4, 5, 6, 7],
[ 6, 8, 10, 12, 14]])
This has nothing to do with a dot product, though. But in the comments you said, that you want this result.
You could also use the numpy function outer
:
In [4]: np.outer(a, b)
Out[4]:
array([[ 0, 0, 0, 0, 0],
[ 3, 4, 5, 6, 7],
[ 6, 8, 10, 12, 14]])
Convert numpy array to numpy column python
You can use transpose
for 2d array and you can see difference for your output you can use .reshape(-1,1)
like below:
>>> x.reshape(-1,1)
array([[1],
[2],
[3]])
Or You can read with more detail in this thread and try this:
>>> np.array([x]).T
>>> np.transpose([x])
Transposing a Numpy Array on a slice
reshape
and using broadcasting
a_out = a.reshape(3,-1,1) * np.ones(9)
Out[40]:
array([[[1., 1., 1., 1., 1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2., 2., 2., 2., 2.]],
[[3., 3., 3., 3., 3., 3., 3., 3., 3.],
[4., 4., 4., 4., 4., 4., 4., 4., 4.]],
[[5., 5., 5., 5., 5., 5., 5., 5., 5.],
[6., 6., 6., 6., 6., 6., 6., 6., 6.]]])
Or
a_out = np.broadcast_to(np.reshape(a, (3,-1,1)), (3,2,9))
Out[43]:
array([[[1, 1, 1, 1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 2]],
[[3, 3, 3, 3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4, 4, 4, 4]],
[[5, 5, 5, 5, 5, 5, 5, 5, 5],
[6, 6, 6, 6, 6, 6, 6, 6, 6]]])
Related Topics
How to Use Stringio in Python3
What Is the Purpose of the -M Switch
Shared-Memory Objects in Multiprocessing
How to Check If a String Contains an Element from a List in Python
Return a Default Value If a Dictionary Key Is Not Available
Perform Commands Over Ssh with Python
How to Format a Decimal to Always Show 2 Decimal Places
Execute Code When Django Starts Once Only
Adding Days to a Date in Python
Determining Application Path in a Python Exe Generated by Pyinstaller
Replace Console Output in Python
String Concatenation of Two Pandas Columns
Calculate Cosine Similarity Given 2 Sentence Strings
Why Do You Need Explicitly Have the "Self" Argument in a Python Method