So, if I want a numpy array [0,1,2,3,4,5,6] (or any other vector with, say 7 elements) which has the dimension (7,1) and not (7,), is there anyway to do that when I create it, instead of writing
t=np.expand_dims(np.array(range(7)),axis=1)
?
Asked
Active
Viewed 298 times
0

CutePoison
- 4,679
- 5
- 28
- 63
-
Essentially the same, but shorter : `np.arange(7)[:,None]`? Starting with `range(7)`, you need to expand dims somehow. – Divakar Sep 05 '18 at 08:23
-
But say you have a vector [20,13,0,13,12,-3,9], it does not work. I find it odd that numpy creates these "dimensionless" vectors as default. – CutePoison Sep 05 '18 at 08:31
-
1`np.array(range(7)).reshape((7,1))` or `np.array([list(range(7))]).T` is the best I can think of. – Kota Mori Sep 05 '18 at 08:32
-
it seems like you need to add som extra methods/functions to do it then... – CutePoison Sep 05 '18 at 08:48
-
`np.arange(7)` isn't dimensionless. It is 1d, We don't need 2d to represent a list of 7 numbers. – hpaulj Sep 05 '18 at 11:37
-
`np.atleast_2d`, and `np.array(..., ndmin=2)` are two builtin tools for starting with 2d arrays. – hpaulj Sep 05 '18 at 11:48