Skip to main content

Python NumPy: How to Get the Size of a Matrix

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 is rows * columns).

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

# Create a 3x2 matrix using a NumPy array
my_array = np.array([[1, 2], [3, 4], [5, 6]])

# Get the shape (dimensions) of the array
matrix_shape = my_array.shape

# Get the size (total number of elements) of the array
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: 6
note

Using numpy.array is the standard practice for all new numerical code in Python. The older numpy.matrix object (covered next) is generally discouraged.

Method 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

# Create a 3x2 matrix using the legacy matrix object
my_matrix = np.matrix([[1, 2], [3, 4], [5, 6]])

# The .shape and .size attributes work identically
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: 6

Getting 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]])

# Unpack the shape tuple directly into 'rows' and 'columns' variables
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 AttributeResultExample
Dimensions.shapeA tuple (rows, columns)(3, 2)
Total Elements.sizeAn integer6
Row/Column CountsUnpack .shapeSeparate integer variablesrows, 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.