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
Answer from coldspeed95 on Stack Overflow
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?
Discussions

python - What is different between x.shape[0] and x.shape in numpy? - Stack Overflow
Your problem is that you don't understand what x.shape is giving you. ... Reason being np.arange accepts either a scalar (which creates an array starting at 0 and increasing by 1 to the length provided) or 3 scalars which define where to start and end the array and the step size. More on stackoverflow.com
🌐 stackoverflow.com
python - What does .shape[] do in "for i in range(Y.shape[0])"? - Stack Overflow
I'm trying to break down a program line by line. Y is a matrix of data but I can't find any concrete data on what .shape[0] does exactly. More on stackoverflow.com
🌐 stackoverflow.com
February 3, 2021
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
🌐 r/learnpython
6
1
October 6, 2023
W 2 A1 | Exercise 2 : X_flatten = X.reshape(X.shape[0], -1).T?
I understand what the matrix.T and X.shape[0] do, but how is the parameter “-1” interpreted here? What other parameters can be here instead of “-1”. In the optional practice assignment- “Python_Basics_with_Numpy”, Exe… More on community.deeplearning.ai
🌐 community.deeplearning.ai
0
2
September 17, 2022
🌐
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
🌐
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 function x[0].shape provides the length of the initial row, while x.shape[0] provides the first dimension component of 'x', which has 1024 rows and 10 columns.
Find elsewhere
🌐
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?

🌐
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 › deep learning specialization › neural networks and deep learning
W 2 A1 | Exercise 2 : X_flatten = X.reshape(X.shape[0], -1).T? - Neural Networks and Deep Learning - DeepLearning.AI
September 17, 2022 - I understand what the matrix.T and X.shape[0] do, but how is the parameter “-1” interpreted here? What other parameters can be here instead of “-1”. In the optional practice assignment- “Python_Basics_with_Numpy”, Exercise 5 it is mentioned: “You can use v = v.reshape(-1, 1). Just make sure you understand why it works.” There we were reshaping a 3 dimensional (num_px, num_px, 3) matrix into a (num_px * num_px * 3, 1) column vector.
🌐
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) () >>> a = np.array([(1, 2), (3, 4), (5, 6)], ... dtype=[('x', 'i4'), ('y', 'i4')]) >>> np.shape(a) (3,) >>> a.shape (3,) Go BackOpen In Tab ·
🌐
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) () >>> a = np.array([(1, 2), (3, 4), (5, 6)], ... dtype=[('x', 'i4'), ('y', 'i4')]) >>> np.shape(a) (3,) >>> a.shape (3,) Go BackOpen In Tab ·
🌐
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.
🌐
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
🌐
Medium
medium.com › @nearlydaniel › assertion-of-arbitrary-array-shapes-in-python-3c96f6b7ccb4
Assertion of arbitrary array shapes in Python | by Daniel | Medium
September 15, 2019 - assert array1.shape[0] == array2.shape[0] assert len(array1.shape) == 3 # check for 3x100x100x100!