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

6
  • Essentially the same, but shorter : np.arange(7)[:,None]? Starting with range(7), you need to expand dims somehow. Commented Sep 5, 2018 at 8: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. Commented Sep 5, 2018 at 8:31
  • 1
    np.array(range(7)).reshape((7,1)) or np.array([list(range(7))]).T is the best I can think of. Commented Sep 5, 2018 at 8:32
  • it seems like you need to add som extra methods/functions to do it then... Commented Sep 5, 2018 at 8:48
  • np.arange(7) isn't dimensionless. It is 1d, We don't need 2d to represent a list of 7 numbers. Commented Sep 5, 2018 at 11:37

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

0

Just transpose it

x = np.array([range(7)]).T

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.