{"id":8993,"date":"2021-03-17T19:37:28","date_gmt":"2021-03-17T14:07:28","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8993"},"modified":"2022-09-11T13:34:00","modified_gmt":"2022-09-11T08:04:00","slug":"creating-special-numpy-arrays-in-python","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/creating-special-numpy-arrays-in-python\/","title":{"rendered":"Creating Special Numpy Arrays in Python"},"content":{"rendered":"\n<p>In the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/python-numpy-array-tutorial\/\" target=\"_blank\" rel=\"noreferrer noopener\">last tutorial<\/a>, we gave an introduction into Numpy arrays. Recall that a Numpy array can be created manually by calling the numpy.array() method and passing a list as an argument. But there may be situations where you may want to create a special <a class=\"rank-math-link\" href=\"https:\/\/www.w3schools.com\/python\/python_arrays.asp\" rel=\"nofollow noopener\" target=\"_blank\">type of arrays<\/a> such as an array populated with only zeroes or ones or an identity array. You do not have to fill the arrays manually using the numpy.array() method. There are methods in Numpy that allow you to create such arrays right out of the gate.&nbsp;<\/p>\n\n\n\n<p>In this tutorial, you will learn how to create arrays and identity arrays, an array filled with zeros, and other special arrays. Let\u2019s get started.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating an Array filled with Zeros<\/h2>\n\n\n\n<p>To create a filled with zeros, the zeros() method is called. The method takes the shape of the array to be created as a required parameter.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\nprint(np.zeros(5))\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&#91;0. 0. 0. 0. 0.]\n<\/code><\/pre>\n\n\n\n<p>If you want to create a 2-dimensional array, you pass the shape of the array as a tuple. Let\u2019s say we want to create a 4 by 3 array filled with zeros<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\nprint(np.zeros((3, 4)))<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&#91;&#91;0. 0. 0. 0.]\n &#91;0. 0. 0. 0.]\n &#91;0. 0. 0. 0.]]\nAs seen, a 2-dimensional array, with a shape (4, 3) is created. \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Creating an Array filled with Ones<\/h2>\n\n\n\n<p>To create an array filled with ones, the ones() method is called. Just like the numpy.zeros() method, the numpy.ones() method takes the shape of the array as an argument.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\n&nbsp;\nprint(np.ones((3, 4)))<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&#91;&#91;1. 1. 1. 1.]\n &#91;1. 1. 1. 1.]\n &#91;1. 1. 1. 1.]]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Create an Array filled with Other Numbers<\/h2>\n\n\n\n<p>To create an array filled with a specific number, the full() method is called. This method receives two required arguments. First, the shape of the array to be created, and second the number you wish to be populated. Say, I wish to create an array, filled with 5, the full() method can be used as shown below.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\n&nbsp;\nprint(np.full((5, 4), 5, dtype='int64'))\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&#91;&#91;5 5 5 5]\n &#91;5 5 5 5]\n &#91;5 5 5 5]\n &#91;5 5 5 5]\n &#91;5 5 5 5]]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Creating an Identity Array<\/h2>\n\n\n\n<p>In mathematics, an identity matrix is a special kind of matrix whose diagonal fields are populated with 1s while the rest are fields with zeros. In Numpy, such types of matrices can be created as arrays by using the eye() method.&nbsp;<\/p>\n\n\n\n<p>The method takes the length of the array to be created as a parameter. Since an identity matrix is always a square matrix, which means it has an equal number of rows and columns, the eye() method does not require a tuple of both numbers. As in (3, 3) to create a 3 by 3 identity matrix. It rather requires just the number, as in 3.&nbsp;<\/p>\n\n\n\n<p>In the example below, we attempt to create a 3 by 3 identity matrix.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\n&nbsp;\nprint(np.eye(3))\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&#91;&#91;1. 0. 0.]\n &#91;0. 1. 0.]\n &#91;0. 0. 1.]]\n<\/code><\/pre>\n\n\n\n<p>Another way of creating an identity array is using the numpy.identity() method. Just like in the numpy.eye() method, it receives the shape of the array as a parameter.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np\n&nbsp;\n#using the identity() method\nprint(np.identity(3))\n<\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Output:\n&#91;&#91;1. 0. 0.]\n &#91;0. 1. 0.]\n &#91;0. 0. 1.]]\n<\/code><\/pre>\n\n\n\n<p>As seen, it returns the same result.&nbsp;<\/p>\n\n\n\n<p>In the next tutorial, we will discuss how to use other <a href=\"https:\/\/www.h2kinfosys.com\/blog\/python-numpy-tutorial\/\" class=\"rank-math-link\">methods in NumPy<\/a> to create NumPy arrays. In the meantime, if you have any questions, please feel free to drop them in the comment section and I\u2019d do my best to answer them.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last tutorial, we gave an introduction into Numpy arrays. Recall that a Numpy array can be created manually by calling the numpy.array() method and passing a list as an argument. But there may be situations where you may want to create a special type of arrays such as an array populated with only [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9013,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[498],"tags":[],"class_list":["post-8993","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8993"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8993\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/9013"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}