How do I change a row vector created with np.linspace into a column vector?
Question:
So far, I have the following:
beta = np.linspace(1, 10, num = 10) / 55
This returns the row vector:
[0.01818182 0.03636364 0.05454545 0.07272727 0.09090909 0.10909091 0.12727273 0.14545455 0.16363636 0.18181818]
I’ve tried using beta.transpose() but to no avail. Any suggestions?
Answers:
This may help you reformat the output of linspace to what you want :- Size of a linspace output
The default output of linspace only has a single dimension (10,), I can’t say this as an idiom but I believe row vectors (length, 1) and column vectors (1, length) are specific cases of a generalized 2D matrix in numpy, while (length,) is just a linear structure
a = np.array([1,2,3])
print(a.shape)
(3,)
print(a.transpose().shape)
(3,)
b = np.array([[1,2,3]])
print(b.shape)
(1,3)
print(b.transpose().shape)
(3,1)
linspace
produced a 1d array. It does not have a transpose, or rather its transpose is the same shape – 1d. numpy
, in contrast to MATLAB, does not have row or column vectors. It has arrays, which may have a wide variety of shape
.
A 1d array with shape (4,):
In [218]: x
Out[218]: array([1, 2, 3, 4])
One reshape – which may be called a ‘row vector’
In [219]: x.reshape(1,4)
Out[219]: array([[1, 2, 3, 4]])
A different reshape – a ‘column vector’, the transpose of the (1,4)
In [220]: x.reshape(4,1)
Out[220]:
array([[1],
[2],
[3],
[4]])
Another common way of making such an array:
In [221]: x[:,None]
Out[221]:
array([[1],
[2],
[3],
[4]])
Two ways:
# 1
arr_col = arr.reshape(-1, 1).copy()
# 2
arr_col = arr[:, None].copy()
So far, I have the following:
beta = np.linspace(1, 10, num = 10) / 55
This returns the row vector:
[0.01818182 0.03636364 0.05454545 0.07272727 0.09090909 0.10909091 0.12727273 0.14545455 0.16363636 0.18181818]
I’ve tried using beta.transpose() but to no avail. Any suggestions?
This may help you reformat the output of linspace to what you want :- Size of a linspace output
The default output of linspace only has a single dimension (10,), I can’t say this as an idiom but I believe row vectors (length, 1) and column vectors (1, length) are specific cases of a generalized 2D matrix in numpy, while (length,) is just a linear structure
a = np.array([1,2,3])
print(a.shape)
(3,)
print(a.transpose().shape)
(3,)
b = np.array([[1,2,3]])
print(b.shape)
(1,3)
print(b.transpose().shape)
(3,1)
linspace
produced a 1d array. It does not have a transpose, or rather its transpose is the same shape – 1d. numpy
, in contrast to MATLAB, does not have row or column vectors. It has arrays, which may have a wide variety of shape
.
A 1d array with shape (4,):
In [218]: x
Out[218]: array([1, 2, 3, 4])
One reshape – which may be called a ‘row vector’
In [219]: x.reshape(1,4)
Out[219]: array([[1, 2, 3, 4]])
A different reshape – a ‘column vector’, the transpose of the (1,4)
In [220]: x.reshape(4,1)
Out[220]:
array([[1],
[2],
[3],
[4]])
Another common way of making such an array:
In [221]: x[:,None]
Out[221]:
array([[1],
[2],
[3],
[4]])
Two ways:
# 1
arr_col = arr.reshape(-1, 1).copy()
# 2
arr_col = arr[:, None].copy()