AppDividend
Latest Code Tutorials

# Python NumPy diagflat() Function Example

Python NumPy diagflat() is an inbuilt function that is declared under the library numpy. The diagflat() function creates a two-dimensional array which consists of inputs flattened in the form of diagonal. It takes the input and put them in the main diagonal of a zero array. We can also set the input either above the main diagonal or below it.

## Python NumPy diagflat()

Python numpy.diagflat (a, k = 0) function creates a two-dimensional array with the array_like input as a diagonal to the new output array.

### Parameters:

v: (array_like)

Input data, which is flattened and set as the k-th diagonal of the output.

k:(int,optional)

Diagonal is by default set to 0, corresponds to the “main diagonal”, the value of k (either positive or negative) gives the number of the diagonal above or below respectively of the main.

### Return value

It gives the output in the form of the 2-D array. The array consists of input in the main diagonal, and the rest of all elements are zero.

### Example programs on diagflat() function in Python:

```import numpy as np

print("diagflat use on main diagonal: \n",np.diagflat([1,2]),"\n")
print("diagflat use on main diagonal: \n",np.diagflat([1,7,6]),"\n")
print("diagflat use on main diagonal: \n",np.diagflat([11,17,16,12]),"\n")
print("diagflat use on main diagonal: \n",np.diagflat([[21,27],[22,28]]),"\n")
print("diagflat use on main diagonal: \n",np.diagflat([[31,37,36],[33,34,35]]),"\n")
```

#### Output

```Value for parameter [1,2]
diagflat use on main diagonal:  array([[1, 0],
[0, 2]])

Value for parameter [1,7,6]
diagflat use on main diagonal:  array([[1, 0, 0],
[0, 7, 0],
[0, 0, 6]])

Value for parameter [11,17,16,12]
diagflat use on main diagonal:  array([[11,  0,  0,  0],
[ 0, 17,  0,  0],
[ 0,  0, 16,  0],
[ 0,  0,  0, 12]])

Value for parameter [[21,27],[22,28]]
diagflat use on main diagonal:  array([[21,  0,  0,  0],
[ 0, 27,  0,  0],
[ 0,  0, 22,  0],
[ 0,  0,  0, 28]])
Value for parameter [[31,37,36],[33,34,35]]
diagflat use on main diagonal:  array([[31,  0,  0,  0,  0,  0],
[ 0, 37,  0,  0,  0,  0],
[ 0,  0, 36,  0,  0,  0],
[ 0,  0,  0, 33,  0,  0],
[ 0,  0,  0,  0, 34,  0],
[ 0,  0,  0,  0,  0, 35]])```

Here we are converting the array input into the 2-D array form with the array elements present at the main diagonal of the 2-D array.

We can see that the 2-D array formed is of size NxN, and the non-diagonal elements are filled with zero’s. Here we have given input of array and array of lists elements.

#### Example 2: Write a program to show the working of the diagflat() method in Python and printing the input above and below the main diagonal.

```import numpy as np

print("diagflat above main diagonal:\n",np.diagflat([1,7,6],1),"\n")
print("diagflat below main diagonal:\n",np.diagflat([1,7,6],-1),"\n")
print("diagflat above main diagonal:\n",np.diagflat([1,7,6],2),"\n")
print("diagflat below main diagonal:\n",np.diagflat([1,7,6],-2),"\n")
```

#### Output

```Value for parameter diagflat([1,7,6], 1)
diagflat above main diagonal:  array([[0, 1, 0, 0],
[0, 0, 7, 0],
[0, 0, 0, 6],
[0, 0, 0, 0]])

Value for parameter diagflat([1,7,6], -1)
diagflat below main diagonal:  array([[0, 0, 0, 0],
[1, 0, 0, 0],
[0, 7, 0, 0],
[0, 0, 6, 0]])

Value for parameter diagflat([1,7,6], 2)
diagflat above main diagonal:  array([[0, 0, 1, 0, 0],
[0, 0, 0, 7, 0],
[0, 0, 0, 0, 6],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
Value for parameter diagflat([1,7,6], -2)
diagflat below main diagonal:  array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 0, 0, 0, 0],
[0, 7, 0, 0, 0],
[0, 0, 6, 0, 0]])```

#### Output

Here we are converting the array input into the 2-D array form with the array elements present above and below the main diagonal of the 2-D array. We can see that the 2-D array formed is, and the array elements are inserted either above the main diagonal or below it depending on the input.

The negative number after the array input indicates below the main diagonal, and the positive number indicates above the main diagonal. Here we have given input of array and array of lists elements.

## Conclusion

NumPy diagflat() function in Python creates a two-dimensional array with the flattened input as a diagonal.