>>> vec = np.asarray(l).reshape((1,-1))
>>> vec.shape
(1, 3)
I think is what you want ... maybe
Answer from Joran Beasley on Stack Overflowpython - Numpy - asarray of a list, dimension not automatically set in the vector - Stack Overflow
what's the difference between a numpy array with shape (x,) and (x, 1)?
np.asarray - unexpected behaviour
python - Unpack numpy array shape for general arrays - Stack Overflow
Videos
>>> vec = np.asarray(l).reshape((1,-1))
>>> vec.shape
(1, 3)
I think is what you want ... maybe
I think an easier way to read this (for me) is to use np.newaxis:
a = np.array([1,3,5])
a.shape
#(3,)
b = a[np.newaxis,...]
print b
#[[1, 3, 4]]
b.shape
#(1, 3)
But this is not a column vector..., maybe you want:
c = a[...,np.newaxis]
print c
#[[1],
# [3],
# [4]]
c.shape
#(3, 1)
You can also use None instead of np.newaxis wherever you want the new axis:
a[...,None]
#[[1],
# [3],
# [4]]
hey, im doing an ai thing in school and my code didnt work as expected, and after 5 hours i found out i reshaped an array from (206,) to (206,1) and that made the results wrong. and from what i understand, the shape means the length of each dimension, and length is not 0 indexed so a size of 1 would be equal to just 1D no?
Your array has ndim=1, which means len(array.shape)==1. Thus, you cannot unpack the shape tuple as if it were of length==2.
To "stretch" your array to have 2dim in case it currently has fewer, use np.atleast_2d.
>>> x = np.arange(3.0)
>>> y = np.atleast_2d(x)
>>> y
array([[ 0., 1., 2.]])
>>> m, n = y.shape
BTW, list and array are not good names for variables in python.
You showed us that:
>>> np.shape(array)
(4,)
that is, it is a single element tuple.
m, n = (4,)
produces the same error. There is one element in the tuple, so Python can only unpack it into 1 variable. This isn't a numpy issue. When doing this kind of unpacking, the numer of variables has to match the number of terms in the tuple (or list).
If you come from MATLAB you may expect all arrays to be 2d or larger. But in numpy, arrays can be 1d or even 0d (with shape ()). There are multiple of ensuring that your array has 2 dimensions - reshape, extra [], [None,...], np.atleast_2d.