The shape attribute for numpy arrays returns the dimensions of the array. If Y has n rows and m columns, then Y.shape is (n,m). So Y.shape[0] is n.

In [46]: Y = np.arange(12).reshape(3,4)

In [47]: Y
Out[47]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [48]: Y.shape
Out[48]: (3, 4)

In [49]: Y.shape[0]
Out[49]: 3
Answer from unutbu on Stack Overflow
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.5.dev0 Manual
len(a) is equivalent to np.shape(a)[0] for N-D arrays with N>=1. ndarray.shape · Equivalent array method. Examples · 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) () >>> a = np.array([(1, 2), (3, 4), (5, 6)], ...
Discussions

python - x.shape[0] vs x[0].shape in NumPy - Stack Overflow
x[0].shape gives you the length of the first row. x.shape[0] gives you the first component of the dimensions of 'x', 1024 rows by 10 columns. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... 439 Difference between numpy... More on stackoverflow.com
🌐 stackoverflow.com
Is there a difference between array.shape[0] and len(array) for a numpy array?
Don’t think so — I’d probably use arr.shape[0] regardless since it’s a bit more explicit what it’s doing (size of the array’s first dimension). More on reddit.com
🌐 r/learnpython
3
1
September 26, 2020
Difference between .shape[0] and .shape[1]
Hi, In the course, i find sometimes the code is written as m=X.shape[0] and n=w.shape[1]. Can you tell me the difference between these 2 functions, .shape[0] and .shape[1], though both returns the number of columns in an array More on community.deeplearning.ai
🌐 community.deeplearning.ai
0
0
August 27, 2022
python - What does a numpy shape starting with zero mean - Stack Overflow
For example, if you have an array a with shape (10, 100) and do np.sum(a[:idx], axis=0), the result will be correct even if idx is 0 (in which case it would result in a 100-vector of zeros). ... numpy arrays are defined not just by their data elements, but also by their shape. More on stackoverflow.com
🌐 stackoverflow.com
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.4 Manual
len(a) is equivalent to np.shape(a)[0] for N-D arrays with N>=1. ndarray.shape · Equivalent array method. Examples · 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) () >>> a = np.array([(1, 2), (3, 4), (5, 6)], ...
Top answer
1 of 4
22

x is a 2D array, which can also be looked upon as an array of 1D arrays, having 10 rows and 1024 columns. x[0] is the first 1D sub-array which has 1024 elements (there are 10 such 1D sub-arrays in x), and x[0].shape gives the shape of that sub-array, which happens to be a 1-tuple - (1024, ).

On the other hand, x.shape is a 2-tuple which represents the shape of x, which in this case is (10, 1024). x.shape[0] gives the first element in that tuple, which is 10.

Here's a demo with some smaller numbers, which should hopefully be easier to understand.

x = np.arange(36).reshape(-1, 9)
x

array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8],
       [ 9, 10, 11, 12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23, 24, 25, 26],
       [27, 28, 29, 30, 31, 32, 33, 34, 35]])

x[0]
array([0, 1, 2, 3, 4, 5, 6, 7, 8])

x[0].shape
(9,)

x.shape
(4, 9)

x.shape[0]
4
2 of 4
10

x[0].shape will give the Length of 1st row of an array. x.shape[0] will give the number of rows in an array. In your case it will give output 10. If you will type x.shape[1], it will print out the number of columns i.e 1024. If you would type x.shape[2], it will give an error, since we are working on a 2-d array and we are out of index. Let me explain you all the uses of 'shape' with a simple example by taking a 2-d array of zeros of dimension 3x4.

import numpy as np
#This will create a 2-d array of zeroes of dimensions 3x4
x = np.zeros((3,4))
print(x)
[[ 0.  0.  0.  0.]
[ 0.  0.  0.  0.]
[ 0.  0.  0.  0.]]

#This will print the First Row of the 2-d array
x[0]
array([ 0.,  0.,  0.,  0.])

#This will Give the Length of 1st row
x[0].shape
(4,)

#This will Give the Length of 2nd row, verified that length of row is showing same 
x[1].shape
(4,)

#This will give the dimension of 2-d Array 
x.shape
(3, 4)

# This will give the number of rows is 2-d array 
x.shape[0]
3

# This will give the number of columns is 2-d array 
x.shape[1]
3

# This will give the number of columns is 2-d array 
x.shape[1]
4

