Reputation: 7809
I would like to change the shape of a numpy array
from this...
[ 3803.74060338 2041.36742598 2884.96439427 17561.9569717 ]
to this...
[ [3803.74060338]
[2041.36742598]
[2884.96439427]
[17561.9569717] ]
I was trying np.reshape(my_array, (1,1))
but I keep getting this error...
ValueError: total size of new array must be unchanged
Is reshape
what I want to do here?
Upvotes: 0
Views: 1830
Reputation: 12587
The reasons np.shape()
didn't work was because you are reshaping a 4 element array into a 1 x 1.
>>> import numpy as np
>>> a=np.array([1,2,3,4,5])
>>> a
array([1, 2, 3, 4, 5])
>>> a.reshape(len(a),1)
array([[1],
[2],
[3],
[4],
[5]])
>>>
Upvotes: 3
Reputation: 281381
Slice with numpy.newaxis
to put additional axes into the shape of an array:
>>> my_array = numpy.array([1, 2, 3, 4, 5])
>>> my_array[:, numpy.newaxis]
array([[1],
[2],
[3],
[4],
[5]])
(numpy.newaxis
is None
, so you can also just use None
directly. I find newaxis
more readable, but it's a matter of personal preference.)
Upvotes: 2