Switch dimensions of 1D ndarray
Question:
I have a ‘row’ vector cast as a numpy ndarray. I would simply like to make it a ‘column’ vector (I don’t care too much about the type as long as it is compatible with matplotlib). Here is an example of what I’m trying:
import numpy as np
a = np.ndarray(shape=(1,4), dtype=float, order='F')
print(a.shape)
a.T #I think this performs the transpose?
print(a.shape)
The output looks like this:
(1, 4)
(1, 4)
I was hoping to get:
(1, 4)
(4, 1)
Can someone point me in the right direction? I have seen that the transpose in numpy doesn’t do anything to a 1D array. But is this a 1D array?
Answers:
Transposing an array does not happen in place. Writing a.T
creates a view of the transpose of the array a
, but this view is then lost immediately since no variable is assigned to it. a
remains unchanged.
You need to write a = a.T
to bind the name a
to the transpose:
>>> a = a.T
>>> a.shape
(4, 1)
In your example a
is indeed a 2D array. Transposing a 1D array (with shape (n,)
) does not change that array at all.
You probably don’t want or need the singular dimension, unless you are trying to force a broadcasting operation.
You can treat rank-1 arrays as either row or column vectors. dot(A,v)
treats v
as a column vector, while dot(v,A)
treats v
as a row vector.
This can save you having to type a lot of transposes.
you can alter the shape ‘in place’ which will be the same as a.T for (1,4) but see the comment by Mr E whether it’s needed. i.e.
...
print(a.shape)
a.shape = (4, 1)
print(a.shape)
I have a ‘row’ vector cast as a numpy ndarray. I would simply like to make it a ‘column’ vector (I don’t care too much about the type as long as it is compatible with matplotlib). Here is an example of what I’m trying:
import numpy as np
a = np.ndarray(shape=(1,4), dtype=float, order='F')
print(a.shape)
a.T #I think this performs the transpose?
print(a.shape)
The output looks like this:
(1, 4)
(1, 4)
I was hoping to get:
(1, 4)
(4, 1)
Can someone point me in the right direction? I have seen that the transpose in numpy doesn’t do anything to a 1D array. But is this a 1D array?
Transposing an array does not happen in place. Writing a.T
creates a view of the transpose of the array a
, but this view is then lost immediately since no variable is assigned to it. a
remains unchanged.
You need to write a = a.T
to bind the name a
to the transpose:
>>> a = a.T
>>> a.shape
(4, 1)
In your example a
is indeed a 2D array. Transposing a 1D array (with shape (n,)
) does not change that array at all.
You probably don’t want or need the singular dimension, unless you are trying to force a broadcasting operation.
You can treat rank-1 arrays as either row or column vectors.
dot(A,v)
treatsv
as a column vector, whiledot(v,A)
treatsv
as a row vector.
This can save you having to type a lot of transposes.
you can alter the shape ‘in place’ which will be the same as a.T for (1,4) but see the comment by Mr E whether it’s needed. i.e.
...
print(a.shape)
a.shape = (4, 1)
print(a.shape)