{"id":36944,"date":"2022-11-19T09:48:28","date_gmt":"2022-11-19T09:48:28","guid":{"rendered":"https:\/\/www.askpython.com\/?p=36944"},"modified":"2023-02-16T19:56:34","modified_gmt":"2023-02-16T19:56:34","slug":"numpy-zeros_like","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-zeros_like","title":{"rendered":"NumPy zeros_like &#8211; A Complete Guide"},"content":{"rendered":"\n<p>In this tutorial, we will be learning about the NumPy zeros_like method and also seeing a lot of examples regarding the same. So let us begin!<\/p>\n\n\n\n<p><strong><em>Recommended Read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-zeros\" type=\"post\" id=\"36933\">NumPy zeros &#8211; A Complete Guide<\/a><\/em><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What is NumPy zeros_like?<\/h2>\n\n\n\n<p>The <code>zeros_like<\/code> method in NumPy is a function that returns an array of zeros having the same shape and size as the given array.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of zeros_like<\/h2>\n\n\n\n<p><strong>numpy.<\/strong><strong>zeros_like<\/strong><strong>(<\/strong><em>a<\/em><strong>,&nbsp;<\/strong><em>dtype=None<\/em><strong>,&nbsp;<\/strong><em>order=&#8217;K&#8217;<\/em><strong>,&nbsp;<\/strong><em>subok=True<\/em><strong>,&nbsp;<\/strong><em>shape=None<\/em><strong>)<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Parameter<\/strong><\/td><td><strong>Description<\/strong><\/td><td><strong>Required\/Optional<\/strong><\/td><\/tr><tr><td>a (array_like)<\/td><td>An object which defines the shape and data type of the array to be returned.<\/td><td>Required<\/td><\/tr><tr><td>dtype (data type)<\/td><td>The data type of the desired array. Overrides the data type of the result.<\/td><td>Optional<\/td><\/tr><tr><td>order<\/td><td>The desired order in which the multi-dimensional data is to be stored in the memory. It can be row-major (\u2018C\u2019), column-major (\u2018F\u2019), &#8216;A&#8217; means &#8216;F&#8217; if <em>a<\/em> is Fortran contiguous, &#8216;C&#8217; otherwise. &#8216;K&#8217; implies matching the layout of <em>a<\/em> as much as possible.<\/td><td>Optional<\/td><\/tr><tr><td>subok (bool)<\/td><td>Determines whether the newly created array will use the sub-class type of <em>a<\/em> (subok=True) or will be a base class array (subok=False).<br>The default value is <strong>True<\/strong>.<\/td><td>Optional<\/td><\/tr><tr><td>shape<\/td><td>The shape of the desired array. Overrides the shape of the result. <\/td><td>Optional<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Returns:<\/strong><br>An array with the same shape and data type as the given array.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of Numpy zeros_like function<\/h2>\n\n\n\n<p>Let&#8217;s now take a look at how the numpy.zeros_like() function works and what is the expected output for different types of inputs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1-dimensional array using zeros_like <\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = np.arange(10)\nprint(&quot;a =&quot;, a)\n\nb = np.zeros_like(a)\nprint(&quot;b =&quot;, b)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;0 1 2 3 4 5 6 7 8 9]\nb = &#x5B;0 0 0 0 0 0 0 0 0 0]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2-dimensional array using zeros_like<\/h3>\n\n\n\n<p><strong>N x N array<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = np.arange(10).reshape(2, 5)\nprint(&quot;a =\\n&quot;, a)\n\nb = np.zeros_like(a)\nprint(&quot;b =\\n&quot;, b)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na =\n &#x5B;&#x5B;0 1 2 3 4]\n &#x5B;5 6 7 8 9]]\nb =\n &#x5B;&#x5B;0 0 0 0 0]\n &#x5B;0 0 0 0 0]]\n<\/pre><\/div>\n\n\n<p><strong>1 x N array<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = np.arange(10).reshape(1, 10)\nprint(&quot;a =\\n&quot;, a)\n\nb = np.zeros_like(a)\nprint(&quot;b =\\n&quot;, b)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na =\n &#x5B;&#x5B;0 1 2 3 4 5 6 7 8 9]]\nb =\n &#x5B;&#x5B;0 0 0 0 0 0 0 0 0 0]]\n<\/pre><\/div>\n\n\n<p><strong>N x 1 array<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = np.arange(10).reshape(10, 1)\nprint(&quot;a =\\n&quot;, a)\n\nb = np.zeros_like(a)\nprint(&quot;b =\\n&quot;, b)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na =\n &#x5B;&#x5B;0]\n &#x5B;1]\n &#x5B;2]\n &#x5B;3]\n &#x5B;4]\n &#x5B;5]\n &#x5B;6]\n &#x5B;7]\n &#x5B;8]\n &#x5B;9]]\nb =\n &#x5B;&#x5B;0]\n &#x5B;0]\n &#x5B;0]\n &#x5B;0]\n &#x5B;0]\n &#x5B;0]\n &#x5B;0]\n &#x5B;0]\n &#x5B;0]\n &#x5B;0]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1-dimensional float-type array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = np.arange(10)\nprint(&quot;a =&quot;, a)\n\nb = np.zeros_like(a, dtype=float)\nprint(&quot;b =&quot;, b)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na = &#x5B;0 1 2 3 4 5 6 7 8 9]\nb = &#x5B;0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2-dimensional float-type array<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n\na = np.arange(10).reshape(2, 5)\nprint(&quot;a =\\n&quot;, a)\n\nb = np.zeros_like(a, dtype=float)\nprint(&quot;b =\\n&quot;, b)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na =\n &#x5B;&#x5B;0 1 2 3 4]\n &#x5B;5 6 7 8 9]]\nb =\n &#x5B;&#x5B;0. 0. 0. 0. 0.]\n &#x5B;0. 0. 0. 0. 0.]]\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Difference between zeros and zeros_like<\/h2>\n\n\n\n<p>Note that, in the <code>zeros<\/code> method we are creating a new array of our desired shape and data type having all the values as 0. But, here, we are directly passing an array or an array-like object to get an array of the same shape and data type. The NumPy <code>zeros_like<\/code> function takes more time than the NumPy <code>zeros<\/code> function to produce an array with all 0.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>That\u2019s all! In this tutorial, we learned about the\u00a0<strong>Numpy zeros_like<\/strong>\u00a0method and practiced different types of examples using the same.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.zeros_like.html\" target=\"_blank\" rel=\"noreferrer noopener\">NumPy zeros_like Official Documentation <\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will be learning about the NumPy zeros_like method and also seeing a lot of examples regarding the same. So let us begin! Recommended Read: NumPy zeros &#8211; A Complete Guide What is NumPy zeros_like? The zeros_like method in NumPy is a function that returns an array of zeros having the same [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":36956,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-36944","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/36944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=36944"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/36944\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/36956"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=36944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=36944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=36944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}