AppDividend
Latest Code Tutorials

np.diag: How to Find Numpy Array Diagonal in Python

The numpy diag() function is defined under numpy, imported as import numpy as np. We can create multidimensional arrays and derive other mathematical statistics with the help of numpy, a library in Python. Python diag() name is also derived from diagonal.

np.diag

The np.diag() function extracts and constructs a diagonal array. The numpy diag() function takes an array and as parameters and returns the diagonal array from the given array.

Syntax

numpy.diag(arr,k) 

Parameters

It takes two parameters, out of which one parameter is optional. 

The first parameter is the array input represented by arr.

The second parameter is k, which is optional and takes 0 by default. If the value of this parameter is greater than 0, it means the diagonal is above the main diagonal and vice versa if it is not.

Return Value

It returns an array with a diagonal array.

Example programs on diag() method in Python

Write a program to show the diag() function’s working in Python.

# app.py

import numpy as np

a = np.matrix([[1, 2, 3], [4, 5, 6], [9, 8, 7]])

print("Main Diagonal: \n", np.diag(a), "\n")

print("Above main diagonal: \n", np.diag(a, 1),
      "\n")  # k=1 (for above main diagonal)

print("Below main diagonal: \n", np.diag(a, -1))  # k=-1 (for below main diagonal)

Output

Main Diagonal: [1 5 7] 
 
Above main diagonal: [2 6] 
 
Below main diagonal: [4 8]

In this example, we can see that by using numpy diag(), we can see that by passing different values of k, we can get their diagonal elements. Here we saw the main diagonal in the matrix, then the diagonal above the main diagonal by passing value k=1 and vice versa by passing value k=-1.

Example 2: Write a program to take a 4×4 matrix and apply the diag() function.

See the following code.

# app.py

import numpy as np

a = np.matrix([[1, 2, 3], [4, 5, 6], [9, 8, 7], [11, 13, 15]])

print("Main Diagonal: \n", np.diag(a), "\n")

# k=1 (for above main diagonal)
print("Above main diagonal: \n", np.diag(a, 1), "\n")

#k=-1 (for below main diagonal)
print("Below main diagonal: \n", np.diag(a, -1))

Output

python3 app.py
Main Diagonal:
 [1 5 7]

Above main diagonal:
 [2 6]

Below main diagonal:
 [ 4  8 15]

In this example, we passed a 4×4 matrix and got the required output of the main diagonal, above the main diagonal ( k=1) and below the main diagonal(k=-1).

Use the np arange() function to create an array and construct the diagonal.

See the following code.

import numpy as np

data = np.arange(12).reshape((4,3))
print(data)
dignl = np.diag(data, k=0)
print('The diagonal is: ')
print(dignl)

Output

python3 app.py
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
The diagonal is:
[0 4 8]

 In this example, we have taken k = 0. It is the main diagonal which is 

0    

      4    

            8

If we take k =1, it will return [1, 5]. See the following code.

# app.py

import numpy as np

data = np.arange(12).reshape((4,3))
print(data)
dignl = np.diag(data, k=1)
print('The diagonal is: ')
print(dignl)

Output

python3 app.py
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
The diagonal is:
[1 5]

If we take k = -1, it will give us the below diagonal of the main diagonal.

See the following code.

import numpy as np

data = np.arange(12).reshape((4, 3))
print(data)
dignl = np.diag(data, k=-1)
print('The diagonal is: ')
print(dignl)

Output

python3 app.py
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
The diagonal is:
[ 3  7 11]

 Our array’s main diagonal is [0, 4, 8], and below diagonal is [3, 7, 11].

That is why when we set k=-1, it will return [3, 7, 11]. If you pass k = -2, then it will return [6, 10]

Construct Diagonal From NumPy Array

If you want to create a diagonal from the array, you can use the np diag() method.

# app.py

import numpy as np

a = np.array([1, 2, 3, 4])
print(a)
d = np.diag(a)
print('The diagonal is: ')
print(d)

Output

python3 app.py
[1 2 3 4]
The diagonal is:
[[1 0 0 0]
 [0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]]

If you have the row vector, you can do the following.

# app.py

import numpy as np

a = np.array([[1, 2, 3, 4]])
print(a)
d = np.diag(a[0])
print('The diagonal is: ')
print(d)

Output

python3 app.py
[[1 2 3 4]]
The diagonal is:
[[1 0 0 0]
 [0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]]

 Conclusion

Python numpy.diagonal() function to extract a diagonal and wish to write to the resulting array; whether it returns a copy or a view depends on what version of numpy you are using.

That’s it for this tutorial.

See also

NumPy insert()

NumPy append()

NumPy bmat()

NumPy asmatrix()

NumPy diag_indices()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.