{"id":21880,"date":"2023-09-29T11:05:48","date_gmt":"2023-09-29T05:35:48","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=21880"},"modified":"2023-09-29T11:05:49","modified_gmt":"2023-09-29T05:35:49","slug":"numpy-eye-in-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-eye-in-python\/","title":{"rendered":"numpy.eye() in Python: Creating Identity Matrix"},"content":{"rendered":"\n<p>The <strong>numpy.eye()<\/strong> function in the <a href=\"https:\/\/codeforgeek.com\/python-top-libraries-for-machine-learning-and-data-science\/\">NumPy<\/a> library is used to create a 2-D identity matrix with ones on the main diagonal (from the top-left to the bottom-right) and zeros elsewhere. <\/p>\n\n\n\n<p>In this article, we\u2019ll understand look at<b> <\/b>five examples of creating an identity matrix by using<strong> numpy.eye() <\/strong>function. Let\u2019s first look at this function\u2019s syntax, parameters and return value.<\/p>\n\n\n\n<p><strong>Also Read: <a href=\"https:\/\/codeforgeek.com\/numpy-cbrt-python\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/numpy-cbrt-python\/\">numpy.cbrt() in Python<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of numpy.eye() Function<\/h2>\n\n\n\n<p>Below is the syntax for using the <strong>numpy.eye()<\/strong> function in <a href=\"https:\/\/codeforgeek.com\/python\/\">Python<\/a>.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.eye(N, M=None, k=0, dtype=&lt;class &#039;float&#039;&gt;, order=&#039;C&#039;)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>N: <\/strong>The number of rows (and columns) in the output matrix.<\/li>\n\n\n\n<li><strong>M (optional):<\/strong> The number of columns in the output matrix. If not provided, it defaults to N.<\/li>\n\n\n\n<li><strong>k (optional): <\/strong>The index of the diagonal. The main diagonal is denoted by k = 0, the diagonal above it by k &gt; 0, and the diagonal below it by k &lt; 0. The default value is k = 0.<\/li>\n\n\n\n<li><strong>dtype (optional):<\/strong> The data type of the output array. The default is a floating-point type.<\/li>\n\n\n\n<li><strong>order (optional):<\/strong> Specifies the memory layout of the output array. It can be &#8216;C&#8217; (row-major, the default) or &#8216;F&#8217; (column-major).<\/li>\n<\/ul>\n\n\n\n<p><strong>Return: <\/strong>An array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of numpy.eye() Function<\/h2>\n\n\n\n<p>Let us now look at examples to demonstrate the use of <strong>numpy.eye()<\/strong> function in Python. In each example, we have passed different parameter combinations to understand how this function works in different scenarios. Let&#8217;s see each of them one by one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: By Specifying N in numpy.eye() Function<\/h3>\n\n\n\n<p>In this example, we will only provide <strong>N<\/strong> (number of the rows) in the <strong>numpy.eye()<\/strong> function.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nnp.eye(5)\n<\/pre><\/div>\n\n\n<p>In the above code, to use the <strong>eye()<\/strong> function we first imported the <strong>Numpy<\/strong> library as <strong>np<\/strong> then we provided parameter <strong>N<\/strong> which is 5 in the <strong>np.eye()<\/strong> function means we provided the number of rows for the output array.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"103\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1817-1-1024x103.png\" alt=\"Example By specifying N in numpy.eye() function\" class=\"wp-image-22106\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1817-1-1024x103.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1817-1-300x30.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1817-1-768x77.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1817-1-1536x154.png 1536w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1817-1.png 1585w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the output above, we got a 5&#215;5 matrix with ones on the main diagonal and zeros elsewhere.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: By Specifying N and M in numpy.eye() Function<\/h3>\n\n\n\n<p>In this example, we will provide <strong>N <\/strong>(number of rows) and <strong>M<\/strong> (number of columns) in the <strong>numpy.eye()<\/strong> function.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.eye(2,3) \n<\/pre><\/div>\n\n\n<p>In the above code, we provided parameters <strong>N<\/strong> as 2 and <strong>M<\/strong> as 3  in the <strong>np.eye()<\/strong> function means we provided the number of rows and the number of columns for the output array respectively.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"79\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1818-1024x79.png\" alt=\"Example By specifying N and M in numpy.eye() function\" class=\"wp-image-22107\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1818-1024x79.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1818-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1818-768x59.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1818.png 1129w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the output above, we got a 2&#215;3 matrix with ones on the main diagonal and zeros elsewhere.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: By Specifying N and k in numpy.eye() Function<\/h3>\n\n\n\n<p>In this example, we will provide <strong>N<\/strong> (number of rows) and <strong>k<\/strong> (index of the diagonal) in the <strong>numpy.eye()<\/strong> function.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.eye(4,k=-1)\n<\/pre><\/div>\n\n\n<p>In the above code, within the <strong>np.eye()<\/strong> function we provided parameter <strong>N<\/strong> as 4 which is the number of rows in the output array and parameter <strong>k<\/strong> as -1 which means the diagonal has been one step downward from the default diagonal.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"89\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1819-1024x89.png\" alt=\"Example By specifying N and k in numpy.eye() function\" class=\"wp-image-22108\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1819-1024x89.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1819-300x26.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1819-768x67.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1819.png 1477w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the output above, we got a 4&#215;4 matrix where the diagonal has been one step downward from the default diagonal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4: By Specifying N and dtype in numpy.eye() Function<\/h3>\n\n\n\n<p>In this example, we will provide <strong>N<\/strong> (number of rows) and <strong>dtype<\/strong> (data type) in the <strong>numpy.eye()<\/strong> function.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.eye(5,dtype=int)\n<\/pre><\/div>\n\n\n<p>In the above code, within the <strong>np.eye()<\/strong> function we provided parameter <strong>N<\/strong> as 5 which is the number of rows in the output array or matrix and parameter <strong>dtype<\/strong> as int which means we changed the datatype of values to an integer which was by default float datatype.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"105\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1820-1024x105.png\" alt=\"Example By specifying N and dtype in numpy.eye() function\" class=\"wp-image-22109\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1820-1024x105.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1820-300x31.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1820-768x79.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1820-1536x158.png 1536w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1820.png 1569w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the output above, we got a 5&#215;5 matrix where the datatype of the values changed to integer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 5: By Specifying N, M, k, and dtype in numpy.eye() Function<\/h3>\n\n\n\n<p>In this example, we will provide all the parameters <strong>N, M<\/strong>,<strong> k,<\/strong> and <strong>dtype<\/strong> together in the <strong>numpy.eye()<\/strong> function.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.eye(6,9,k=3,dtype=int)\n<\/pre><\/div>\n\n\n<p>In the above code, within the <strong>np.eye()<\/strong> function we provided <strong>N<\/strong> as 6 and <strong>M<\/strong> as 9  which are the number of the rows and the number of columns. Also, we provided <strong>K<\/strong> as 3 which means diagonal three steps upward to the default diagonal and we mentioned <strong>dtype<\/strong> as <strong>int<\/strong> which changed the datatype of values in the output matrix from float to integer.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"122\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1821-1024x122.png\" alt=\"Example By specifying N, M, k, and dtype in numpy.eye() function\" class=\"wp-image-22110\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1821-1024x122.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1821-300x36.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1821-768x91.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1821-1536x183.png 1536w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1821.png 1570w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the output above, we got a 6&#215;9 matrix with a diagonal of three steps upward to the default diagonal and the datatype of the values is integers.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this tutorial, we have discussed<strong> numpy.eye()<\/strong> function provided by Python\u2019s NumPy library, its syntax, parameters, and return value and also explored five examples to create an identity matrix by passing different parameters. After reading this tutorial, we hope you can easily<strong> <\/strong>create an identity matrix by using numpy.eye() function<b> <\/b>in Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/46126914\/what-does-np-eyennparray-mean\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/46126914\/what-does-np-eyennparray-mean<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The numpy.eye() function in the NumPy library is used to create a 2-D identity matrix with ones on the main diagonal (from the top-left to the bottom-right) and zeros elsewhere. In this article, we\u2019ll understand look at five examples of creating an identity matrix by using numpy.eye() function. Let\u2019s first look at this function\u2019s syntax, [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":22103,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_surecart_dashboard_logo_width":"180px","_surecart_dashboard_show_logo":true,"_surecart_dashboard_navigation_orders":true,"_surecart_dashboard_navigation_invoices":true,"_surecart_dashboard_navigation_subscriptions":true,"_surecart_dashboard_navigation_downloads":true,"_surecart_dashboard_navigation_billing":true,"_surecart_dashboard_navigation_account":true,"_uag_custom_page_level_css":"","footnotes":""},"categories":[134],"tags":[],"class_list":["post-21880","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"uagb_featured_image_src":{"full":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.eye-in-Python-Creating-an-Identity-Matrix.png",1200,800,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.eye-in-Python-Creating-an-Identity-Matrix-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.eye-in-Python-Creating-an-Identity-Matrix-300x200.png",300,200,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.eye-in-Python-Creating-an-Identity-Matrix-768x512.png",768,512,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.eye-in-Python-Creating-an-Identity-Matrix-1024x683.png",1024,683,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.eye-in-Python-Creating-an-Identity-Matrix.png",1200,800,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.eye-in-Python-Creating-an-Identity-Matrix.png",1200,800,false]},"uagb_author_info":{"display_name":"Priyanshu Singh","author_link":"https:\/\/codeforgeek.com\/author\/priyanshu\/"},"uagb_comment_info":0,"uagb_excerpt":"The numpy.eye() function in the NumPy library is used to create a 2-D identity matrix with ones on the main diagonal (from the top-left to the bottom-right) and zeros elsewhere. In this article, we\u2019ll understand look at five examples of creating an identity matrix by using numpy.eye() function. Let\u2019s first look at this function\u2019s syntax,&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/21880","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/users\/95"}],"replies":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/comments?post=21880"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/21880\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/22103"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=21880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=21880"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=21880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}