Python Numpy diag_indices() is an inbuilt NumPy function that returns the indices of the main diagonal in the form of tuples. These indices are further used for accessing the main diagonal of an array with minimum dimension 2.

Python Numpy diag_indices()

The diag_indices() method returns the indices to access the main diagonal of the array. That returns a  tuple of indices that can be used to access a main diagonal of an array a with the a.ndim >= 2 dimensions and shape (n, n, …, n).

For a.ndim = 2 this is a usual diagonal and for a.ndim > 2 this is the set of indices to access a[i, i, …, i] for i = [0…n-1].


numpy.diag_indices(n, ndim=2)

#python #numpy

Python Numpy diag_indices() Function Example
1.40 GEEK