Use .shape to obtain a tuple of array dimensions:

>>> a.shape
(2, 2)
Answer from Felix Kling on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.matrix.shape.html
numpy.matrix.shape — NumPy v2.4 Manual
matrix.shape# Tuple of array dimensions. The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it. 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.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.matrix.size.html
numpy.matrix.size — NumPy v2.4 Manual
matrix.size# Number of elements in the array. Equal to np.prod(a.shape), i.e., the product of the array’s dimensions. Notes · a.size returns a standard arbitrary precision Python integer.
Discussions

How do I find the length (or dimensions, size) of a numpy matrix in python? - Stack Overflow
So, is there a more intuitive way to find the size of a matrix, or is this the best I have? ... First result for numpy dimensions btw, try the search box ... More on stackoverflow.com
🌐 stackoverflow.com
How to correctly count (and think about) dimensions in a numpyarray
d is 2 x 3 matrix, so why would d = 3? No it's not. >>> d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) >>> d.shape (2, 2, 3) It's a 2x2x3 array (the word "matrix" doesn't work here). To put it another way, it's a 2 matrices, each 2x3. This 3rd dimension is sometimes called "pages". So 2 pages, each page with 2 rows, each row with 3 items ... 2x2x3. More on reddit.com
🌐 r/learnpython
8
1
July 27, 2023
How can I find the dimensions of a matrix in Python? - Stack Overflow
The correct pythonic way to get the dimensions of a matrix (formed using np.array) would be to use .shape. Let a be your matrix. To get the dimensions you would write a.shape, which would return (# columns, # rows). 2019-01-11T16:23:35.58Z+00:00 ... @amc I believe you have the return values reversed. Should be (# rows, # columns) 2022-07-20T03:48:13.547Z+00:00 ... If you are using NumPy ... More on stackoverflow.com
🌐 stackoverflow.com
Numpy: What's the point of producing arrays of shape (N,) instead of (N,1)? Super annoying feature.
I don't have a good answer for you, I share your frustration. I do have a couple tips that might help sometimes. Numpy's newaxis is an alias for None, so you can quickly expand dimensions with X[:, None] or X[None, :] (etc.) Also, you can pass keepdims=True as an argument to many numpy functions to produce an array that broadcasts properly. More on reddit.com
🌐 r/learnpython
12
3
August 29, 2020
🌐
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 - You can get the number of dimensions of a NumPy array as an integer using the ndim attribute.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-get-the-number-of-dimensions-of-a-matrix-using-numpy-in-python
How to get the number of dimensions of a matrix using NumPy in Python? - GeeksforGeeks
July 23, 2025 - Use ndim attribute available with the NumPy array as numpy_array_name.ndim to get the number of dimensions. Alternatively, we can use the shape attribute to get the size of each dimension and then use len() function for the number of dimensions.
🌐
SciPy
docs.scipy.org › doc › numpy-1.10.1 › reference › generated › numpy.matrix.size.html
numpy.matrix.size — NumPy v1.10 Manual
numpy.matrix · index · next · previous · matrix.size¶ · Number of elements in the array. Equivalent to np.prod(a.shape), i.e., the product of the array’s dimensions. Examples · >>> x = np.zeros((3, 5, 2), dtype=np.complex128) >>> x.size 30 >>> np.prod(x.shape) 30 ·
Find elsewhere
🌐
SciPy
docs.scipy.org › doc › numpy-1.13.0 › reference › generated › numpy.matrix.shape.html
matrix. shape - Numpy and Scipy Documentation
June 10, 2017 - numpy.matrix · index · next · previous · matrix.shape¶ · Tuple of array dimensions. Notes · May be used to “reshape” the array, as long as this would not require a change in the total number of elements ·
🌐
Sololearn
sololearn.com › en › Discuss › 2640053 › do-the-rows-or-the-columns-count-for-dimensions-of-a-matrix-in-numpy
Do the rows or the columns count for dimensions of a Matrix in NumPy? | Sololearn: Learn to code for FREE!
let there be a numpy array given as >>> x = np.array([[5, 2, 3], [2, 3, 2]]) >>> x.ndim [out] 2 This means (to my knowledge) that there are 2 dimension - obviously rows here. This confuses me because many YouTubers, books and especially 3b1b uses columns as dimension of the matrix!
🌐
NumPy
numpy.org › doc › 1.13 › reference › generated › numpy.matrix.shape.html
numpy.matrix.shape — NumPy v1.13 Manual
matrix.shape¶ · Tuple of array dimensions. Notes · May be used to “reshape” the array, as long as this would not require a change in the total number of elements ·
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_shape.asp
NumPy Array Shape
NumPy arrays have an attribute ... above returns (2, 4), which means that the array has 2 dimensions, where the first dimension has 2 elements and the second has 4....
🌐
IncludeHelp
includehelp.com › python › how-to-find-the-length-or-dimensions-size-of-a-numpy-matrix.aspx
Python - How to find the length (or dimensions, size) of a NumPy matrix?
This method will return a tuple that contains the number of words and number of columns and also, NumPy provides us a method called matrix.size() which returns the number of elements in the array. ... # Import numpy import numpy as np # Creating a numpy array arr = np.array([ [5, 2, 3, 1, 4, 5], [5, 2, 3, 1, 4, 5], [5, 2, 3, 1, 4, 5] ]) # Display original arrays print("Original Array:\n",arr,"\n") # Using arr.shape method to find the size res = arr.shape # Display result print("Dimensions of the given array:\n",res)
🌐
Medium
medium.com › @rlwh1019 › numpy-fundamentals-understanding-matrix-dimensions-ad52334d4128
NumPy Fundamentals — Understanding Matrix Dimensions | by Ray Lai | Medium
November 4, 2024 - The next dimension to the left represents the number of layers of 3D-array. This is also documented in the official NumPy tutorial: It is familiar practice in mathematics to refer to elements of a matrix by the row index first and the column index second. This happens to be true for two-dimensional arrays, but a better mental model is to think of the column index as coming last and the row index as second to last.
🌐
NumPy
numpy.org › doc › stable › reference › arrays.ndarray.html
The N-dimensional array (ndarray) — NumPy v2.4 Manual
An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N non-negative integers that specify the sizes of each dimension.
🌐
Studyopedia
studyopedia.com › home › dimensions in numpy arrays
Dimensions in Numpy Arrays - Studyopedia
March 28, 2025 - In this lesson, we will learn how to check how many dimensions an array has with the numpy.ndarray.ndim() method in Python Numpy.
🌐
Reddit
reddit.com › r/learnpython › how to correctly count (and think about) dimensions in a numpyarray
r/learnpython on Reddit: How to correctly count (and think about) dimensions in a numpyarray
July 27, 2023 -

from https://www.w3schools.com/python/numpy/trypython.asp?filename=demo_numpy_ndim

import numpy as np

a = np.array(42)

b = np.array([1, 2, 3, 4, 5])

c = np.array([[1, 2, 3], [4, 5, 6]])

d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])

