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
🌐
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…
Discussions

python - What does .shape[] do in "for i in range(Y.shape[0])"? - Stack Overflow
and http://www.scipy.org/Numpy_Example_List#shape has some more examples. ... @HipsterCarlGoldstein Just a friendly note, if any one of these answers provided solved your problem please consider accepting it by clicking the checkmark next to the answer. This will give you and the answerer both some rep points and also mark this problem as solved - thanks. 2012-06-17T19... 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
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
python - Why use arrays of shape (x,) rather than (x,1)? - Stack Overflow
1D arrays are supposed to be ... or (1, x) are 2D arrays. They have two dimensions, one of them set to 1. Can you be more specific in what type of bug you encounter? I suspect these bugs might actually be features :) ... Better fix the bug where it is. – Stop harming Monica Commented Apr 3, 2017 at 13:14 ... Each item in shape's tuple denotes ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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?

🌐
DigitalOcean
digitalocean.com › community › tutorials › python-shape-method
Python shape() method - All you need to know! | DigitalOcean
August 4, 2022 - Yes, it returns a tuple value that indicates the dimensions of a Python object. To understand the output, the tuple returned by the shape() method is the actual number of elements that represent the value of the dimension of the object.
🌐
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?
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.2 Manual
Method similar to setting shape. Examples · >>> 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.
Find elsewhere
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.5.dev0 Manual
As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the array and the remaining dimensions. Reshaping an array in-place will fail if a copy is required. ... Setting arr.shape is deprecated and may be removed in the future. Using ndarray.reshape is the preferred approach. ... Equivalent getter function. ... Function similar to setting shape. ... Method similar to setting shape. ... Try it in your browser! >>> 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)
🌐
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 ·
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.shape.html
pandas.DataFrame.shape — pandas 3.0.1 documentation
X · Mastodon · property DataFrame.shape[source]# Return a tuple representing the dimensionality of the DataFrame. 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.
🌐
Bogotobogo
bogotobogo.com › python › python_numpy_array_tutorial_basic_A.php
Python Tutorial: NumPy Array Basics A - 2020
T >>> bt array([[5], [6]]) >>> bt.shape (2, 1) >>> np.concatenate((a,bt),axis=1) array([[1, 2, 5], [3, 4, 6]]) Continued to NumPy Array Basics B. Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization ... Sponsor Open Source development activities and free contents for everyone. ... Python Home Introduction Running Python Programs (os, sys, import) Modules and IDLE (Import, Reload, exec) Object Types - Numbers, Strings, and None Strings - Escape Sequence, Raw String, and Slicing Strings - Methods Formatting Strings
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.4 Manual
As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the array and the remaining dimensions. Reshaping an array in-place will fail if a copy is required. ... Setting arr.shape is discouraged and may be deprecated in the future. Using ndarray.reshape is the preferred approach. ... Equivalent getter function. ... Function similar to setting shape. ... Method similar to setting shape. ... Try it in your browser! >>> 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)
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.3 Manual
As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the array and the remaining dimensions. Reshaping an array in-place will fail if a copy is required. ... Setting arr.shape is discouraged and may be deprecated in the future. Using ndarray.reshape is the preferred approach. ... Equivalent getter function. ... Function similar to setting shape. ... Method similar to setting shape. ... Try it in your browser! >>> 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)
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.ndarray.shape.html
numpy.ndarray.shape — NumPy v2.1 Manual
Method similar to setting shape. Examples · >>> 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.
🌐
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 - What happens if you try to access an index beyond the shape? This might surprise you: if you try to access a dimension that doesn’t exist, NumPy won’t quietly ignore it — it’ll throw an error! It’s like asking for a third floor in a building that only has two. ... # A 1D array array = np.array([1, 2, 3]) # Trying to access a nonexistent dimension print(array.shape[1]) # This will raise an error
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_shape.asp
NumPy Array Shape
In the example above at index-4 we have value 4, so we can say that 5th ( 4 + 1 th) dimension has 4 elements. ... 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