{"id":22284,"date":"2023-09-29T11:16:01","date_gmt":"2023-09-29T05:46:01","guid":{"rendered":"https:\/\/codeforgeek.com\/?p=22284"},"modified":"2023-09-29T13:56:41","modified_gmt":"2023-09-29T08:26:41","slug":"numpy-reshape-python","status":"publish","type":"post","link":"https:\/\/codeforgeek.com\/numpy-reshape-python\/","title":{"rendered":"numpy.reshape() in Python: Reshaping NumPy Array"},"content":{"rendered":"\n<p>An array may have one or more dimensions for storing the data and sometimes we need to manipulate or change the structure of our data, i.e., the dimension of the array containing data, for this purpose, NumPy provided us with<strong> numpy.reshape() <\/strong>function for reshaping an array.<\/p>\n\n\n\n<p>The most common use case of reshaping the arrays is to make an array compatible with others so that it can be possible to perform matrix multiplication and operations such as element-wise operations. <\/p>\n\n\n\n<p>In this article, we will understand <a href=\"https:\/\/codeforgeek.com\/python\/\">Python<strong> <\/strong><\/a><strong>numpy.reshape()<\/strong> function with multiple examples. Let&#8217;s first look at this function&#8217;s syntax, parameters and return value.<\/p>\n\n\n\n<p><strong>Also Read: <a href=\"https:\/\/codeforgeek.com\/numpy-broadcasting\/\" data-type=\"link\" data-id=\"https:\/\/codeforgeek.com\/numpy-broadcasting\/\">Numpy Broadcasting (With Examples)<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to numpy.reshape() Function<\/h2>\n\n\n\n<p>The <strong>numpy.reshape()<\/strong> function in NumPy is used to change the shape (dimensions) of an existing array without modifying its data. It allows us to create a new view of the array with a different shape. Reshaping is a fundamental operation in numerical computing, as it enables us to prepare data for various computations and analyses.<\/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=\"\">\narray.reshape(shape)\n<\/pre><\/div>\n\n\n<p><strong>Parameters:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>array:<\/strong> The original array that we want to reshape.<\/li>\n\n\n\n<li><strong>shape:<\/strong> The new shape (dimensions) we want to give to the array. It can be specified as a tuple or as separate integer arguments.<\/li>\n<\/ul>\n\n\n\n<p><strong>Return:<\/strong> <\/p>\n\n\n\n<p>Array which got reshaped without changing the data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">NumPy&nbsp;Array Reshaping<\/h2>\n\n\n\n<p>Let&#8217;s see how we can use this function to reshape the Numpy array. For reshaping an array we must first have to create an array. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a 1D Array for Reshaping<\/h3>\n\n\n\n<p>Let&#8217;s see an example of creating a 1D array for reshaping later.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>We are creating a 1D array with 12 elements in it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\nnp1= np.array(&#x5B;1,2,3,4,5,6,7,8,9,10,11,12])\nprint(np1)\nprint(np1.shape)\n<\/pre><\/div>\n\n\n<p>Here we first imported the<strong> NumPy <\/strong>as <strong>np<\/strong>, then we created a 1D array and saved it into a variable <strong>np1<\/strong> and after that, we printed the <strong>np1<\/strong> and shape of the <strong>np1<\/strong> where we got shape as (12, ) means 12 rows and 0 columns which is a 1D array.<\/p>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"978\" height=\"74\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1845.png\" alt=\"Created a 1D array\" class=\"wp-image-22480\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1845.png 978w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1845-300x23.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1845-768x58.png 768w\" sizes=\"(max-width: 978px) 100vw, 978px\" \/><\/figure>\n\n\n\n<p>The array&nbsp;<strong>np1&nbsp;<\/strong>is 1D. We can arrange the same data contained in&nbsp;<strong>np1<\/strong>&nbsp;with a different number of dimensions such as (3,4), (4,3), (6,2), (12,1), (2,3,2), etc.<a href=\"https:\/\/files.realpython.com\/media\/numpy-reshape-8-222.db4b1cdd542a.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/p>\n\n\n\n<p>Let us now understand how we can use&nbsp;<strong>numpy.reshape()<\/strong>&nbsp;with some examples to reshape the above 1D array <strong>np1<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reshaping 1D Array to 2D Array<\/h3>\n\n\n\n<p>Here we will reshape the above 1D array <strong>np1<\/strong> with 12 elements into the  2-D array.<\/p>\n\n\n\n<p>The dimensions we will be taking for our 2D array will be 3 rows and 4 columns.<\/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=\"\">\nnp2 = np1.reshape(3,4)\nprint(np2)\nprint(np2.shape)\n<\/pre><\/div>\n\n\n<p>In the above code, we have first provided the dimension of the newly reshaped array which is to have 3 rows and 4 columns. Then we printed the <strong>np2 <\/strong>array with its shape and we got the 2D array with its shape means dimension as (3,4) which means our new 2D array contains 3 rows and 4 columns as expected.<\/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=\"99\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1846-1024x99.png\" alt=\"Reshaping a 1D array to 2D array\" class=\"wp-image-22481\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1846-1024x99.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1846-300x29.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1846-768x74.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1846.png 1411w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Reshaping 1D Array to 3D Array<\/h3>\n\n\n\n<p>Here we will reshape the above 1D array <strong>np1<\/strong> into a 3D array.<\/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=\"\">\nnp3 = np1.reshape(2,3,2)\nprint(np3)\nprint(np3.shape)\n<\/pre><\/div>\n\n\n<p>In the above code, we provided the shape or the dimension of the new array as (2,3,2) in the <strong>reshape()<\/strong> function which means the new array should be a 3D array as we provided 3 dimensions and then we saved the result into the <strong>np3 <\/strong>variable. Lastly, we printed the <strong>np3 <\/strong>variable with its shape and we got a 3D array with its shape as (2,3,2) as expected.<\/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=\"186\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1847-1024x186.png\" alt=\"Reshaping a 1D array to 3D array\" class=\"wp-image-22482\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1847-1024x186.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1847-300x54.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1847-768x139.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1847.png 1356w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Flattening the Array<\/h2>\n\n\n\n<p>Converting a multidimensional array into a 1D array is called Flattening and it can be done by providing -1 into the <strong>reshape()<\/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=\"\">\nnp4 = np3.reshape(-1)\nprint(np4)\nprint(np4.shape)\n<\/pre><\/div>\n\n\n<p>In the above code, we provided -1 into<strong> np3.reshape()<\/strong> means it will reshape the <strong>np3<\/strong> array which is a 3D array into the 1D array and we saved this result into the <strong>np4<\/strong> variable. Lastly, we printed the <strong>np4<\/strong> array with its shape and we got a 1D array with 12 elements and the shape as (12, ) means 12 rows and 0 columns. So we came where we started from, we got our 1D array back.<\/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=\"77\" src=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1848-1024x77.png\" alt=\"Flattening the Array\" class=\"wp-image-22483\" srcset=\"https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1848-1024x77.png 1024w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1848-300x22.png 300w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1848-768x57.png 768w, https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/Screenshot-1848.png 1177w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this tutorial, we have discussed<strong> numpy.reshape()<\/strong> function provided by Python\u2019s NumPy library and explored reshaping a 1D array into a 2D or 3D array and also flattening the arrays using <strong>numpy.reshape()<\/strong> function with examples. After reading this tutorial, we hope you can easily reshape an array 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\/18691084\/what-does-1-mean-in-numpy-reshape\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/18691084\/what-does-1-mean-in-numpy-reshape<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>An array may have one or more dimensions for storing the data and sometimes we need to manipulate or change the structure of our data, i.e., the dimension of the array containing data, for this purpose, NumPy provided us with numpy.reshape() function for reshaping an array. The most common use case of reshaping the arrays [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":22473,"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-22284","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.reshape-in-Python-Reshaping-the-NumPy-Array.png",1200,800,false],"thumbnail":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.reshape-in-Python-Reshaping-the-NumPy-Array-150x150.png",150,150,true],"medium":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.reshape-in-Python-Reshaping-the-NumPy-Array-300x200.png",300,200,true],"medium_large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.reshape-in-Python-Reshaping-the-NumPy-Array-768x512.png",768,512,true],"large":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.reshape-in-Python-Reshaping-the-NumPy-Array-1024x683.png",1024,683,true],"1536x1536":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.reshape-in-Python-Reshaping-the-NumPy-Array.png",1200,800,false],"2048x2048":["https:\/\/codeforgeek.com\/wp-content\/uploads\/2023\/09\/numpy.reshape-in-Python-Reshaping-the-NumPy-Array.png",1200,800,false]},"uagb_author_info":{"display_name":"Priyanshu Singh","author_link":"https:\/\/codeforgeek.com\/author\/priyanshu\/"},"uagb_comment_info":0,"uagb_excerpt":"An array may have one or more dimensions for storing the data and sometimes we need to manipulate or change the structure of our data, i.e., the dimension of the array containing data, for this purpose, NumPy provided us with numpy.reshape() function for reshaping an array. The most common use case of reshaping the arrays&hellip;","_links":{"self":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/22284","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=22284"}],"version-history":[{"count":0,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/posts\/22284\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media\/22473"}],"wp:attachment":[{"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/media?parent=22284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/categories?post=22284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeforgeek.com\/wp-json\/wp\/v2\/tags?post=22284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}