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
>>> import numpy as np >>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) ()
🌐
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…
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.3 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) ()
🌐
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.
🌐
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')])
🌐
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.
Find elsewhere
🌐
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)
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.4 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) ()
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.shape.html
numpy.shape — NumPy v2.2 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) ()
🌐
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) ()
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.2 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: 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.
🌐
NumPy
numpy.org › doc › 1.19 › reference › generated › numpy.shape.html
numpy.shape — NumPy v1.19 Manual
June 29, 2020 - >>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 2]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) ()
🌐
NumPy
numpy.org › doc › 1.25 › reference › generated › numpy.shape.html
numpy.shape — NumPy v1.25 Manual
>>> np.shape(np.eye(3)) (3, 3) >>> np.shape([[1, 3]]) (1, 2) >>> np.shape([0]) (1,) >>> np.shape(0) ()
🌐
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?
🌐
Qiita
qiita.com › python
【備忘録】shape[0], shape[1], shape[2]の中身 #Python - Qiita
August 25, 2020 - Go to list of users who liked · Add to Hatena Bookmark
🌐
NumPy
numpy.org › devdocs › user › quickstart.html
NumPy quickstart — NumPy v2.5.dev0 Manual
Even though 0 occurs twice in the list of indices, the 0th element is only incremented once. This is because Python requires a += 1 to be equivalent to a = a + 1. When we index arrays with arrays of (integer) indices we are providing the list of indices to pick. With boolean indices the approach is different; we explicitly choose which items in the array we want and which ones we don’t. The most natural way one can think of for boolean indexing is to use boolean arrays that have the same shape as the original array:
🌐
Interactive Chaos
interactivechaos.com › en › python › function › ndarrayshape
ndarray.shape | Interactive Chaos
January 21, 2019 - The numpy.ndarray.shape attribute returns a tuple with the size of the array, but can also be used to resize it.