Python:NumPy .diagonal()
The .diagonal() method returns the specified diagonal elements of a NumPy array. For 2D arrays, the diagonal consists of elements where the row index equals the column index (or with an offset). For multi-dimensional arrays, the axes specified by axis1 and axis2 define the matrix dimensions from which to extract the diagonal.
Unlike .trace(), which returns the sum of diagonal elements, .diagonal() returns the actual diagonal elements as an array. This method typically returns a view rather than a copy, making it memory-efficient for large arrays.
Syntax
ndarray.diagonal(offset=0, axis1=0, axis2=1)
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 is0(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 is0.axis2(Optional): The axis to be used as the second axis of the 2D sub-arrays from which the diagonals should be taken. Default is1.
Return value:
Returns an array containing the diagonal elements. For 2D arrays, this is a 1D array. For higher-dimensional arrays, the result has one fewer dimension than the input.
Example 1: Extracting Diagonals from 2D and Rectangular Arrays
This example demonstrates how to extract diagonals from both square and rectangular arrays using different offset values:
import numpy as np# Create a 3×3 square arraysquare = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9]])print("Square array:")print(square)print("\nMain diagonal:", square.diagonal())print("Upper diagonal (offset=1):", square.diagonal(offset=1))print("Lower diagonal (offset=-1):", square.diagonal(offset=-1))# Create a 4×3 rectangular arrayrectangular = np.array([[1, 2, 3],[4, 5, 6],[7, 8, 9],[10, 11, 12]])print("\nRectangular array:")print(rectangular)print("\nMain diagonal:", rectangular.diagonal())print("Upper diagonal (offset=1):", rectangular.diagonal(offset=1))print("Lower diagonal (offset=-2):", rectangular.diagonal(offset=-2))
The output produced by this code is:
Square array:[[1 2 3][4 5 6][7 8 9]]Main diagonal: [1 5 9]Upper diagonal (offset=1): [2 6]Lower diagonal (offset=-1): [4 8]Rectangular array:[[ 1 2 3][ 4 5 6][ 7 8 9][10 11 12]]Main diagonal: [1 5 9]Upper diagonal (offset=1): [2 6]Lower diagonal (offset=-2): [7 11]
For rectangular matrices, the diagonal length is determined by the smaller dimension, and different offsets extract parallel diagonals above or below the main one.
Example 2: Using .diagonal() with Multi-Dimensional Arrays
This example demonstrates how to extract diagonals from higher-dimensional arrays by specifying axes:
import numpy as np# Create a 3D array (2x3x3)array_3d = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],[[10, 11, 12], [13, 14, 15], [16, 17, 18]]])print("3D array shape:", array_3d.shape)print("3D array:")print(array_3d)# Extract diagonal from the last two dimensionsdiag_default = array_3d.diagonal()print("\nDiagonal (axis1=0, axis2=1):")print(diag_default)# Extract diagonal with different axesdiag_12 = array_3d.diagonal(axis1=1, axis2=2)print("\nDiagonal (axis1=1, axis2=2):")print(diag_12)
The output produced by this code is:
3D array shape: (2, 3, 3)3D array:[[[ 1 2 3][ 4 5 6][ 7 8 9]][[10 11 12][13 14 15][16 17 18]]]Diagonal (axis1=0, axis2=1):[[ 1 13][ 2 14][ 3 15]]Diagonal (axis1=1, axis2=2):[[ 1 5 9][10 14 18]]
For 3D arrays, .diagonal() extracts diagonal elements from 2D slices defined by the specified axes, resulting in an array with one fewer dimension.
Codebyte Example
Run the following codebyte example to understand the usage of the .diagonal() method:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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