Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reshape an array in Python using Numpy?

Tags:

python

numpy

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 ?

like image 314
vbx8977 Avatar asked Dec 12 '25 01:12

vbx8977


2 Answers

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)
like image 132
CAPSLOCK Avatar answered Dec 14 '25 15:12

CAPSLOCK


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.

like image 25
Chitrank Dixit Avatar answered Dec 14 '25 13:12

Chitrank Dixit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!