NumPy: numpy.diag() function
numpy.diag() function
The numpy.diag() function creates a diagonal matrix or extracts the diagonal elements of a matrix. It can also construct a diagonal array from a one-dimensional array.
Syntax:
numpy.diag(v, k=0)

Parameters:
Name | Description | Required / Optional |
---|---|---|
v | If v is a 2-D array, return a copy of its k-th diagonal. If v is a 1-D array, return a 2-D array with v on the k-th diagonal. | Required |
k | Diagonal in question. The default is 0. Use k>0 for diagonals above the main diagonal, and k<0 for diagonals below the main diagonal. | optional |
Return value:
out : ndarray - The extracted diagonal or constructed diagonal array.
Example: Extracting Diagonal Elements from a Matrix using Numpy diag()
>>> import numpy as np
>>> a = np.arange(12).reshape((4,3))
>>> np.diag(a)
array([0, 4, 8])
The above code demonstrates the use of the Numpy function diag() to extract the diagonal elements of a matrix.
First, a 2-dimensional array a is created using the arange() function which produces a 1-dimensional array of 12 elements, which is then reshaped to a 4x3 matrix using reshape().
Next, the diag() function is called on the matrix a which returns a 1-dimensional array of the diagonal elements of a.
Pictorial Presentation:

Example: Extracting diagonal elements with a given offset from a 2D array
>>> import numpy as np
>>> a = np.arange(12).reshape((4,3))
>>> np.diag(a, k=1)
array([1, 5])
The above code demonstrates the use of the numpy.diag() function to extract the diagonal elements of a 2D array with a given offset. First, a 2D array of shape (4,3) is created using np.arange(12).reshape((4,3)).
Then, np.diag(a, k=1) is used to extract the diagonal elements with an offset of 1. This means that the function returns the elements of the array that are one diagonal above the main diagonal.
Example: Extracting Subdiagonal from a Matrix
>>> import numpy as np
>>> a = np.arange(12).reshape((4,3))
>>> np.diag(a, k=-1)
array([ 3, 7, 11])
In the above code a 4x3 matrix is created by reshaping a range of values from 0 to 11 using the reshape() method. Then, np.diag() is used to extract the subdiagonal from the matrix. The k parameter is set to -1 to indicate that the subdiagonal one below the main diagonal should be extracted.
Finally the resulting output is an array containing the values [3, 7, 11], which are the elements of the subdiagonal.
Example: Extract diagonal elements and create diagonal matrix
>>> import numpy as np
>>> a = np.arange(12).reshape((4,3))
>>> np.diag(np.diag(a))
array([[0, 0, 0],
[0, 4, 0],
[0, 0, 8]])
The above code creates a 4x3 matrix a using numpy.arange() and numpy.reshape(). It then extracts the diagonal elements of a using numpy.diag(), which returns a 1D array of those elements.
The resulting 1D array is then used as input to numpy.diag() again to create a new matrix where the diagonal elements of a become the diagonal of the new matrix, while all other elements are set to 0.
The final output is a 3x3 matrix with all elements set to 0 except for the diagonal elements which are the same as the diagonal elements of the original a matrix.
Pictorial Presentation:

Python - NumPy Code Editor:
Previous: ogrid()
Next: diagflat()
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join