Open In App

numpy.diag_indices() in Python

Last Updated : 08 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
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.

 



Similar Reads

Python | Numpy numpy.resize()
With the help of Numpy numpy.resize(), we can resize the size of an array. Array can be of any shape but to resize it we just need the size i.e (2, 2), (2, 3) and many more. During resizing numpy append zeros if values at a particular place is missing. Parameters: new_shape : [tuple of ints, or n ints] Shape of resized array refcheck : [bool, optio
2 min read
Python | Numpy numpy.transpose()
With the help of Numpy numpy.transpose(), We can perform the simple function of transpose within one line by using numpy.transpose() method of Numpy. It can transpose the 2-D arrays on the other hand it has no effect on 1-D arrays. This method transpose the 2-D numpy array. Parameters: axes : [None, tuple of ints, or n ints] If anyone wants to pass
2 min read
Python | Numpy numpy.ndarray.__lt__()
With the help of numpy.ndarray.__lt__() method of Numpy, We can find that which element in an array is less than the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__lt__($self, value, /) Return: self<value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__gt__()
With the help of numpy.ndarray.__gt__() method of Numpy, We can find that which element in an array is greater then the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__gt__($self, value, /) Return: self>value Example #1 : In this example we can see th
1 min read
Python | Numpy numpy.ndarray.__le__()
With the help of numpy.ndarray.__le__() method of Numpy, We can find that which element in an array is less than or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__le__($self, value, /) Return: self<=value Example #1 : In this example we
1 min read
Python | Numpy numpy.ndarray.__ge__()
With the help of numpy.ndarray.__ge__() method of Numpy, We can find that which element in an array is greater then or equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ge__($self, value, /) Return: self>=value Example #1 : In this example
1 min read
Python | Numpy numpy.ndarray.__ne__()
With the help of numpy.ndarray.__ne__() method of Numpy, We can find that which element in an array is not equal to the value which is provided in the parameter. It will return you numpy array with boolean type having only values True and False. Syntax: ndarray.__ne__($self, value, /) Return: self!=value Example #1 : In this example we can see that
1 min read
Python | Numpy numpy.ndarray.__neg__()
With the help of numpy.ndarray.__neg__() method of Numpy, one can multiply each and every element of an array with -1. Hence, the resultant array having values like positive values becomes negative and negative values become positive. Syntax: ndarray.__neg__($self, /) Return: -self Example #1 : In this example we can see that after applying numpy._
1 min read
Python | Numpy numpy.ndarray.__pos__()
With the help of numpy.ndarray.__pos__() method of Numpy, one can multiply each and every element of an array with 1. Hence, the resultant array having values same as original array. Syntax: ndarray.__pos__($self, /) Return: +self Example #1 : In this example we can see that after applying numpy.__pos__(), we get the simple array that can be the sa
1 min read
Python | Numpy numpy.ndarray.__truediv__()
With the help of Numpy numpy.ndarray.__truediv__(), we can divide a particular value that is provided as a parameter in the ndarray.__truediv__() method. Value will be divided to each and every element in a numpy array. Syntax: ndarray.__truediv__($self, value, /) Return: self/value Example #1 : In this example, we can see that each element in an a
1 min read
Practice Tags :