3

I have a (10,) array I want to reshape in (1,10)

I did the following (u being my array)

import numpy as np

u = u.reshape(-1,1).T

but it does not work, any advice ?

Underoos
  • 4,708
  • 8
  • 42
  • 85
vbx8977
  • 57
  • 1
  • 4

5 Answers5

3

As mentioned by Chris in his comment you simply want to reshape the array by fixing the number of rows to one and let Python figure out the other dimension:

u=u.reshape(1, -1)
CAPSLOCK
  • 6,243
  • 3
  • 33
  • 56
3

I think @Chris has mentioned well in the comment, you can try out

I have tried out the scenario

>>> import numpy as np
>>> u = np.zeros((10))

>>> u.shape
(10,)
>>> u.T
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
>>> u = u.reshape(1, -1)
>>> u.shape
(1, 10)
>>> u.T
array([[0.],
   [0.],
   [0.],
   [0.],
   [0.],
   [0.],
   [0.],
   [0.],
   [0.],
   [0.]])

I think for your case u.reshape(1, -1) would do your work.

Chitrank Dixit
  • 3,961
  • 4
  • 39
  • 59
2

You can try to use expand_dims() method.

import numpy as np

a = np.zeros(10) # a.shape = (10,)
a = np.expand_dims(a, axis=0)
print(a.shape)

Output

(1, 10)
user3053452
  • 640
  • 1
  • 12
  • 38
1

What you want is :

u = u.reshape((-1, 1)).T
sowlosc
  • 470
  • 3
  • 8
0

Another trick in the book that works is with np.newaxis

 u = u[np.newaxis,:]

Should give you an array with shape (1,10)

Ash Sharma
  • 470
  • 3
  • 18