{"id":88578,"date":"2024-07-15T18:00:49","date_gmt":"2024-07-15T12:30:49","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=88578"},"modified":"2024-07-15T18:10:37","modified_gmt":"2024-07-15T12:40:37","slug":"numpy-hstack-vstack-reshape-and-flatten-function","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/","title":{"rendered":"Numpy hstack(), vstack(), reshape(), and flatten() Function"},"content":{"rendered":"<p>If you&#8217;re diving into the world of data manipulation and analysis using Python, you&#8217;ll likely come across the powerful numpy library. Numpy provides a varied range of functions and tools for working with arrays, and in this tutorial, we&#8217;ll explore four essential functions: numpy.hstack(), numpy.vstack(), numpy.reshape(), and numpy.flatten(). These functions are extremely crucial for reshaping and combining arrays, which is a basic necessity in data preprocessing and analysis.<\/p>\n<h3>1. numpy.hstack()<\/h3>\n<p>numpy.hstack() stands for &#8220;horizontal stack&#8221;. It&#8217;s used to concatenate multiple arrays horizontally, meaning it combines arrays side by side.<\/p>\n<p>When you use numpy.hstack(), the arrays within the tuple are combined along the second axis (axis 1), effectively joining their columns. It&#8217;s important that the arrays being stacked have compatible shapes along this axis.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">numpy.hstack(tup)<\/pre>\n<p><strong>tup:<\/strong> A tuple of arrays to be stacked horizontally.<\/p>\n<p><strong>Diagram:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/numpy.hstack.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-89072\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/numpy.hstack.webp\" alt=\"numpy.hstack()\" width=\"450\" height=\"400\" \/><\/a><\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\narray1 = np.array([1, 2, 3])\r\narray2 = np.array([4, 5, 6])\r\n\r\nhorizontal_stack = np.hstack((array1, array2))\r\nprint(horizontal_stack)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>[1 2 3 4 5 6]<\/p>\n<h3>2. numpy.vstack()<\/h3>\n<p>numpy.vstack() stands for &#8220;vertical stack&#8221;. It&#8217;s used to concatenate multiple arrays vertically, meaning it stacks arrays on top of each other.<\/p>\n<p>When you use numpy.vstack(), the arrays within the tuple are combined along the first axis (axis 0), effectively joining their rows. The arrays being stacked must have the same number of columns.<\/p>\n<p><strong>Syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">numpy.vstack(tup)<\/pre>\n<p><strong>tup:<\/strong> A tuple of arrays to be stacked vertically.<\/p>\n<p><strong>Diagram:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/numpy.vstack.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-89073\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/numpy.vstack.webp\" alt=\"numpy. vstack()\" width=\"344\" height=\"400\" \/><\/a><\/p>\n<p><strong>Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\narray1 = np.array([1, 2, 3])\r\narray2 = np.array([4, 5, 6])\r\n\r\nvertical_stack = np.vstack((array1, array2))\r\nprint(vertical_stack)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>[[1 2 3]<br \/>\n[4 5 6]]<\/p>\n<h3>3. numpy.reshape()<\/h3>\n<p>numpy.reshape() is used to change the shape of an array without changing its data. This can be incredibly useful for converting between 1D, 2D, and higher-dimensional arrays.<\/p>\n<p>numpy.reshape() alters the arrangement of elements in the array according to the new shape specified. It doesn&#8217;t modify the data itself, just the view of it. The new shape must have the same total number of elements as the original array.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">numpy.reshape(a, newshape)<\/pre>\n<p><strong>a:<\/strong> The array to be reshaped.<\/p>\n<p><strong>newshape:<\/strong> The new shape you want for the array, represented as a tuple of integers.<\/p>\n<p><strong>Diagram:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/01\/numpy.reshape.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-89077\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2024\/01\/numpy.reshape.webp\" alt=\"numpy.-reshape()\" width=\"414\" height=\"400\" \/><\/a><\/p>\n<p><strong>Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\noriginal_array = np.array([1, 2, 3, 4, 5, 6])\r\nreshaped_array = np.reshape(original_array, (2, 3))\r\nprint(reshaped_array)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>[[1 2 3]<br \/>\n[4 5 6]]<\/p>\n<h3>4. numpy.flatten()<\/h3>\n<p>numpy.flatten() is used to convert a multi-dimensional array into a 1D array.<\/p>\n<p>numpy.flatten() collapses a multi-dimensional array into a 1D array by iterating through the elements in row-major order (unless otherwise specified). This can be helpful for certain operations or when dealing with algorithms that require 1D data.<\/p>\n<p><strong>Syntax<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">numpy.flatten(order='C')<\/pre>\n<p><strong>order:<\/strong> The order in which the elements should be flattened. &#8216;C&#8217; means row-major (default), and &#8216;F&#8217; means column-major.<\/p>\n<p><strong>Diagram:<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/flatten.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-89074\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/flatten.webp\" alt=\"flatten()\" width=\"320\" height=\"400\" \/><\/a><\/p>\n<p><strong>Example<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\nmulti_dimensional_array = np.array([[1, 2, 3], [4, 5, 6]])\r\nflattened_array = multi_dimensional_array.flatten()\r\nprint(flattened_array)<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>[1 2 3 4 5 6]<\/p>\n<h3>Conclusion<\/h3>\n<p>Congratulations! You&#8217;ve successfully completed this tutorial on NumPy&#8217;s hstack, vstack, flatten, and reshape functions. By mastering these array manipulation techniques, you&#8217;ve gained powerful tools to efficiently manage and transform data in Python. From combining arrays to reshaping their structures, you&#8217;re now equipped to handle diverse data challenges with confidence. Happy Coding with TechVidvan!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re diving into the world of data manipulation and analysis using Python, you&#8217;ll likely come across the powerful numpy library. Numpy provides a varied range of functions and tools for working with arrays,&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447403,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[385],"tags":[5602,5603,383,5601,5657,384,386,387,388],"class_list":["post-88578","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy-tutorials","tag-flatten-hstack-reshape-vstack","tag-flatten-hstack-reshape-vstack-in-numpy","tag-learn-numpy","tag-numpy-flatten-hstack-reshape-vstack","tag-numpy-hstack-vstack-reshape-flatten-function","tag-numpy-tutorial","tag-numpy-flatten","tag-numpy-hstack","tag-numpy-reshape"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Numpy hstack(), vstack(), reshape(), and flatten() Function - TechVidvan<\/title>\n<meta name=\"description\" content=\"These functions are extremely crucial for reshaping and combining arrays, which is a basic necessity in data preprocessing and analysis.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Numpy hstack(), vstack(), reshape(), and flatten() Function - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"These functions are extremely crucial for reshaping and combining arrays, which is a basic necessity in data preprocessing and analysis.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-15T12:30:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-15T12:40:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/10\/numpy.hstack-numpy.vstack.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Numpy hstack(), vstack(), reshape(), and flatten() Function - TechVidvan","description":"These functions are extremely crucial for reshaping and combining arrays, which is a basic necessity in data preprocessing and analysis.","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:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/","og_locale":"en_US","og_type":"article","og_title":"Numpy hstack(), vstack(), reshape(), and flatten() Function - TechVidvan","og_description":"These functions are extremely crucial for reshaping and combining arrays, which is a basic necessity in data preprocessing and analysis.","og_url":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-07-15T12:30:49+00:00","article_modified_time":"2024-07-15T12:40:37+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/10\/numpy.hstack-numpy.vstack.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"Numpy hstack(), vstack(), reshape(), and flatten() Function","datePublished":"2024-07-15T12:30:49+00:00","dateModified":"2024-07-15T12:40:37+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/"},"wordCount":453,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/10\/numpy.hstack-numpy.vstack.webp","keywords":[".flatten() .hstack() .reshape() .vstack()",".flatten() .hstack() .reshape() .vstack() in numpy","learn numpy","numpy .flatten() .hstack() .reshape() .vstack()","numpy hstack() vstack() reshape() flatten() function","numPy tutorial","numpy.flatten()","numpy.hstack()","numpy.reshape()"],"articleSection":["NumPy Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/","url":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/","name":"Numpy hstack(), vstack(), reshape(), and flatten() Function - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/10\/numpy.hstack-numpy.vstack.webp","datePublished":"2024-07-15T12:30:49+00:00","dateModified":"2024-07-15T12:40:37+00:00","description":"These functions are extremely crucial for reshaping and combining arrays, which is a basic necessity in data preprocessing and analysis.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/10\/numpy.hstack-numpy.vstack.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/10\/numpy.hstack-numpy.vstack.webp","width":1200,"height":628,"caption":"numpy.hstack() numpy.vstack()"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/numpy-hstack-vstack-reshape-and-flatten-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Numpy hstack(), vstack(), reshape(), and flatten() Function"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf","name":"TechVidvan Team"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/88578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=88578"}],"version-history":[{"count":4,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/88578\/revisions"}],"predecessor-version":[{"id":447535,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/88578\/revisions\/447535"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447403"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=88578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=88578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=88578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}