0

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) ?

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

2 Answers2

0

Best way I can think of is

i = range(7)

j = np.array(i)[:, None]

A slightly shorter way is:

j = np.atleast_2d(i).T
Daniel F
  • 13,620
  • 2
  • 29
  • 55
0

Just transpose it

x = np.array([range(7)]).T
blue_note
  • 27,712
  • 9
  • 72
  • 90