# This will give an error as we have a 2-d array and we are asking value for an index 
out of range
x.shape[2]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-20-4b202d084bc7> in <module>()
----> 1 x.shape[2]

IndexError: tuple index out of range
🌐
DeepLearning.AI
community.deeplearning.ai › course q&a › machine learning specialization › advanced learning algorithms
Difference between .shape[0] and .shape[1] - Advanced Learning Algorithms - DeepLearning.AI
August 27, 2022 - Hi, In the course, i find sometimes the code is written as m=X.shape[0] and n=w.shape[1]. Can you tell me the difference between these 2 functions, .shape[0] and .shape[1], though both returns the number of columns in a…
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_shape.asp
NumPy Array Shape
NumPy Editor NumPy Quiz NumPy Exercises NumPy Syllabus NumPy Study Plan NumPy Certificate ... The shape of an array is the number of elements in each dimension.
Find elsewhere
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.0 Manual
>>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) ()
🌐
GeeksforGeeks
geeksforgeeks.org › numpy › numpy-zeros-python
numpy.zeros() in Python - GeeksforGeeks
January 24, 2025 - numpy.zeros() function creates a new array of specified shapes and types, filled with zeros. It is beneficial when you need a placeholder array to initialize variables or store intermediate results.
🌐
Codegive
codegive.com › blog › numpy_array_shape_0.php
Numpy array shape 0
scalar_arr = np.array(42) ... these are distinct from arrays that are "empty" due to a dimension having a length of zero. NumPy arrays with zero-length dimensions (e.g., shape=(0,), (0, 5), (3, 0)) are a powerful and integral part of NumPy's design for handling datasets ...
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.3 Manual
len(a) is equivalent to np.shape(a)[0] for N-D arrays with N>=1. ndarray.shape · Equivalent array method. Examples · 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) () >>> a = np.array([(1, 2), (3, 4), (5, 6)], ...
🌐
Medium
medium.com › @amit25173 › understanding-numpy-shape-6fbb6b83891e
Understanding numpy.shape. If you think you need to spend $2,000… | by Amit Yadav | Medium
February 9, 2025 - For a 1D array, the shape might look like (n,), where n is the number of elements. For a 3D array, it could look like (x, y, z), representing depth, rows, and columns. Here’s another quick example to make it more fun. You might wonder: “What if I have a 3D array?” Let’s explore that: # Creating a 3D NumPy array array_3d = np.array([ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]) print("3D Array Shape:", array_3d.shape)
🌐
Python Guides
pythonguides.com › python-numpy-shape
NumPy Shape And Array Dimensions In Python
May 16, 2025 - Check out np.genfromtxt() Function in Python · One common use of shape is to validate input data: import numpy as np def process_monthly_data(data): # Check if the data has 12 months if data.shape[0] != 12: raise ValueError(f"Expected 12 months of data, got {data.shape[0]}") # Process data here return data.mean() # Test with correct data monthly_sales = np.random.randint(1000, 5000, size=12) print("Average monthly sales:", process_monthly_data(monthly_sales)) # Test with incorrect data try: quarterly_sales = np.random.randint(1000, 5000, size=4) process_monthly_data(quarterly_sales) except Va
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.1 Manual
len(a) is equivalent to np.shape(a)[0] for N-D arrays with N>=1. ndarray.shape · Equivalent array method. Examples · >>> import numpy as np >>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) () >>> a = np.array([(1, 2), (3, 4), (5, 6)], ...
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.2 Manual
len(a) is equivalent to np.shape(a)[0] for N-D arrays with N>=1. ndarray.shape · Equivalent array method. Examples · >>> import numpy as np >>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) () >>> a = np.array([(1, 2), (3, 4), (5, 6)], ...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.4 Manual
>>> 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.
🌐
Saturn Cloud
saturncloud.io › blog › understanding-the-difference-xshape0-vs-x0shape-in-numpy
Understanding the Difference: x.shape[0] vs x[0].shape in NumPy | Saturn Cloud Blog
October 4, 2023 - Remember, x.shape[0] gives you the size of the first dimension of your array, while x[0].shape gives you the shape of the first element of your array. Keep exploring, and happy data wrangling!
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.zeros.html
numpy.zeros — NumPy v2.1 Manual
Return a new array of given shape filled with value. ... >>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype array([(0, 0), (0, 0)], dtype=[('x', '<i4'), ('y', '<i4')])