{"id":89223,"date":"2024-09-30T18:00:11","date_gmt":"2024-09-30T12:30:11","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=89223"},"modified":"2024-09-30T19:16:58","modified_gmt":"2024-09-30T13:46:58","slug":"numpy-array-reshaping","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/","title":{"rendered":"NumPy Array Reshaping with Examples"},"content":{"rendered":"<p>NumPy is a must-have library for any Python programmer working with numerical data. It provides an extensive set of tools for working with arrays and matrices, including the necessary size and size functions.<\/p>\n<p>This exercise helps you understand and manage the dimensions of your arrays, which are essential for many everyday data science tasks.<\/p>\n<p>This beginner-friendly guide will explore the programs&#8217; size and scope in detail and show you how to use them effectively.<\/p>\n<h2>Understanding numpy.shape<\/h2>\n<p>The numpy.shape function is used to determine the shape or dimensions of an array. It returns a tuple of integers, where each element of the tuple represents the length of the corresponding array dimension.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<p><strong>a:<\/strong> Array-like input &#8211; the array whose shape you want to determine.<br \/>\nReturns:<br \/>\n<strong>Shape<\/strong>: Tuple of integers &#8211; each element represents the length of the corresponding array dimension.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/Understanding-numpy-shape.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-89598 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/Understanding-numpy-shape.webp\" alt=\"Understanding numpy shape\" width=\"400\" height=\"202\" \/><\/a><\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\n# Example 1: A 3x3 identity matrix\r\nshape = np.shape(np.eye(3))\r\n# Output: (3, 3)\r\n\r\n# Example 2: A 1x2 list\r\nshape = np.shape([[1, 3]])\r\n# Output: (1, 2)\r\n\r\n# Example 3: A single-element list\r\nshape = np.shape([0])\r\n# Output: (1,)\r\n\r\n# Example 4: An array with structured data\r\na = np.array([(1, 2), (3, 4), (5, 6)],\r\n             dtype=[('x', 'i4'), ('y', 'i4')])\r\nshape = np.shape(a)\r\n# Output: (3,)<\/pre>\n<p>In the above examples, you can see how numpy.shape determines the shape of different arrays. It returns a tuple representing the dimensions of the input arrays.<\/p>\n<h3>Reshaping Arrays with numpy.reshape<\/h3>\n<p>The numpy.reshape function allows you to give a new shape to an array without changing its data. You can use this function to rearrange the elements of an array into a different shape as long as the total number of elements remains the same.<\/p>\n<p><strong>Parameters<\/strong>:<\/p>\n<p><strong>a:<\/strong> Array-like input &#8211; the array to be reshaped.<br \/>\n<strong>newshape:<\/strong> Integer or tuple of integers &#8211; the desired new shape for the array.<br \/>\n<strong>order (optional):<\/strong> Specifies the index order for reading and placing elements. Options are &#8216;C&#8217; (C-like), &#8216;F&#8217; (Fortran-like), or &#8216;A&#8217; (depends on memory layout).<br \/>\n<strong>Returns: <\/strong>reshaped_array: ndarray &#8211; the reshaped array. This may be a new view object or a copy.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/Reshaping-Arrays-with-numpy-reshape-1.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-89604 size-full\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/11\/Reshaping-Arrays-with-numpy-reshape-1.webp\" alt=\"Reshaping Arrays with numpy reshape\" width=\"400\" height=\"377\" \/><\/a><\/p>\n<p><strong>Examples:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import numpy as np\r\n\r\n# Example 1: Reshaping a 2D array from (3, 2) to (2, 3)\r\na = np.arange(6).reshape((3, 2))\r\n# Output:\r\n# array([[0, 1],\r\n#        [2, 3],\r\n#        [4, 5]])\r\n\r\n# Example 2: Reshaping using 'order' parameter\r\nb = np.reshape(a, (2, 3), order='F')  # Fortran-like index ordering\r\n# Output:\r\n# array([[0, 4, 3],\r\n#        [2, 1, 5]])\r\n\r\n# Example 3: Reshaping with an inferred dimension\r\nc = np.reshape(a, (3, -1))  # Inferred value for the second dimension\r\n# Output:\r\n# array([[0, 1],\r\n#        [2, 3],\r\n#        [4, 5]])<\/pre>\n<p>In these examples, numpy.reshape is used to change the shape of arrays while keeping the data intact. You can specify the new shape or let NumPy infer it based on the original array&#8217;s size.<\/p>\n<h3>Here&#8217;s a brief recap of what we&#8217;ve covered:<\/h3>\n<p><strong>numpy.shape:<\/strong> This function allows us to retrieve the shape or dimensions of an array. It returns a tuple of integers, where each element of the tuple represents the length of the corresponding array dimension. Understanding the shape of your data is crucial for many data manipulation tasks.<\/p>\n<p><strong>numpy.reshape:<\/strong> With this function, you can give a new shape to an array without changing its data. You can specify the new shape as an integer or a tuple of integers. If you don&#8217;t provide a value for one of the dimensions, NumPy can infer it based on the size of the array. This function is indispensable for preparing data for various analytical tasks.<\/p>\n<h3>Conclusion<\/h3>\n<p>As you continue your journey into data science and numerical computing, mastering NumPy&#8217;s array manipulation capabilities, including shape and reshape, will be invaluable. They provide a solid foundation for more advanced tasks and analyses. So, whether you&#8217;re crunching numbers, visualizing data, or building machine learning models, NumPy&#8217;s shape and reshape functions will be your reliable companions. Happy coding and data exploring!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NumPy is a must-have library for any Python programmer working with numerical data. It provides an extensive set of tools for working with arrays and matrices, including the necessary size and size functions. This&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":447261,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[385],"tags":[5677,383,412,5675,5676,413,384],"class_list":["post-89223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy-tutorials","tag-array-reshaping-in-numpy","tag-learn-numpy","tag-numpy-array","tag-numpy-array-reshaping","tag-numpy-array-reshaping-with-examples","tag-numpy-array-shape-reshape","tag-numpy-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>NumPy Array Reshaping with Examples - TechVidvan<\/title>\n<meta name=\"description\" content=\"NumPy&#039;s array manipulation capabilities, including shape and reshape, will be an invaluable skill. These functions will be your reliable companions.\" \/>\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-array-reshaping\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NumPy Array Reshaping with Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"NumPy&#039;s array manipulation capabilities, including shape and reshape, will be an invaluable skill. These functions will be your reliable companions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/\" \/>\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-09-30T12:30:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-09-30T13:46:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/11\/numpy-array-shape-reshape.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 Array Reshaping with Examples - TechVidvan","description":"NumPy's array manipulation capabilities, including shape and reshape, will be an invaluable skill. These functions will be your reliable companions.","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-array-reshaping\/","og_locale":"en_US","og_type":"article","og_title":"NumPy Array Reshaping with Examples - TechVidvan","og_description":"NumPy's array manipulation capabilities, including shape and reshape, will be an invaluable skill. These functions will be your reliable companions.","og_url":"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2024-09-30T12:30:11+00:00","article_modified_time":"2024-09-30T13:46:58+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/11\/numpy-array-shape-reshape.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-array-reshaping\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/dde481bb412350cde1ed6e389bc0deaf"},"headline":"NumPy Array Reshaping with Examples","datePublished":"2024-09-30T12:30:11+00:00","dateModified":"2024-09-30T13:46:58+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/"},"wordCount":515,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/11\/numpy-array-shape-reshape.webp","keywords":["array reshaping in numpy","learn numpy","numPy array","numpy array reshaping","numpy array reshaping with examples","numpy array shape reshape","numPy tutorial"],"articleSection":["NumPy Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/","url":"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/","name":"NumPy Array Reshaping with Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/11\/numpy-array-shape-reshape.webp","datePublished":"2024-09-30T12:30:11+00:00","dateModified":"2024-09-30T13:46:58+00:00","description":"NumPy's array manipulation capabilities, including shape and reshape, will be an invaluable skill. These functions will be your reliable companions.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/11\/numpy-array-shape-reshape.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/11\/numpy-array-shape-reshape.webp","width":1200,"height":628,"caption":"numpy array shape reshape"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/numpy-array-reshaping\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"NumPy Array Reshaping with Examples"}]},{"@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\/89223","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=89223"}],"version-history":[{"count":4,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89223\/revisions"}],"predecessor-version":[{"id":447719,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/89223\/revisions\/447719"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/447261"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=89223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=89223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=89223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}