{"id":19654,"date":"2021-06-29T08:05:12","date_gmt":"2021-06-29T08:05:12","guid":{"rendered":"https:\/\/www.askpython.com\/?p=19654"},"modified":"2021-06-30T09:10:28","modified_gmt":"2021-06-30T09:10:28","slug":"numpy-vstack","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy-vstack","title":{"rendered":"Numpy vstack() method &#8211; A Complete Overview"},"content":{"rendered":"\n<p>Hello everyone! In this tutorial, we will learn what the Numpy <code>vstack()<\/code> method is and how to use it in Python. So let&#8217;s get started.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is numpy.vstack() method ?<\/h2>\n\n\n\n<p><code>Numpy.vstack()<\/code> is a function in Python that takes a <a href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple\" data-type=\"post\" data-id=\"449\">tuple<\/a> of arrays and concatenates them vertically along the first dimension to make them a single array. <\/p>\n\n\n\n<p>It&#8217;s syntax is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.vstack(tup)\n<\/pre><\/div>\n\n\n<p>The parameter it takes is a tuple which is a sequence of ndarrays that we want to concatenate. The arrays must have the same shape along all axis except the first axis.<\/p>\n\n\n\n<p>The method returns a ndarray which is formed by stacking the arrays given in the input. The returned array will have at least 2 dimensions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples of Numpy vstack()<\/h2>\n\n\n\n<p>For linear 1-D arrays, all the arrays are stacked vertically to form a 2-D array. All the input arrays must have the same length.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy\n\na = numpy.array(&#x5B;1, 2, 3, 4, 5])\nb = numpy.array(&#x5B;6, 7, 8, 9, 10])\nc = numpy.array(&#x5B;11, 12, 13, 14, 15])\n\nprint(&quot;Shape of array A:&quot;, a.shape)\nprint(&quot;Shape of array B:&quot;, b.shape)\nprint(&quot;Shape of array C:&quot;, c.shape)\nprint()\n\nstack = numpy.vstack((a, b, c))\nprint(&quot;Shape of new stacked array:&quot;, stack.shape)\nprint(&quot;Stacked array is&quot;)\nprint(stack)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nShape of array A: (5,)\nShape of array B: (5,)\nShape of array C: (5,)\n\nShape of new stacked array: (3, 5)\nStacked array is\n&#x5B;&#x5B; 1  2  3  4  5]\n &#x5B; 6  7  8  9 10]\n &#x5B;11 12 13 14 15]]\n<\/pre><\/div>\n\n\n<p>For N-dimensional arrays, arrays are stacked along the first dimensions as shown in the following example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy\n\na = numpy.array(&#x5B; &#x5B;1, 2, 3], &#x5B;4, 5, 6] ])\nb = numpy.array(&#x5B; &#x5B;7, 8, 9], &#x5B;10, 11, 12] ])\n\nprint(&quot;Shape of array A:&quot;, a.shape)\nprint(&quot;Shape of array B:&quot;, b.shape)\nprint()\n\nstack = numpy.vstack((a, b))\nprint(&quot;Shape of new stacked array:&quot;, stack.shape)\nprint(&quot;Array is&quot;)\nprint(stack)\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=\"\">\nShape of array A: (2, 3)\nShape of array B: (2, 3)\n\nShape of new stacked array: (4, 3)\nArray is\n&#x5B;&#x5B; 1  2  3]\n &#x5B; 4  5  6]\n &#x5B; 7  8  9]\n &#x5B;10 11 12]]\n<\/pre><\/div>\n\n\n<p>For N-dimensional arrays, the shape of the arrays must be the same along all dimensions except the first dimension as shown below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy\n\na = numpy.array(&#x5B; &#x5B;1, 2], &#x5B;3, 4] ])\nb = numpy.array(&#x5B; &#x5B;5, 6], &#x5B;7, 8], &#x5B;9, 10] ])\n\nprint(&quot;Shape of array A:&quot;, a.shape)\nprint(&quot;Shape of array B:&quot;, b.shape)\nprint()\n\nstack = numpy.vstack((a, b))\nprint(&quot;Shape of new stacked array:&quot;, stack.shape)\nprint(&quot;Array is&quot;)\nprint(stack)\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nShape of array A: (2, 2)\nShape of array B: (3, 2)\n\nShape of new stacked array: (5, 2)\nArray is\n&#x5B;&#x5B; 1  2]\n &#x5B; 3  4]\n &#x5B; 5  6]\n &#x5B; 7  8]\n &#x5B; 9 10]]\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this Python tutorial, we learned about <code>vstack()<\/code> method present in the NumPy module. This function makes most sense for arrays with up to 3 dimensions. For instance, for pixel-data with a height (first axis), width (second axis), and r\/g\/b channels (third axis).<\/p>\n\n\n\n<p>Thanks for reading!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone! In this tutorial, we will learn what the Numpy vstack() method is and how to use it in Python. So let&#8217;s get started. What is numpy.vstack() method ? Numpy.vstack() is a function in Python that takes a tuple of arrays and concatenates them vertically along the first dimension to make them a single [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":19656,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-19654","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/19654","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\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=19654"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/19654\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/19656"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=19654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=19654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=19654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}