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 Top answer 1 of 6
148
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
2 of 6
48
shape is a tuple that gives dimensions of the array..
>>> c = arange(20).reshape(5,4)
>>> c
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19]])
c.shape[0]
5
Gives the number of rows
c.shape[1]
4
Gives number of columns
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)], ...
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.
Reddit
reddit.com › r/learnpython › is there a difference between array.shape[0] and len(array) for a numpy array?
r/learnpython on Reddit: Is there a difference between array.shape[0] and len(array) for a numpy array?
September 26, 2020 -
And if so, which is better to use?
Top answer 1 of 2
2
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).
2 of 2
1
Actually it's generally better to use array.ndim. Both of your alternatives raise exceptions when used on zero-dimensional arrays: >>> a = np.array(1) >>> a.ndim 0 >>> a.shape () >>> a.shape[0] Traceback (most recent call last): File "", line 1, in IndexError: tuple index out of range >>> len(a) Traceback (most recent call last): File "", line 1, in TypeError: len() of unsized object I wouldn't be surprised if there are some other subtle differences too.
TutorialsPoint
tutorialspoint.com › article › what-does-shape-do-in-for-i-in-range-y-shape-using-matplotlib
What does .shape[] do in "for i in range(Y.shape[0])" using Matplotlib?
Y.shape[0] method would return 4, i.e., the first element of the tuple.
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.
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 - How to use len() in Python · For a numpy.ndarray, len() returns the size of the first dimension, which is equivalent to shape[0]. It is also equal to size only for one-dimensional arrays. print(len(a_1d)) # 3 print(a_1d.shape[0]) # 3 print(a_1d.size) # 3 print(len(a_2d)) # 3 print(a_2d.shape[0]) # 3 print(len(a_3d)) # 2 print(a_3d.shape[0]) # 2 ·
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 - # A 2D array array = np.array([[1, 2, 3], [4, 5, 6]]) # Print the number of rows and columns print("Number of rows:", array.shape[0]) print("Number of columns:", array.shape[1]) # Iterating through rows for i in range(array.shape[0]): print("Row", i + 1, ":", array[i]) Output: Number of rows: 2 Number of columns: 3 Row 1 : [1 2 3] Row 2 : [4 5 6] Did you notice how I used array.shape[0] and array.shape[1]? These let you dynamically adapt your loops to any array size.
AskPython
askpython.com › home › python shape function: find dimensions of arrays and dataframes
Python Shape Function: Find Dimensions of Arrays and DataFrames - AskPython
April 8, 2023 - To use the shape function in Python, first import the Pandas library with import pandas as pd and create a DataFrame using df = pd.DataFrame(data). Then, obtain the dimensions as a tuple using dimensions = df.shape. For NumPy arrays, import the NumPy library using import numpy as np, create an array with arr = np.array(data), and get the dimensions with dimensions = arr.shape.
Wikipedia
en.wikipedia.org › wiki › Sigmoid_function
Sigmoid function - Wikipedia
January 19, 2026 - A sigmoid function is convex for values less than a particular point, and it is concave for values greater than that point: in many of the examples here, that point is 0.
NumPy
numpy.org › devdocs › reference › generated › numpy.zeros.html
numpy.zeros — NumPy v2.5.dev0 Manual
Return a new array of given shape and type, filled with zeros.
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.shape.html
pandas.DataFrame.shape — pandas 3.0.1 documentation
Unlike the len() method, which only returns the number of rows, shape provides both row and column counts, making it a more informative method for understanding dataset size.