print(a.ndim)

print(b.ndim)

print(c.ndim)

print(d.ndim)

--- output

0

1

2

3

---

Answers, explained:

  • a is just a number, so 0 dimensions

  • b is 1 row, so 1 dimension

  • c has 2 rows, so 2 dimensions.

but d ?

How do you correctly describe matrix d in words?

(deleted: d is 2 x 3 matrix, so why would d = 3?)

🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.matrix.resize.html
numpy.matrix.resize — NumPy v2.1 Manual
matrix.resize(new_shape, refcheck=True)# Change shape and size of array in-place. Parameters: new_shapetuple of ints, or n ints · Shape of resized array. refcheckbool, optional · If False, reference count will not be checked. Default is True. Returns: None ·
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.matrix.html
numpy.matrix — NumPy v2.3 Manual
View of the matrix transposed array. ... Total bytes consumed by the elements of the array. ... Number of array dimensions.
🌐
CodeSignal
codesignal.com › learn › courses › fundamentals-of-vectors-and-matrices-with-numpy › lessons › matrix-properties-shape-size-and-transpose
Matrix Properties: Shape, Size, and Transpose
Matrices are a set of numbers arranged in rows and columns, much like a spreadsheet. Using Python, and specifically the NumPy library, allows you to handle these mathematical structures efficiently. ... Shape: This property tells you the dimensions of a matrix, much like saying a room is 3 ...