numpy.diag () in Python

numpy.diag (a, k = 0): extracts and builds a diagonal array
Parameters :

  a :  array_like  k:  [int, optional, 0 by default] Diagonal we require; k & gt; 0 means diagonal above main diagonal or vice versa. 

Returns :

 ndarray 

# Python Programming Illustrative
# numpy.diag method

 

import numpy as geek

 
# create a matrix by entering an array

a = geek.matrix ([[ 1 , 21 , 30 ], 

[ 63 , 434 , 3 ], 

  [ 54 , 54 , 56 ]])

 

print ( "Main Diagnol elements:" , geek.diag (a), "" )

 

print ( "Diagnol above main diagnol:" , geek.diag (a, 1 ), "" )

 

print ( "Diagnol below main diagnol:" , geek.diag (a, - 1 ))

Output:

 Main Diagnol elements: [1 434 56] Diagnosis above main diagnol: [21 3] Diagnosis below main diagnol: [63 54] 

Links:
https://docs.scipy.org/doc/numpy/reference/generated/numpy. diagflat.html # numpy.diagflat
Notes:
These NumPy-Python programs will not run by onlineID, so run them on your systems to learn them
,
This article is provided by Mohit Gupta_OMG