# Numpy diag() | How to Use np.diag Function in Python

In Python, we have seen many functions or methods of the numpy library. This tutorial will teach us about the numpy diag() function. As we come in a situation where we need to calculate the diagonal, print the diagonal of an array of matrices. We will see all how we can print the values of the diagonal through the numpy diag() function.

Contents

## What is the Numpy Diag() function?

The diag() function extracts and constructs a diagonal 2-D array with a numpy library. It contains two parameters: an input array and k, which decides the diagonal, i.e., main diagonal, lower diagonal, or upper diagonal. The numpy library function is used to perform the mathematical and statistics operation on the multidimensional array.

## Syntax

``numpy.diag(arr,k=0) ``

## Parameters

• arr: It is an input array. If it is a 2-D array, it returns a copy of its k-th diagonal. If it is a 1-D array, it returns a 2-D array with arr on the kth diagonal.
• k: It is an integer value and an optional input. If k>0, then the diagonal is above the main diagonal, and if k<0, then the diagonal is below the main diagonal. By default, it is 0.

## Return value

It returns the extracted diagonal or a constructed diagonal.

## Examples of numpy diag() function

Let us understand the numpy diag() function of the numpy module in detail with the help of examples:

### 1. Finding diagonal without k parameter

In this example, we will create a multidimensional array(matrix) with the help of numpy and then apply the numpy diag() function without passing the k parameter. By default, it is set to 0 and also called the main diagonal. Let us look at the example for understanding the concept in detail.

```#import numpy library
import numpy as np

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

print("Main diagonal : ",np.diag(a))
```

Output:

``Main diagonal :  [1 5 7]``

Explanation:

• Firstly, we will import the numpy library with an alias named np.
• Then, we will take the input to create a multidimensional array.
• After that, we will apply the numpy diag() without the value of the k parameter. By default, it is set to 0.
• At last, we have printed the main diagonal of the multidimensional array.
• Hence, you can see the output.

### 2. Printing upper diagonal of main diagonal

In this example, we will be printing the upper diagonal of the main diagonal in the multidimensional array. In this, we will pass the k parameter with the positive value of k to give us the upper diagonal. By applying the numpy diag() function, we will print the upper diagonal. Let us look at the example for understanding the concept in detail.

```#import numpy library
import numpy as np

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

print(a)
print("Main diagonal : ",np.diag(a,1))
```

Output:

``````[[1 2 3]
[4 5 6]
[9 8 7]]
Main diagonal :  [2 6]``````

Explanation:

• Firstly, we will be importing the numpy library with an alias name as np.
• Then, we will take the input for creating a multidimensional array.
• After that, we will apply the numpy diag() with the k parameter value equal to 1.
• For the upper diagonal, we need to put the k value as positive and greater than 0.
• At last, we have printed the upper diagonal of the main diagonal of the multidimensional array.
• Hence, you can see the output.

### 3. Printing lower diagonal of main diagonal

In this example, we will be printing the lower diagonal of the main diagonal in the multidimensional array. In this, we will pass the k parameter with the negative value of k to give us the lower diagonal. By applying the numpy diag() function, we will print the lower diagonal. Let us look at the example for understanding the concept in detail.

```#import numpy library
import numpy as np

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

print(a)
print("Main diagonal : ",np.diag(a,-1))
```

Output:

``````[[1 2 3]
[4 5 6]
[9 8 7]]
Main diagonal :  [4 8]``````

Explanation:

• Firstly, we will be importing the numpy library with an alias name as np.
• Then, we will take the input for creating a multidimensional array.
• After that, we will apply the numpy diag() with the k parameter value equal to 1.
• For the lower diagonal, we need to put the k value as negative and lesser than 0.
• At last, we have printed the lower diagonal of the main diagonal of the multidimensional array.
• Hence, you can see the output.

### 4. Using arange function to create an array and then construct the diagonal

In this example, we will be importing the numpy library. Then, we will use arange() from numpy to create the multidimensional array. After that, we will print the main diagonal of the array created. Let us look at the example for understanding the concept in detail.

```#import numpy library
import numpy as np

x = np.arange(9).reshape((3,3))

print(x)
print("Main diagonal : ",np.diag(x))
```

Output:

