{"id":1522,"date":"2024-01-14T00:00:00","date_gmt":"2024-01-14T05:00:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=1522"},"modified":"2024-01-26T15:11:25","modified_gmt":"2024-01-26T20:11:25","slug":"python-numpy-reshape-examples","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/python-numpy-reshape-examples\/","title":{"rendered":"Python: NumPy reshape examples"},"content":{"rendered":"\n<p>In NumPy, the <code>reshape<\/code> function is used to change the shape of an array without changing its data. Here are some examples of using <code>reshape<\/code> with different scenarios:<\/p>\n\n\n\n<p><strong>Example 1: Basic Reshape<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\n\n# Create a 1D array\narr_1d = np.array(&#91;1, 2, 3, 4, 5, 6])\n\n# Reshape to a 2D array with 2 rows and 3 columns\narr_2d = arr_1d.reshape((2, 3))\n\nprint(\"Original 1D array:\")\nprint(arr_1d)\nprint(\"\\nReshaped 2D array:\")\nprint(arr_2d)<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original 1D array:\r\n&#91;1 2 3 4 5 6]\r\n\r\nReshaped 2D array:\r\n&#91;&#91;1 2 3]\r\n &#91;4 5 6]]\r<\/code><\/pre>\n\n\n\n<p><strong>Example 2: Reshape with -1<\/strong><\/p>\n\n\n\n<p>Using <code>-1<\/code> as one of the dimensions allows NumPy to automatically calculate the size of that dimension based on the size of the original array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\r\n\r\n# Create a 1D array with 12 elements\r\narr_1d = np.arange(1, 13)\r\n\r\n# Reshape to a 2D array with 3 rows and an automatically calculated number of columns\r\narr_2d = arr_1d.reshape((3, -1))\r\n\r\nprint(\"Original 1D array:\")\r\nprint(arr_1d)\r\nprint(\"\\nReshaped 2D array:\")\r\nprint(arr_2d)\r<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original 1D array:\r\n&#91; 1  2  3  4  5  6  7  8  9 10 11 12]\r\n\r\nReshaped 2D array:\r\n&#91;&#91; 1  2  3  4]\r\n &#91; 5  6  7  8]\r\n &#91; 9 10 11 12]]\r<\/code><\/pre>\n\n\n\n<p><strong>Example 3: Reshape for 3D Array<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\r\n\r\n# Create a 1D array with 24 elements\r\narr_1d = np.arange(1, 25)\r\n\r\n# Reshape to a 3D array with 2 planes, 3 rows, and 4 columns\r\narr_3d = arr_1d.reshape((2, 3, 4))\r\n\r\nprint(\"Original 1D array:\")\r\nprint(arr_1d)\r\nprint(\"\\nReshaped 3D array:\")\r\nprint(arr_3d)\r<\/code><\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Original 1D array:\r\n&#91; 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]\r\n\r\nReshaped 3D array:\r\n&#91;&#91;&#91; 1  2  3  4]\r\n  &#91; 5  6  7  8]\r\n  &#91; 9 10 11 12]]\r\n\r\n &#91;&#91;13 14 15 16]\r\n  &#91;17 18 19 20]\r\n  &#91;21 22 23 24]]]\r<\/code><\/pre>\n\n\n\n<p>These examples showcase different use cases for reshaping arrays using the <code>reshape<\/code> function in NumPy. Adjust the dimensions and content of the arrays based on your specific requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In NumPy, the reshape function is used to change the shape of an array without changing its data. Here are some examples of using reshape with different scenarios: Example 1: Basic Reshape Output: Example 2: Reshape with -1 Using -1 as one of the dimensions allows NumPy to automatically calculate the size of that dimension [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1573,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203],"tags":[208,137],"class_list":["post-1522","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-numpy","tag-python"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1522","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=1522"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1522\/revisions"}],"predecessor-version":[{"id":1523,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/1522\/revisions\/1523"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media\/1573"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=1522"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=1522"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=1522"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}