>>> vec = np.asarray(l).reshape((1,-1))
>>> vec.shape
(1, 3)
I think is what you want ... maybe
Answer from Joran Beasley on Stack OverflowW3Schools
w3schools.com โบ python โบ numpy โบ numpy_array_shape.asp
NumPy Array Shape
NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements. ... The example above returns (2, 4), which means that the array has 2 dimensions, where the first dimension has 2 ...
W3Schools
w3schools.com โบ python โบ numpy โบ numpy_array_reshape.asp
NumPy Array Reshaping
The shape of an array is the number of elements in each dimension. By reshaping we can add or remove dimensions or change number of elements in each dimension. Convert the following 1-D array with 12 elements into a 2-D array.
python - Numpy - asarray of a list, dimension not automatically set in the vector - Stack Overflow
@user1506145 I don't believe this is what the OP wants... shape should be (3, 1) to make a column vector. ... I am really tired of reshaping every vector after np.asarray(). More on stackoverflow.com
what's the difference between a numpy array with shape (x,) and (x, 1)?
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr.shape) # Output: (5,) arr = np.array([[1], [2], [3], [4], [5]]) print(arr.shape) # Output: (5, 1) arr = np.array([[1, 2, 3, 4, 5]]) print(arr.shape) # Output: (1, 5) More on reddit.com
np.asarray - unexpected behaviour
When I try to do np.asarray of a list of arrays, it results in different behaviour depending on the dimensions of the arrays. When the list of arrays is of dimensions like (n,n) and (n,) it results in error. Reproducing code example: imp... More on github.com
python - Shape of an array in numpy.asarray - Stack Overflow
array_split can split the array into unequal sized arrays. Look at As in the 102 case. Look at the [distance ...] list (before applying np.asarray to it. It will contain many arrays. Check their shapes. If they are all the same it makes an array with a new first dimension. More on stackoverflow.com
Videos
04:08
NumPy Array Shape - YouTube
09:22
Numpy Array Shape Manipulation using Reshape, Ravel, and Flatten ...
07:04
Shape and Reshape Numpy Arrays - Numpy For Machine Learning 5 - ...
09:43
Numpy Array Shape Tutorials - YouTube
05:47
NumPy Array Shape | Python NumPy Array Shape | NumPy Shape Function ...
NumPy
numpy.org โบ doc โบ stable โบ reference โบ generated โบ numpy.ndarray.shape.html
numpy.ndarray.shape โ NumPy v2.4 Manual
Method similar to setting shape. ... Try it in your browser! >>> import numpy as np >>> x = np.array([1, 2, 3, 4]) >>> x.shape (4,) >>> y = np.zeros((2, 3, 4)) >>> y.shape (2, 3, 4) >>> y.shape = (3, 8) >>> y array([[ 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0.]]) >>> y.shape = (3, 6) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: cannot reshape array of size 24 into shape (3,6) >>> np.zeros((4,2))[::2].shape = (-1,) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Incompatible shape for in-place modification.
NumPy
numpy.org โบ devdocs โบ reference โบ generated โบ numpy.shape.html
numpy.shape โ NumPy v2.5.dev0 Manual
The elements of the shape tuple give the lengths of the corresponding array dimensions. ... Equivalent array method. ... Try it in your browser! >>> import numpy as np >>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) ()
Top answer 1 of 2
6
>>> vec = np.asarray(l).reshape((1,-1))
>>> vec.shape
(1, 3)
I think is what you want ... maybe
2 of 2
1
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]]
Reddit
reddit.com โบ r/learnpython โบ what's the difference between a numpy array with shape (x,) and (x, 1)?
r/learnpython on Reddit: what's the difference between a numpy array with shape (x,) and (x, 1)?
October 6, 2023 -
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?
Top answer 1 of 4
2
import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr.shape) # Output: (5,) arr = np.array([[1], [2], [3], [4], [5]]) print(arr.shape) # Output: (5, 1) arr = np.array([[1, 2, 3, 4, 5]]) print(arr.shape) # Output: (1, 5)
2 of 4
2
Collections such as tuples, lists and 1d ndarrays are neither rows or columns (generally they display using whatever is most convenient) which si why: archive = ('hello', 'hello', 'hello') Can be rewritten as: archive = ('hello', 'hello', 'hello') And the two expressions are equivalent. For a ndarray the dimension of a vector is important for some applications: import numpy as np vector = np.array([0, 1, 2]) row = vector[np.newaxis, :] col = vector[:, np.newaxis] The vector: vector.shape vector.ndim Is a single dimension: (3, ) 1 The following are 2 dimensions: row.shape row.ndim (1, 3) 2 This is by definition 1 row by multiple (in this case 3) columns. col.shape cold.ndim (3, 1) 2 This is by definition multiple (in this case 3) rows by 1 column. For matrix multiplication for example, the inner dimensions need to match row @ col #(1, 3) @ (3, 1) And the array returned has the outer dimensions: array([[5]]) #(1) @ (1) If the other way is compared: col @ row #(3, 1) @ (1, 3) array([[0, 0, 0], [0, 1, 2], [0, 2, 4]]) #(3) @ (3)
Python Guides
pythonguides.com โบ python-numpy-shape
NumPy Shape And Array Dimensions In Python
May 16, 2025 - The shape attribute returns a tuple showing the size of each dimension. For a 1D array, itโs simply the number of elements. For a 2D array, itโs (rows, columns). For higher dimensions, it follows the same pattern. ... Letโs use a more practical example.
NumPy
numpy.org โบ doc โบ stable โบ reference โบ generated โบ numpy.asarray.html
numpy.asarray โ NumPy v2.4 Manual
>>> issubclass(np.recarray, np.ndarray) True >>> a = np.array([(1., 2), (3., 4)], dtype='f4,i4').view(np.recarray) >>> np.asarray(a) is a False >>> np.asanyarray(a) is a True
NumPy
numpy.org โบ doc โบ 2.2 โบ reference โบ generated โบ numpy.ndarray.shape.html
numpy.ndarray.shape โ NumPy v2.2 Manual
Method similar to setting shape. ... >>> import numpy as np >>> x = np.array([1, 2, 3, 4]) >>> x.shape (4,) >>> y = np.zeros((2, 3, 4)) >>> y.shape (2, 3, 4) >>> y.shape = (3, 8) >>> y array([[ 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0., 0.]]) >>> y.shape = (3, 6) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: total size of new array must be unchanged >>> np.zeros((4,2))[::2].shape = (-1,) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Incompatible shape for in-place modification.
Programiz
programiz.com โบ python-programming โบ numpy โบ methods โบ shape
NumPy shape()
The shape() method returns the shape of an array i.e. the number of elements in each dimension. import numpy as np array = np.array([[0, 1], [2, 3]])
GitHub
github.com โบ numpy โบ numpy โบ issues โบ 14221
np.asarray - unexpected behaviour ยท Issue #14221 ยท numpy/numpy
August 7, 2019 - import numpy as np a1 = [array([[-0.00491985, 0.00491965], [-0.00334969, 0.00334955], [-0.00136081, 0.00136076]], dtype=float32), array([-0.00104678, 0.00104674], dtype=float32)] a2 = [array([[-0.00334969, 0.00334955], [-0.00136081, 0.00136076]], dtype=float32), array([-0.00104678, 0.00104674], dtype=float32)] print(np.asarray(a1)) #works fine print(np.asarray(a2)) #throws error print(np.asarray([a.reshape(-1, 2) for a in a2])) #works fine
Author ย deviganesan-coder
NumPy
numpy.org โบ doc โบ stable โบ reference โบ generated โบ numpy.shape.html
numpy.shape โ NumPy v2.4 Manual
The elements of the shape tuple give the lengths of the corresponding array dimensions. ... Equivalent array method. ... Try it in your browser! >>> import numpy as np >>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) ()
GeeksforGeeks
geeksforgeeks.org โบ numpy-asarray-in-python
numpy.asarray() in Python - GeeksforGeeks
November 12, 2021 - Let's understand with a simple ... is used when we want to return a contiguous array in memory (C order). Syntax : numpy.ascontiguousarray(arr, dtype=None) Parameters : arr : [array_like] Input data, in any form that can be converted to an array. This includes scalars, lists, lists of tuples, tuples, ... numpy.asarray_chkfinite() function is used when we want to convert the input to an array, checking for NaNs (Not A Number) or Infs(Infinities)....
Stack Overflow
stackoverflow.com โบ questions โบ 53924928 โบ shape-of-an-array-in-numpy-asarray
python - Shape of an array in numpy.asarray - Stack Overflow
December 26, 2018 - QQ=np.asarray([distance.cdist(X,Y) for X in As for Y in Bs]) QQ.reshape(len(As),len(Bs) JJ=[np.concatenate((QQ[i,:]),axis=1) for i in range(len(As))] dist=np.concatenate((JJ[:]),axis=0) Gives exact same matrix with distance.cdist(A,B). But if Na=100, Nb=500 shape of previous array will be (100,10,50) prevent reshaping and concatenate operations.
Note.nkmk.me
note.nkmk.me โบ home โบ python โบ numpy
NumPy: Get the dimensions, shape, and size of an array | note.nkmk.me
April 23, 2025 - You can get the number of dimensions, the shape (length of each dimension), and the size (total number of elements) of a NumPy array (numpy.ndarray) using the ndim, shape, and size attributes. The built-in len() function returns the size of the first dimension. ... Use the following one- to three-dimensional arrays as examples. import numpy as np a_1d = np.arange(3) print(a_1d) # [0 1 2] a_2d = np.arange(12).reshape((3, 4)) print(a_2d) # [[ 0 1 2 3] # [ 4 5 6 7] # [ 8 9 10 11]] a_3d = np.arange(24).reshape((2, 3, 4)) print(a_3d) # [[[ 0 1 2 3] # [ 4 5 6 7] # [ 8 9 10 11]] # # [[12 13 14 15] # [16 17 18 19] # [20 21 22 23]]]
NumPy
numpy.org โบ devdocs โบ reference โบ generated โบ numpy.asarray.html
numpy.asarray โ NumPy v2.5.dev0 Manual
>>> issubclass(np.recarray, np.ndarray) True >>> a = np.array([(1., 2), (3., 4)], dtype='f4,i4').view(np.recarray) >>> np.asarray(a) is a False >>> np.asanyarray(a) is a True
CS231n
cs231n.github.io โบ python-numpy-tutorial
Python Numpy Tutorial (with Jupyter and Colab)
The number of dimensions is the rank of the array; the shape of an array is a tuple of integers giving the size of the array along each dimension. We can initialize numpy arrays from nested Python lists, and access elements using square brackets: import numpy as np a = np.array([1, 2, 3]) # ...
NumPy
numpy.org โบ devdocs โบ reference โบ generated โบ numpy.ndarray.shape.html
numpy.ndarray.shape โ NumPy v2.5.dev0 Manual
Method similar to setting shape. ... Try it in your browser! >>> import numpy as np >>> x = np.array([1, 2, 3, 4]) >>> x.shape (4,) >>> y = np.zeros((2, 3, 4)) >>> y.shape (2, 3, 4)