Python:NumPy .trace()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 28, 2025
Contribute to Docs

The .trace() method returns the sum of the elements along the diagonal of a NumPy array. For 2D arrays, the diagonal consists of elements where the row index equals the column index. For multi-dimensional arrays, the axes specified by axis1 and axis2 define the matrix dimensions for diagonal summation.

The .trace() method supports optional parameters to select a diagonal offset or specify axes, making it versatile for arrays of different shapes and orientations.

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

ndarray.trace(offset=0, axis1=0, axis2=1, dtype=None, out=None)

Parameters:

  • offset (Optional): The diagonal offset from the main diagonal. A positive value selects a diagonal above the main diagonal, while a negative value selects one below. Default is 0 (main diagonal).
  • axis1 (Optional): The axis to be used as the first axis of the 2D sub-arrays from which the diagonals should be taken. Default is 0.
  • axis2 (Optional): The axis to be used as the second axis of the 2D sub-arrays from which the diagonals should be taken. Default is 1.
  • dtype (Optional): The data type of the returned array. If not specified, it is determined from the input array.
  • out (Optional): An alternative output array to place the result. It must have the same shape as the expected output.

Return value:

Returns the sum of the diagonal elements as a scalar or array, depending on the input array’s dimensions and the specified parameters.

Example

This example demonstrates how to use .trace() to calculate the sum of the main diagonal elements of a 2D array:

import numpy as np
# Create a 2D array
array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Original array:")
print(array_2d)
# Calculate the trace (sum of main diagonal)
trace_value = array_2d.trace()
print("\nTrace (sum of diagonal elements):", trace_value)

The output produced by this code is:

Original array:
[[1 2 3]
[4 5 6]
[7 8 9]]
Trace (sum of diagonal elements): 15

This code creates a 3×3 array and calculates the trace by summing the diagonal elements (1 + 5 + 9 = 15).

Codebyte Example

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours