How to get Diagonal of NumPy Array Using diag()

NumPy diag() function in Python is used to extract a diagonal or construct a diagonal array. This function takes an array and k as parameters and returns the diagonal array from the given array. In this article, I will explain how to use NumPy diag() function and using how to extract or construct a diagonal array from the given array with examples.

1. Quick Examples of Python NumPy Diagonal

If you are in a hurry, below are some quick examples of how to get NumPy diagonal in Python by using diag().


# Below are a quick example

# Example 1: use main Diagonal elements
arr2 = np.diag(arr)

# Example 2: use above main diagonal
arr2 = np.diag(arr, 1)

# Example 3: use below main diagonal
arr2 = np.diag(arr, -1)

# Example 4: construct diagonal from numpy array
arr = np.array([3, 7, 12, 18])
arr2 = np.diag(arr)

# Example 5: use diag to numpy row vector
arr = np.array([[3, 7, 12, 18]])
arr2 = np.diag(arr[0])

2. Syntax of numpy.diag() Function

Following is the syntax to create numpy.diag() function.


# Syntax of numpy.diag()
numpy.diag(arr, k=0)

2.1 Parameter of diag()

  • arr: Input array arr. For a 2-D array, it returns a copy of its k-th diagonal. For 1-D array, return a 2-D array with arr on the k-th diagonal.
  • k: The default is 0. Use k > 0 to get diagonals above the main diagonal, and k < 0 to get diagonals below the main diagonal.

2.2 Return Value of diag

This function is used to extract diagonal or constructed diagonal arrays.

3. Usage numpy.diag() To Extract Diagonal

Numpy diag() function is used to extract or construct a diagonal 2-d array. It contains two parameters: an input array and k, which decides the diagonal, i.e., k=0 for the main diagonal, k=1 for the above main diagonal, or k=-1 for the below diagonal. It is used to perform the mathematical and statistics operation on the multidimensional array.


import numpy as np
  
# matrix creation by array input
arr = np.matrix([[9, 18, 25], 
                 [155 ,240, 68], 
                 [29, 82, 108]])

# use main Diagonal elements
arr2 = np.diag(arr)
print(arr2)

# Output
# [  9 240 108]

If we provide k=1, it will return the diagonal one above the main diagonal, from our example, it returns [18, 68].


# use above main diagonal
arr2 = np.diag(arr, 1)
print(arr2)

# Output
# [18 68]

If we use k=-1, it will give us the below diagonal of the main diagonal, from our example, it returns [155 82].


# use below main diagonal
arr2 = np.diag(arr, -1)
print(arr2)

# Output
# [155  82]

4. Take a 4×4 Matrix and Apply the diag() Function

Let’s take the 4X4 matrix and get the NumPy diagonal from it. From the below example, it returns 6, 35, and 27.


import numpy as np
  
# use 4×4 matrix
arr = np.matrix([[6,9,15], 
                 [24,35,26], 
                 [19,8,27], 
                 [41,53,15]])

# main diagonal
arr2 = np.diag(arr)
print(arr2)

# Output
# [ 6 35 27]

# above main diagonal
arr2 = np.diag(arr, 1)
print(arr2)

# Output
# [ 9 26]

# below main diagonal
arr2 = np.diag(arr, -1)
print(arr2)

# Output
# [24  8 15]

5. Use Construct Diagonal From Python NumPy Array

By using numpy.diag() function we can also create a matrix with diagonal values.


import numpy as np

arr = np.array([3, 7, 12, 18])

# construct diagonal from numpy array
arr2 = np.diag(arr)
print(arr2)

# OutPut
# [[ 3  0  0  0]
#  [ 0  7  0  0]
#  [ 0  0 12  0]
#  [ 0  0  0 18]]

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


# use diag to numpy row vector
arr = np.array([[3, 7, 12, 18]])
arr2 = np.diag(arr[0])
print(arr2)

Yields the same output as above.

6. Conclusion

In this article, I have explained how to use numpy.diag() function and using how to extract a diagonal or construct a diagonal array from the given array with examples.

Happy Learning!!

You May Also Like

References

Leave a Reply

You are currently viewing How to get Diagonal of NumPy Array Using diag()