How to get Diagonal of NumPy Array Using diag()

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:January 17, 2023
Spread the love

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 the 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!!

References

Malli

I am Mallikarjuna an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly documentation. Over the years, I have written hundreds of articles in Pandas, NumPy, Python, and I take pride in my ability to bridge the gap between technical experts and end-users by delivering well-structured, accessible, and informative content.

Leave a Reply

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