numpy.diag() in Python Last Updated : 09 Mar, 2022 Comments Improve Suggest changes Like Article Like Report numpy.diag(a, k=0) : Extracts and construct a diagonal array Parameters : a : array_like k : [int, optional, 0 by default] Diagonal we require; k>0 means diagonal above main diagonal or vice versa. Returns : ndarray Python # Python Programming illustrating # numpy.diag method import numpy as geek # matrix creation by array input a = geek.matrix([[1, 21, 30], [63 ,434, 3], [54, 54, 56]]) print("Main Diagonal elements : \n", geek.diag(a), "\n") print("Diagonal above main diagonal : \n", geek.diag(a, 1), "\n") print("Diagonal below main diagonal : \n", geek.diag(a, -1)) Output : Main Diagonal elements : [ 1 434 56] Diagonal above main diagonal : [21 3] Diagonal below main diagonal : [63 54] References : https://docs.scipy.org/doc/numpy/reference/generated/numpy.diagflat.html#numpy.diagflat Note : These NumPy-Python programs won't run on online IDE's, so run them on your systems to explore them . Comment More infoAdvertise with us Next Article numpy.diag() in Python M Mohit Gupta_OMG Improve Article Tags : Python Python-numpy Python numpy-arrayCreation Practice Tags : python Similar Reads numpy.diagflat() in Python numpy.diagflat (a, k = 0): Create a two-dimensional array with the array_like input as a diagonal to the new output array. Parameters : a : array_like input data with diagonal elements strong>k : [int, optional, 0 by default] Diagonal we require; k>0 means diagonal above main diagonal or vice 1 min read numpy.diag_indices() in Python The numpy.diag_indices() function returns indices in order to access the elements of main diagonal of a array with minimum dimension = 2. Returns indices in the form of tuple. to access the main diagonal of an array. Syntax: numpy.diag_indices(n, n_dim = 2) Parameters : n : size of array, for whic 2 min read NumPy Array in Python NumPy (Numerical Python) is a powerful library for numerical computations in Python. It is commonly referred to multidimensional container that holds the same data type. It is the core data structure of the NumPy library and is optimized for numerical and scientific computation in Python. Table of C 2 min read numpy.eye() in Python numpy.eye() is a function in the NumPy library that creates a 2D array with ones on the diagonal and zeros elsewhere. This function is often used to generate identity matrices with ones along the diagonal and zeros in all other positions.Let's understand with the help of an example:Pythonimport nump 2 min read numpy.identity() in Python numpy.identity() function is used to create an identity matrix which is used to make identity matrix. This is commonly used in linear algebra and numerical computations. It has the following properties:Diagonal elements are all 1s.Non-diagonal elements are all 0s.Syntax: numpy.identity(n, dtype=None 1 min read Like