Change language

numpy.diag_indices () in Python

| |

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:

# Python program illustrating
# work diag_indices ()

 

import numpy as geek 

 
# Creates a 5 X 5 array and returns indices
# main diagonal elements

d = geek.diag_indices ( 5 )

print ( "Indices of diagnol elements as tuple:" )

print (d, "" )

 

array = geek.arange ( 16 ). reshape ( 4 , 4 )

print ( "Initial array:" , array)

 
# Here we can manipulate diagonal elements
# by accessing the diagonal elements

d = geek.diag_indices ( 4 )

array [d] = 25

print ( "New array:" , array)

Output:

 Indices of diagnol 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: 2D array manipulation

# Python program illustrating
# diag_indices () work

 

import numpy as geek 

 
# Control 2D array development

d = geek .diag_indices ( 3 , 2 )

 

array = geek.arange ( 12 ) .reshape ( 4 , 3 )

 

array [d] = 111

print ( "Manipulated array:" , array)

Output:

 Manipulated array: [[ 111 1 2] [3 111 5] [6 7 111] [9 10 11]] 

Code 3: Manipulating a 3D Array

# Python program illustrating
# diag_indices () work

  

import numpy as geek 

 
# Setting diagonal indices

d = geek.diag_indices ( 1 , 2 )

print ( "Diag indices:" , d)

  
# Create a 3D array with all

array = geek.ones (( 2 , 2 , 2 ), dtype = geek. int )

print ( "Initial array:" , array)

 
# 3D array management

array [d] = 0

print ( "New array:" , 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]]] 

Links:
https://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.diag_indices.html

Notes:
These codes will not work for online IDs. Please run them on your systems to see how they work.

This article is courtesy of Mohit Gupta_OMG

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically