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
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_shape.asp
NumPy Array Shape
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Discussions

What does shape[0] and shape[1] do in python? - Stack Overflow
In python shape[0] returns the dimension but in this code it is returning total number of set. More on stackoverflow.com
🌐 stackoverflow.com
python - x.shape[0] vs x[0].shape in NumPy - Stack Overflow
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. More on stackoverflow.com
🌐 stackoverflow.com
What does X.shape[0] mean? - Machine Learning - Coding Blocks Discussion Forum
Did not understand how this gives number of training examples? And shouldn’t the syntax should be X.shape only? Why [0] is there? More on discuss.codingblocks.com
🌐 discuss.codingblocks.com
2
0
October 17, 2019
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
🌐
Folkstalk
folkstalk.com › home › 2022 › october
Df.Shape 0 With Code Examples
October 5, 2022 - None is a data type of its own (NoneType) and only None can be None.https://www.w3schools.com › python › ref_keyword_nonePython None Keyword – W3Schools() Method The isnull() method returns a DataFrame object where all the values are replaced ...
🌐
W3Schools
w3schools.com › python › pandas › ref_df_shape.asp
Pandas DataFrame shape Property
The shape is the number of rows and columns of the DataFrame ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
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.
🌐
CopyProgramming
copyprogramming.com › howto › python-shape-0-0-what-does-this-results
Python: Understanding the Meaning of Python's Shape 0 0 Output
March 27, 2023 - The tuple shape specifies the dimensionality of the array. As Y.shape[0] has an index value of 0, you are operating on the first dimension of the array.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.5.dev0 Manual
>>> import numpy as np >>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) ()
Find elsewhere
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
🌐
Coding Blocks
discuss.codingblocks.com › machine learning
What does X.shape[0] mean? - Machine Learning - Coding Blocks Discussion Forum
October 17, 2019 - Did not understand how this gives number of training examples? And shouldn’t the syntax should be X.shape only? Why [0] is there?
🌐
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 - Did you notice how I used array.shape[0] and array.shape[1]? These let you dynamically adapt your loops to any array size.
🌐
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…
🌐
IQCode
iqcode.com › code › python › shape0-python
.shape[0] python Code Example
shape python numpy how to get shape in python pandas: shape shape matrix python shape 5 in python numpy np.shape(x,-1) .shape in python syntax how to use shape method in python df.shape() in python shape() function return in python shape() function in python np.shape 0 numpy.shape() what does .shape in python return python array.shape pandas .shape python shape 1 in python how to use .shape python x.shape[0] in python array python shape what does y.shape[0] means in python shape nu,py shape(9,0) to shape(X,9) NUMPY tf.shape()[-1] y.shape[1] np python (-1, 1) shape means array.shape[0] in pytho
🌐
Educative
discuss.educative.io › courses
Res.shape[0] Concept - Courses - Educative
February 17, 2021 - I didn’t understand the concept behind res.shape[0] in the solution. Please guide me further to understand it.
🌐
Python
docs.python.org › 3 › library › turtle.html
Turtle graphics — Python 3.14.3 documentation
February 23, 2026 - Home is at (0, 0). And after a while, it will probably help to clear the window so we can start anew: ... Let’s draw the star shape at the top of this page.
🌐
Medium
medium.com › @heyamit10 › understanding-pandas-shape-ba74dadf8387
Understanding pandas.shape
March 6, 2025 - rows = df.shape[0] # Number of rows columns = df.shape[1] # Number of columns print("Rows:", rows) print("Columns:", columns)
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-shape-method
Python shape() method - All you need to know! | DigitalOcean
August 4, 2022 - Hello, readers! This article talks about the Python shape() method and its variants in programming with examples.
🌐
Python Guides
pythonguides.com › python-numpy-shape
NumPy Shape And Array Dimensions In Python
May 16, 2025 - Learn how to use NumPy shape in Python to understand and manipulate array dimensions. Examples with real-world data, reshaping techniques, and common solutions.