numpy.diag_indices() in Python Last Updated : 08 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 which indices of diag elements are required along each dimension n_dim : [int, optional]number of dimensions. Return : Indices(as tuples) to access diagonal elements. Code 1 : Python3 # Python Program illustrating # working of diag_indices() import numpy as geek # Creates a 5 X 5 array and returns indices of # main diagonal elements d = geek.diag_indices(5) print("Indices of diagonal elements as tuple : ") print(d, "\n") array = geek.arange(16).reshape(4,4) print("Initial array : \n", array) # Here we can manipulate diagonal elements # by accessing the diagonal elements d = geek.diag_indices(4) array[d] = 25 print("\n New array : \n", array) Output : Indices of diagonal elements as tuple : (array([0, 1, 2, 3, 4]), array([0, 1, 2, 3, 4])) Initial array : [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] New array : [[25 1 2 3] [ 4 25 6 7] [ 8 9 25 11] [12 13 14 25]] Code 2 : Manipulating 2D array Python # Python Program illustrating # working of diag_indices() import numpy as geek # Manipulating a 2D array d = geek.diag_indices(3, 2) array = geek.arange(12).reshape(4, 3) array[d] = 111 print("Manipulated array : \n", array) Output : Manipulated array : [[111 1 2] [ 3 111 5] [ 6 7 111] [ 9 10 11]] Code 3 : Manipulating 3D array Python # Python Program illustrating # working of diag_indices() import numpy as geek # Setting diagonal indices d = geek.diag_indices(1, 2) print("Diag indices : \n", d) # Creating a 3D array with all ones array = geek.ones((2, 2, 2), dtype=geek.int) print("Initial array : \n", array) # Manipulating a 3D array array[d] = 0 print("New array : \n", array) Output : Diag indices : (array([0]), array([0])) Initial array : [[[1 1] [1 1]] [[1 1] [1 1]]] New array : [[[0 0] [1 1]] [[1 1] [1 1]]] Note : These codes won't run on online IDE's. So please, run them on your systems to explore the working. Comment More infoAdvertise with us Next Article numpy.diag_indices() in Python M Mohit Gupta_OMG Improve Article Tags : Python Python-numpy Python numpy-Indexing Practice Tags : python Similar Reads numpy.diag() in Python 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 1 min read 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.alen() in Python numpy.alen() function is used to return the length of the first dimension of the input array. Syntax : numpy.alen(arr) Parameters : arr : [array_like] Input array. Return : [int]Length of the first dimension of arr. Code #1 : Python3 # Python program explaining # alen() function import numpy as geek 1 min read numpy.tril_indices() function | Python numpy.tril_indices() function return the indices for the lower-triangle of an (n, m) array. Syntax : numpy.tril_indices(n, k = 0, m = None) Parameters : n : [int] The row dimension of the arrays for which the returned indices will be valid. k : [int, optional] Diagonal offset. m : [int, optional] Th 1 min read numpy.mask_indices() function | Python numpy.mask_indices() function return the indices to access (n, n) arrays, given a masking function. Syntax : numpy.mask_indices(n, mask_func, k = 0) Parameters : n : [int] The returned indices will be valid to access arrays of shape (n, n). mask_func : [callable] A function whose call signature is s 1 min read numpy.linalg.eig() Method in Python In NumPy we can compute the eigenvalues and right eigenvectors of a given square array with the help of numpy.linalg.eig(). It will take a square array as a parameter and it will return two values first one is eigenvalues of the array and second is the right eigenvectors of a given square array. Syn 1 min read Python | Numpy np.triu_indices With the help of np.triu_indices() method, we can get the indices for the upper triangle of an [n, m] array by using np.triu_indices() method. Syntax : np.triu_indices(n, m) Return : Return the indices for the upper triangle. Example #1 : In this example we can see that by using np.triu_indices() me 1 min read numpy.nonzero() in Python numpy.nonzero()function is used to Compute the indices of the elements that are non-zero. It returns a tuple of arrays, one for each dimension of arr, containing the indices of the non-zero elements in that dimension. The corresponding non-zero values in the array can be obtained with arr[nonzero(ar 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.asmatrix() in Python numpy.asmatrix(data, dtype = None) Returns a matrix by interpreting the input as a matrix. Parameters : data : array-like input data dtype : Data type of returned array Returns : Interprets the input as a matrix Python # Python Programming illustrating # numpy.asmatrix import numpy as geek # array-l 1 min read Like