``````[[0 1 2]
[3 4 5]
[6 7 8]]
Main diagonal :  [0 4 8]``````

Explanation:

• Firstly, we will be importing the numpy library with an alias name as np.
• Then, we will apply the np.arange() function from the numpy library to create a multidimensional array.
• After that, we have printed the matrix formed.
• At last, we have applied the diag() function and printed the diagonal of the array.
• Hence, you can see the output.

### 5. Constructing diagonal from array

In this example, we will be importing the numpy library. Then, we will be passing the array values in the numpy array() function. Finally, we will apply the numpy diag function to create the diagonal of the values passed inside the array. This will construct a diagonal array. Let us look at the example for understanding the concept in detail.

```#importing numpy library
import numpy as np

a = np.array([5, 6, 7, 8])

print(a)
print("Diagonal : ",np.diag(a))
```

Output:

``````[5 6 7 8]
Diagonal :
[[5 0 0 0]
[0 6 0 0]
[0 0 7 0]
[0 0 0 8]]``````

Explanation:

• Firstly, we will be importing the numpy library with an alias name as np.
• Then, we have created an array with the help of the numpy library.
• After that, we have printed the array, which we have taken as an input.
• Finally, we have applied the numpy diag() function for constructing the diagonal for the array values and print the output.
• Hence, you can see the output.

## What is numpy diagonal() of a 3-D array?

The numpy diagonal() function is used to extract and construct a diagonal of a 2-d and 3-d array with a numpy library.

Let us take an example and understand the concept in detail.

```#import numpy library
import numpy as np

a = np.arange(8).reshape(2,2,2);
print(a)
print("\n")
print(" diagonal output : ",a.diagonal(0,0,1))
```

Output:

``````[[[0 1]
[2 3]]

[[4 5]
[6 7]]]

diagonal output :  [[0 6]
[1 7]]``````

Explanation:

• Firstly, we will be importing the numpy library with an alias name as np.
• Then, we will apply the np.arange() function from the numpy library to create a 3-d multidimensional array.
• Then, we will print the 3-d array.
• After that, we will apply the diagonal() function with all the parameters and print the output.
• Hence, you can see the output.

## Difference between numpy diag() and numpy diagonal()?

### Numpy diag()

The diag() function is used to extract and construct a diagonal 2-D array with a numpy library.

### Numpy diagonal()

The diagonal() function is used to extract and construct a diagonal of a 2-d and 3-d array with a numpy library.

### Example of numpy diag() and numpy diagonal()

In this example, we will explain the examples of the numpy diag() function operating on the 2-d array and numpy diagonal() function operating on the 2-d and 3-d array both. We will understand the difference between in a more efficient way through this example.

```#import numpy library
import numpy as np

x = np.arange(9).reshape((3,3))

print(x)
print("\n")
print("2-d Main diagonal : ",np.diag(x))
print("\n")

#numpy diagonal() example
import numpy as np

#2-d array
x = np.arange(9).reshape((3,3))
print("2-d Main diagonal : ",np.diagonal(x))

#3-d array
a = np.arange(8).reshape(2,2,2);
print(a)
print("\n")
print(" 3-d diagonal output : ",a.diagonal(0,0,1))
```

Output:

``````[[0 1 2]
[3 4 5]
[6 7 8]]

2-d Main diagonal :  [0 4 8]

2-d Main diagonal :  [0 4 8]
[[[0 1]
[2 3]]

[[4 5]
[6 7]]]

3-d diagonal output :  [[0 6]
[1 7]]``````

Explanation:

• Firstly, we will import the numpy library with an alias name as np.
• Then, we will apply the np.arange() function from the numpy library to create a 2-D multidimensional array.
• After that, we have printed the matrix formed.
• At last, we applied the diag() function and printed the diagonal of the array.
• Hence, you can see the output.

## Conclusion

In this tutorial, we learned about the numpy diagonal() function concept. We have seen how to print the multidimensional array’s diagonal and construct a multidimensional diagonal. We have all the ways through which we can do so, and all the examples are explained in detail for a better understanding of the concept. You can use any of the methods according to your needs in the program.

However, please let me know in the comment section below if you have any questions. I will try to help you as soon as possible.

Subscribe
Notify of