how to cite 1Xn array as nX1 array?

Dear all,
suppose I have a ndarray a:
In [66]: a Out[66]: array([0, 1, 2, 3, 4])
how can use it as 5X1 array without doing a=a.reshape(5,1)?
thanks
Chao

On 27. jan. 2012, at 14:52, Chao YUE wrote:
Dear all,
suppose I have a ndarray a:
In [66]: a Out[66]: array([0, 1, 2, 3, 4])
how can use it as 5X1 array without doing a=a.reshape(5,1)?
Several ways, this is one, although not much simpler. In [6]: a Out[6]: array([0, 1, 2, 3, 4])
In [7]: a.shape = 5, 1
In [8]: a Out[8]: array([[0], [1], [2], [3], [4]])
Paul

On Fri, Jan 27, 2012 at 9:28 AM, Paul Anton Letnes < paul.anton.letnes@gmail.com> wrote:
On 27. jan. 2012, at 14:52, Chao YUE wrote:
Dear all,
suppose I have a ndarray a:
In [66]: a Out[66]: array([0, 1, 2, 3, 4])
how can use it as 5X1 array without doing a=a.reshape(5,1)?
Several ways, this is one, although not much simpler. In [6]: a Out[6]: array([0, 1, 2, 3, 4])
In [7]: a.shape = 5, 1
In [8]: a Out[8]: array([[0], [1], [2], [3], [4]])
Paul
I'm assuming your issue with that call to reshape is that you need to know the dimensions beforehand. An alternative is to call:
a.reshape(-1, 1)
The "-1" allows numpy to "infer" the length based on the given sizes.
Another alternative is:
a[:, np.newaxis]
-Tony

Thanks all.
chao
2012/1/27 Tony Yu tsyu80@gmail.com
On Fri, Jan 27, 2012 at 9:28 AM, Paul Anton Letnes < paul.anton.letnes@gmail.com> wrote:
On 27. jan. 2012, at 14:52, Chao YUE wrote:
Dear all,
suppose I have a ndarray a:
In [66]: a Out[66]: array([0, 1, 2, 3, 4])
how can use it as 5X1 array without doing a=a.reshape(5,1)?
Several ways, this is one, although not much simpler. In [6]: a Out[6]: array([0, 1, 2, 3, 4])
In [7]: a.shape = 5, 1
In [8]: a Out[8]: array([[0], [1], [2], [3], [4]])
Paul
I'm assuming your issue with that call to reshape is that you need to know the dimensions beforehand. An alternative is to call:
a.reshape(-1, 1)
The "-1" allows numpy to "infer" the length based on the given sizes.
Another alternative is:
a[:, np.newaxis]
-Tony
NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (4)
-
Chao YUE
-
Dag Sverre Seljebotn
-
Paul Anton Letnes
-
Tony Yu