{"id":10403,"date":"2021-04-06T08:50:25","date_gmt":"2021-04-06T03:20:25","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=10403"},"modified":"2024-01-03T13:26:11","modified_gmt":"2024-01-03T07:56:11","slug":"numpy-diag","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/numpy-diag\/","title":{"rendered":"Numpy diag() | How to Use np.diag Function in Python"},"content":{"rendered":"\n<p>In Python, we have seen many functions or methods of the numpy library. This tutorial will teach us about the numpy diag() function. As we come in a situation where we need to calculate the diagonal, print the diagonal of an array of matrices. We will see all how we can print the values of the diagonal through the numpy diag() function.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#What_is_the_Numpy_Diag_function\" >What is the Numpy Diag() function?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#Syntax\" >Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#Parameters\" >Parameters<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#Return_value\" >Return value<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#Examples_of_numpy_diag_function\" >Examples of numpy diag() function<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#1_Finding_diagonal_without_k_parameter\" >1. Finding diagonal without k parameter<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#2_Printing_upper_diagonal_of_main_diagonal\" >2. Printing upper diagonal of main diagonal<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#3_Printing_lower_diagonal_of_main_diagonal\" >3. Printing lower diagonal of main diagonal<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#4_Using_arange_function_to_create_an_array_and_then_construct_the_diagonal\" >4. Using arange function to create an array and then construct the diagonal<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#5_Constructing_diagonal_from_array\" >5. Constructing diagonal from array<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#What_is_numpy_diagonal_of_a_3-D_array\" >What is numpy diagonal() of a 3-D array?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#Difference_between_numpy_diag_and_numpy_diagonal\" >Difference between numpy diag() and numpy diagonal()?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#Numpy_diag\" >Numpy diag()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#Numpy_diagonal\" >Numpy diagonal()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#Example_of_numpy_diag_and_numpy_diagonal\" >Example of numpy diag() and numpy diagonal()<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-the-numpy-diag-function\"><span class=\"ez-toc-section\" id=\"What_is_the_Numpy_Diag_function\"><\/span>What is the Numpy Diag() function?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>The diag() function extracts and constructs a diagonal 2-D array with a numpy library.<\/strong> It contains two parameters: an input array and k, which decides the diagonal, i.e., main diagonal, lower diagonal, or upper diagonal. The numpy library function is used to perform the mathematical and statistics operation on the multidimensional array.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-syntax\"><span class=\"ez-toc-section\" id=\"Syntax\"><\/span>Syntax<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>numpy.diag(arr,k=0) <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-parameters\"><span class=\"ez-toc-section\" id=\"Parameters\"><\/span>Parameters<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>arr<\/strong>: It is an input array. If&nbsp;it&nbsp;is a 2-D array, it returns a copy of its&nbsp;<em>k<\/em>-th diagonal. If&nbsp;it&nbsp;is a 1-D array, it returns a 2-D array with&nbsp;arr&nbsp;on the&nbsp;kth diagonal.<\/li>\n\n\n\n<li><strong>k:<\/strong> It is an integer value and an optional input. If k&gt;0, then the diagonal is above the main diagonal, and if k&lt;0, then the diagonal is below the main diagonal. By default, it is 0.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-return-value\"><span class=\"ez-toc-section\" id=\"Return_value\"><\/span>Return value<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>It returns the extracted diagonal or a constructed diagonal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-examples-of-numpy-diag-function\"><span class=\"ez-toc-section\" id=\"Examples_of_numpy_diag_function\"><\/span>Examples of numpy diag() function<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Let us understand the <strong>numpy diag() function<\/strong> of the numpy module in detail with the help of examples:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-finding-diagonal-without-k-parameter\"><span class=\"ez-toc-section\" id=\"1_Finding_diagonal_without_k_parameter\"><\/span>1. Finding diagonal without k parameter<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will create a multidimensional array(<a href=\"http:\/\/www.pythonpool.com\/numpy-identity-matrix\/\" target=\"_blank\" rel=\"noreferrer noopener\">matrix<\/a>) with the help of numpy and then apply the numpy diag() function without passing the k parameter. By default, it is set to 0 and also called the main diagonal. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import numpy library\nimport numpy as np\n\na = np.matrix(&#x5B;&#x5B;1, 2, 3], &#x5B;4, 5, 6], &#x5B;9, 8, 7]])\n\nprint(&quot;Main diagonal : &quot;,np.diag(a))\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Main diagonal :  &#91;1 5 7]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Firstly, we will import the numpy library with an alias named np.<\/li>\n\n\n\n<li>Then, we will take the input to create a multidimensional array.<\/li>\n\n\n\n<li>After that, we will apply the numpy diag() without the value of the k parameter. By default, it is set to 0.<\/li>\n\n\n\n<li>At last, we have printed the main diagonal of the multidimensional array.<\/li>\n\n\n\n<li>Hence, you can see the output.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-printing-upper-diagonal-of-main-diagonal\"><span class=\"ez-toc-section\" id=\"2_Printing_upper_diagonal_of_main_diagonal\"><\/span>2. Printing upper diagonal of main diagonal<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will be printing the upper diagonal of the main diagonal in the multidimensional array. In this, we will pass the k parameter with the positive value of k to give us the upper diagonal. By applying the numpy diag() function, we will print the upper diagonal. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import numpy library\nimport numpy as np\n\na = np.matrix(&#x5B;&#x5B;1, 2, 3], &#x5B;4, 5, 6], &#x5B;9, 8, 7]])\n\nprint(a)\nprint(&quot;Main diagonal : &quot;,np.diag(a,1))\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;&#91;1 2 3]\n &#91;4 5 6]\n &#91;9 8 7]]\nMain diagonal :  &#91;2 6]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Firstly, we will be importing the numpy library with an alias name as np.<\/li>\n\n\n\n<li>Then, we will take the input for creating a multidimensional array.<\/li>\n\n\n\n<li>After that, we will apply the numpy diag() with the k parameter value equal to 1.<\/li>\n\n\n\n<li>For the upper diagonal, we need to put the k value as positive and greater than 0.<\/li>\n\n\n\n<li>At last, we have printed the upper diagonal of the main diagonal of the multidimensional array.<\/li>\n\n\n\n<li>Hence, you can see the output.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-printing-lower-diagonal-of-main-diagonal\"><span class=\"ez-toc-section\" id=\"3_Printing_lower_diagonal_of_main_diagonal\"><\/span>3. Printing lower diagonal of main diagonal<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will be printing the lower diagonal of the main diagonal in the multidimensional array. In this, we will pass the k parameter with the negative value of k to give us the lower diagonal. By applying the numpy diag() function, we will print the lower diagonal. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import numpy library\nimport numpy as np\n\na = np.matrix(&#x5B;&#x5B;1, 2, 3], &#x5B;4, 5, 6], &#x5B;9, 8, 7]])\n\nprint(a)\nprint(&quot;Main diagonal : &quot;,np.diag(a,-1))\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;&#91;1 2 3]\n &#91;4 5 6]\n &#91;9 8 7]]\nMain diagonal :  &#91;4 8]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-f5d97bbb-0a34-44f9-be84-251714e1be9f\">\n<li>Firstly, we will be importing the numpy library with an alias name as np.<\/li>\n\n\n\n<li>Then, we will take the input for creating a multidimensional array.<\/li>\n\n\n\n<li>After that, we will apply the numpy diag() with the k parameter value equal to 1.<\/li>\n\n\n\n<li>For the lower diagonal, we need to put the k value as negative and lesser than 0.<\/li>\n\n\n\n<li>At last, we have printed the lower diagonal of the main diagonal of the<a href=\"https:\/\/en.wikipedia.org\/wiki\/Array_data_structure#Multidimensional_arrays\" target=\"_blank\" rel=\"noreferrer noopener\"> multidimensional array<\/a>.<\/li>\n\n\n\n<li>Hence, you can see the output.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-using-arange-function-to-create-an-array-and-then-construct-the-diagonal\"><span class=\"ez-toc-section\" id=\"4_Using_arange_function_to_create_an_array_and_then_construct_the_diagonal\"><\/span>4. Using arange function to create an array and then construct the diagonal<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will be importing the numpy library. Then, we will use arange() from numpy to create the multidimensional array. After that, we will print the main diagonal of the array created. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import numpy library\nimport numpy as np\n\nx = np.arange(9).reshape((3,3))\n\nprint(x)\nprint(&quot;Main diagonal : &quot;,np.diag(x))\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;&#91;0 1 2]\n &#91;3 4 5]\n &#91;6 7 8]]\nMain diagonal :  &#91;0 4 8]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Firstly, we will be importing the numpy library with an alias name as np.<\/li>\n\n\n\n<li>Then, we will apply the<strong> np.arange()<\/strong> function from the numpy library to create a multidimensional array.<\/li>\n\n\n\n<li>After that, we have printed the matrix formed.<\/li>\n\n\n\n<li>At last, we have applied the diag() function and printed the diagonal of the array.<\/li>\n\n\n\n<li>Hence, you can see the output. <\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-constructing-diagonal-from-array\"><span class=\"ez-toc-section\" id=\"5_Constructing_diagonal_from_array\"><\/span>5. Constructing diagonal from array<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will be importing the numpy library. Then, we will be passing the array values in the numpy array() function. Finally, we will apply the numpy diag function to create the diagonal of the values passed inside the array. This will construct a diagonal array. Let us look at the example for understanding the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#importing numpy library\nimport numpy as np\n\na = np.array(&#x5B;5, 6, 7, 8])\n\nprint(a)\nprint(&quot;Diagonal : &quot;,np.diag(a))\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;5 6 7 8]\nDiagonal : \n  &#91;&#91;5 0 0 0]\n &#91;0 6 0 0]\n &#91;0 0 7 0]\n &#91;0 0 0 8]]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Firstly, we will be importing the numpy library with an alias name as np.<\/li>\n\n\n\n<li>Then, we have created an array with the help of the numpy library.<\/li>\n\n\n\n<li>After that, we have printed the array, which we have taken as an input.<\/li>\n\n\n\n<li>Finally, we have applied the numpy diag() function for constructing the diagonal for the array values and print the output.<\/li>\n\n\n\n<li>Hence, you can see the output.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-numpy-diagonal-of-a-3-d-array\"><span class=\"ez-toc-section\" id=\"What_is_numpy_diagonal_of_a_3-D_array\"><\/span>What is numpy diagonal() of a 3-D array?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>The numpy diagonal() function is used to extract and construct a diagonal of a 2-d and 3-d array with a numpy library.<\/strong><\/p>\n\n\n\n<p>Let us take an example and understand the concept in detail.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import numpy library\nimport numpy as np\n\na = np.arange(8).reshape(2,2,2);\nprint(a)\nprint(&quot;\\n&quot;)\nprint(&quot; diagonal output : &quot;,a.diagonal(0,0,1))\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;&#91;&#91;0 1]\n  &#91;2 3]]\n\n &#91;&#91;4 5]\n  &#91;6 7]]]\n\n\ndiagonal output :  &#91;&#91;0 6]\n &#91;1 7]]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Firstly, we will be importing the numpy library with an alias name as np.<\/li>\n\n\n\n<li>Then, we will apply the np.arange() function from the numpy library to create a 3-d multidimensional array.<\/li>\n\n\n\n<li>Then, we will print the 3-d array.<\/li>\n\n\n\n<li>After that, we will apply the diagonal() function with all the parameters and print the output.<\/li>\n\n\n\n<li>Hence, you can see the output.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-difference-between-numpy-diag-and-numpy-diagonal\"><span class=\"ez-toc-section\" id=\"Difference_between_numpy_diag_and_numpy_diagonal\"><\/span>Difference between numpy diag() and numpy diagonal()?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-numpy-diag\"><span class=\"ez-toc-section\" id=\"Numpy_diag\"><\/span>Numpy diag()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>The diag() function is used to extract and construct a diagonal 2-D array with a numpy library.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-numpy-diagonal\"><span class=\"ez-toc-section\" id=\"Numpy_diagonal\"><\/span>Numpy diagonal()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>The diagonal() function is used to extract and construct a diagonal of a 2-d and 3-d array with a numpy library.<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-of-numpy-diag-and-numpy-diagonal\"><span class=\"ez-toc-section\" id=\"Example_of_numpy_diag_and_numpy_diagonal\"><\/span>Example of numpy diag() and numpy diagonal()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will explain the examples of the numpy diag() function operating on the 2-d array and numpy diagonal() function operating on the 2-d and 3-d array both. We will understand the difference between in a<strong> <\/strong>more efficient way through this example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import numpy library\nimport numpy as np\n\nx = np.arange(9).reshape((3,3))\n\nprint(x)\nprint(&quot;\\n&quot;)\nprint(&quot;2-d Main diagonal : &quot;,np.diag(x))\nprint(&quot;\\n&quot;)\n\n#numpy diagonal() example\nimport numpy as np\n\n#2-d array\nx = np.arange(9).reshape((3,3))\nprint(&quot;2-d Main diagonal : &quot;,np.diagonal(x))\n\n#3-d array\na = np.arange(8).reshape(2,2,2);\nprint(a)\nprint(&quot;\\n&quot;)\nprint(&quot; 3-d diagonal output : &quot;,a.diagonal(0,0,1))\n<\/pre><\/div>\n\n\n<p class=\"has-medium-font-size\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;&#91;0 1 2]\n &#91;3 4 5]\n &#91;6 7 8]]\n\n2-d Main diagonal :  &#91;0 4 8]\n\n2-d Main diagonal :  &#91;0 4 8]\n&#91;&#91;&#91;0 1]\n  &#91;2 3]]\n\n &#91;&#91;4 5]\n  &#91;6 7]]]\n\n 3-d diagonal output :  &#91;&#91;0 6]\n &#91;1 7]]<\/code><\/pre>\n\n\n\n<p class=\"has-medium-font-size\"><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\" id=\"block-e51bda78-89a2-4ea7-a7b4-7a95241749ab\">\n<li>Firstly, we will import the numpy library with an alias name as np.<\/li>\n\n\n\n<li>Then, we will apply the np.arange() function from the numpy library to create a 2-D multidimensional array.<\/li>\n\n\n\n<li>After that, we have printed the matrix formed.<\/li>\n\n\n\n<li>At last, we applied the diag() function and printed the diagonal of the array.<\/li>\n\n\n\n<li>Hence, you can see the output.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In this tutorial, we learned about the numpy diagonal() function concept. We have seen how to print the multidimensional array&#8217;s diagonal and construct a multidimensional diagonal. We have all the ways through which we can do so, and all the examples are explained in detail for a better understanding of the concept. You can use any of the methods according to your needs in the program.<\/p>\n\n\n\n<p>However, please let me know in the comment section below if you have any questions. I will try to help you as soon as possible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Python, we have seen many functions or methods of the numpy library. This tutorial will teach us about the numpy diag() function. As we &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Numpy diag() | How to Use np.diag Function in Python\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/#more-10403\" aria-label=\"More on Numpy diag() | How to Use np.diag Function in Python\">Read more<\/a><\/p>\n","protected":false},"author":17,"featured_media":10440,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1495],"tags":[3811,3813,3812,3814],"class_list":["post-10403","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy","tag-diag-numpy","tag-diag-numpy-2","tag-numpy-diag-matrix","tag-numpy-diag-command","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Numpy diag() | How to Use np.diag Function in Python - Python Pool<\/title>\n<meta name=\"description\" content=\"The diag() function is used to extract the diagonal of multidimensional array and construct a diagonal array with the help of a numpy library.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/numpy-diag\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Numpy diag() | How to Use np.diag Function in Python\" \/>\n<meta property=\"og:description\" content=\"In Python, we have seen many functions or methods of the numpy library. This tutorial will teach us about the numpy diag() function. As we come in a\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/numpy-diag\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-06T03:20:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-03T07:56:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Use-np.diag-Function-in-Python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1350\" \/>\n\t<meta property=\"og:image:height\" content=\"650\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Siddharth Jain\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Siddharth Jain\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/numpy-diag\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-diag\/\"},\"author\":{\"name\":\"Siddharth Jain\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa\"},\"headline\":\"Numpy diag() | How to Use np.diag Function in Python\",\"datePublished\":\"2021-04-06T03:20:25+00:00\",\"dateModified\":\"2024-01-03T07:56:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-diag\/\"},\"wordCount\":1275,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-diag\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Use-np.diag-Function-in-Python.jpg\",\"keywords\":[\"diag numpy\",\"diag() numpy\",\"numpy diag matrix\",\"numpy diag() command\"],\"articleSection\":[\"Numpy\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/numpy-diag\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.pythonpool.com\/numpy-diag\/\",\"url\":\"https:\/\/www.pythonpool.com\/numpy-diag\/\",\"name\":\"Numpy diag() | How to Use np.diag Function in Python - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-diag\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-diag\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Use-np.diag-Function-in-Python.jpg\",\"datePublished\":\"2021-04-06T03:20:25+00:00\",\"dateModified\":\"2024-01-03T07:56:11+00:00\",\"description\":\"The diag() function is used to extract the diagonal of multidimensional array and construct a diagonal array with the help of a numpy library.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/numpy-diag\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/numpy-diag\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/numpy-diag\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Use-np.diag-Function-in-Python.jpg\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Use-np.diag-Function-in-Python.jpg\",\"width\":1350,\"height\":650,\"caption\":\"Numpy diag\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/numpy-diag\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Numpy diag() | How to Use np.diag Function in Python\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa\",\"name\":\"Siddharth Jain\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g\",\"caption\":\"Siddharth Jain\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Numpy diag() | How to Use np.diag Function in Python - Python Pool","description":"The diag() function is used to extract the diagonal of multidimensional array and construct a diagonal array with the help of a numpy library.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/numpy-diag\/","og_locale":"en_US","og_type":"article","og_title":"Numpy diag() | How to Use np.diag Function in Python","og_description":"In Python, we have seen many functions or methods of the numpy library. This tutorial will teach us about the numpy diag() function. As we come in a","og_url":"https:\/\/www.pythonpool.com\/numpy-diag\/","og_site_name":"Python Pool","article_published_time":"2021-04-06T03:20:25+00:00","article_modified_time":"2024-01-03T07:56:11+00:00","og_image":[{"width":1350,"height":650,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Use-np.diag-Function-in-Python.jpg","type":"image\/jpeg"}],"author":"Siddharth Jain","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Siddharth Jain","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/numpy-diag\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/numpy-diag\/"},"author":{"name":"Siddharth Jain","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa"},"headline":"Numpy diag() | How to Use np.diag Function in Python","datePublished":"2021-04-06T03:20:25+00:00","dateModified":"2024-01-03T07:56:11+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-diag\/"},"wordCount":1275,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-diag\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Use-np.diag-Function-in-Python.jpg","keywords":["diag numpy","diag() numpy","numpy diag matrix","numpy diag() command"],"articleSection":["Numpy"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/numpy-diag\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.pythonpool.com\/numpy-diag\/","url":"https:\/\/www.pythonpool.com\/numpy-diag\/","name":"Numpy diag() | How to Use np.diag Function in Python - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/numpy-diag\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/numpy-diag\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Use-np.diag-Function-in-Python.jpg","datePublished":"2021-04-06T03:20:25+00:00","dateModified":"2024-01-03T07:56:11+00:00","description":"The diag() function is used to extract the diagonal of multidimensional array and construct a diagonal array with the help of a numpy library.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/numpy-diag\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/numpy-diag\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/numpy-diag\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Use-np.diag-Function-in-Python.jpg","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/04\/How-to-Use-np.diag-Function-in-Python.jpg","width":1350,"height":650,"caption":"Numpy diag"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/numpy-diag\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Numpy diag() | How to Use np.diag Function in Python"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/75a3240fabe5ea90200777a9d8d3b4fa","name":"Siddharth Jain","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/152b0ac2e5fa2e6328f374499fff4a7a6299477b9cf7bbb15ebc01a88f8f673f?s=96&d=wavatar&r=g","caption":"Siddharth Jain"}}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/10403","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=10403"}],"version-history":[{"count":10,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/10403\/revisions"}],"predecessor-version":[{"id":31937,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/10403\/revisions\/31937"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/10440"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=10403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=10403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=10403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}