In Python, high-performance matrix operations are handled by the NumPy library. When working with NumPy, getting the "size" of a matrix can mean two different things: its dimensions (the number of rows and columns) or the total number of elements it contains. NumPy provides simple, direct attributes on its array objects to get both of these values.
This guide will show you how to use the .shape and .size attributes to get the dimensions of a NumPy array, with a note on the legacy numpy.matrix object.
Understanding "Size": Shape vs. Total Elements
When inspecting a NumPy matrix or array, you can retrieve two different "size" metrics:
.shape: An attribute that returns a tuple representing the dimensions of the array. For a 2D matrix, this will be(rows, columns)..size: An attribute that returns an integer representing the total number of elements in the array (which isrows * columns).
Method 1: Using numpy.ndarray (Recommended)
The modern and standard way to work with matrices in NumPy is to use the numpy.ndarray object, typically created with np.array().
Solution:
import numpy as np
my_array = np.array([[1, 2], [3, 4], [5, 6]])
matrix_shape = my_array.shape
matrix_size = my_array.size
print(f"The matrix is:\n{my_array}")
print(f"Shape of the matrix: {matrix_shape}")
print(f"Size (total elements) of the matrix: {matrix_size}")Output:
The matrix is:
[[1 2]
[3 4]
[5 6]]
Shape of the matrix: (3, 2)
Size (total elements) of the matrix: 6Method 2: Using the Legacy numpy.matrix Object
The numpy.matrix object is a specialized subclass of ndarray that is strictly 2-dimensional. While its use is no longer recommended for new code, you may encounter it in older projects. It has the same .shape and .size attributes.
Solution:
import numpy as np
my_matrix = np.matrix([[1, 2], [3, 4], [5, 6]])
matrix_shape = my_matrix.shape
matrix_size = my_matrix.size
print(f"The matrix is:\n{my_matrix}")
print(f"Shape of the matrix: {matrix_shape}")
print(f"Size (total elements) of the matrix: {matrix_size}")Output:
The matrix is:
[[1 2]
[3 4]
[5 6]]
Shape of the matrix: (3, 2)
Size (total elements) of the matrix: 6Getting Individual Dimensions (Rows and Columns)
A very common task is to get the number of rows and columns into separate variables. You can easily do this by unpacking the tuple returned by the .shape attribute.
Solution:
import numpy as np
my_array = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
rows, columns = my_array.shape
print(f"The matrix has {rows} rows.")
print(f"The matrix has {columns} columns.")Output:
The matrix has 2 rows.
The matrix has 4 columns.Conclusion
| To Get... | Use This Attribute | Result | Example |
|---|---|---|---|
| Dimensions | .shape |
A tuple (rows, columns) |
(3, 2) |
| Total Elements | .size |
An integer | 6 |
| Row/Column Counts | Unpack .shape |
Separate integer variables | rows, cols = my_array.shape |
To find the size of a matrix in Python, you should use the NumPy library. The .shape attribute will give you its dimensions in a (rows, columns) format, and the .size attribute will give you the total number of elements.