{"id":15007,"date":"2021-10-20T04:25:49","date_gmt":"2021-10-20T04:25:49","guid":{"rendered":"https:\/\/machinelearningplus.com\/?p=15007"},"modified":"2022-03-08T15:36:01","modified_gmt":"2022-03-08T15:36:01","slug":"numpy-reshape","status":"publish","type":"post","link":"https:\/\/machinelearningplus.com\/python\/numpy-reshape\/","title":{"rendered":"Numpy Reshape &#8211; How to reshape arrays and what does -1 mean?"},"content":{"rendered":"\r\n<h2 class=\"wp-block-heading\" id=\"how-to-use-numpy-reshape-function-\">How to reshape a numpy array?<\/h2>\r\n\r\n\r\n\r\n<p>The <code>numpy.reshape()<\/code> function is used to reshape a numpy array without changing the data in the array. It is a very common practice to reshape arrays to make them compatible for further calculations.<\/p>\r\n\r\n\r\n\r\n<p>In this article, you will learn about the possible use cases of the <code>numpy.reshape<\/code> function.<\/p>\r\n\r\n<h2 class=\"wp-block-heading\">numpy.reshape<\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><strong>Syntax:<\/strong> numpy.reshape(a, newshape, order=&#8217;C&#8217;)<\/li>\r\n<li><strong>Purpose: <\/strong>Gives a new shape to the array without changing the data<\/li>\r\n<li><strong>Parameters:<\/strong>\r\n<ul>\r\n<li><strong>a:<\/strong> _array<em>like<\/em> Array to be reshaped<\/li>\r\n<li><strong>newshape:<\/strong><em>int or tuples of ints<\/em> Should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.<\/li>\r\n<li><strong>order:<\/strong><em>{&#8216;C&#8217;, &#8216;F&#8217;, &#8216;A&#8217;}, optional<\/em> Read the elements of a using this index order, and place the elements into the reshaped array using this index order. Detailed usage will be discussed further.<\/li>\r\n<\/ul>\r\n<\/li>\r\n<li><strong>Returns<\/strong> reshaped_array <em>ndarray<\/em><\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code><span class=\"hljs-meta\"># Import Packages<\/span>\r\n<span class=\"hljs-keyword\">import<\/span> numpy <span class=\"hljs-keyword\">as<\/span> np<\/code><\/pre>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"1-numpy-reshape-function\">1. Numpy.reshape function<\/h2>\r\n\r\n\r\n\r\n<p>The <code>numpy.reshape()<\/code> function is used to change the shape of the numpy array without modifying the array data. To use this function, pass the array and the new shape to <code>np.reshape()<\/code>. The shape argument should be passed in the form either &#8220;tuple&#8221; or &#8220;int&#8221;.<\/p>\r\n\r\n\r\n\r\n<p>Let&#8217;s understand this with examples.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"reshaping-1-d-array-into-a-2-d-array\">Reshaping 1-D array into a 2-D array<\/h3>\r\n\r\n\r\n\r\n<p>In this example, you have to transform a 1-dimensional array of shape (8,) to 2-dimensional array of shape (4,2).<\/p>\r\n\r\n\r\n\r\n<p><strong>Step 1: Create a numpy array of shape (8,)<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>num_array = np.array([<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">5<\/span>,<span class=\"hljs-number\">6<\/span>,<span class=\"hljs-number\">7<\/span>,<span class=\"hljs-number\">8<\/span>])\r\nnum_array<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>])<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Step 2: Use <code>np.reshape()<\/code> function with new shape as <code>(4,2)<\/code><\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>np.reshape(num_array, (<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">2<\/span>))\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([[<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>],\r\n       [<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>],\r\n       [<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>],\r\n       [<span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>]])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>As you can see the shape of the input array has been changed to a (4,2). This is a 2-D array and contains the smae data present in the original input 1-D array<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"reshaping-2-d-array-into-a-3-d-array\">Reshaping 2-D array into a 3-D array<\/h3>\r\n\r\n\r\n\r\n<p>In this example, you have to transform a2-dimensional array of shape (4,2) to 3-dimensional array of shape (2,2,2).<\/p>\r\n\r\n\r\n\r\n<p><strong>Step 1: Create a numpy array of shape (4,2)<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>num_array = np.array([[<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>],\r\n                      [<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>],\r\n                      [<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>],\r\n                      [<span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>]])\r\n\r\nnum_array\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([[<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>],\r\n       [<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>],\r\n       [<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>],\r\n       [<span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>]])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Step 2: Use <code>np.reshape()<\/code> function with new shape as <code>(2, 2, 2)<\/code><\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>np.reshape(num_array, (<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">2<\/span>))\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array(<span class=\"hljs-string\">[[[1, 2],\r\n        [3, 4]]<\/span>,\r\n\r\n       <span class=\"hljs-string\">[[5, 6],\r\n        [7, 8]]<\/span>])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>As you can see the shape of the input array has been changed to a (2, 2, 2). This is a 3-D array and contains the smae data present in the original input 2-D array.<\/p>\r\n\r\n<h2 class=\"wp-block-heading\" id=\"2-can-you-reshape-the-array-into-any-shape-\">2. Can you reshape the numpy array into any shape?<\/h2>\r\n\r\n\r\n\r\n<p>The <code>np.reshape()<\/code> function returns the transformed array with the new shape provided in the function. The only condition is that the number of elements in the original array and the number of elements in the transformed array should be equal.<\/p>\r\n\r\n\r\n\r\n<p>If you don&#8217;t know how to find out the number of elements in an array, simply multiply the number of elements per axis\/dimension. It simply means multiplication of all the numbers mentioned in the shape tuple.<\/p>\r\n\r\n\r\n\r\n<p>&nbsp;<\/p>\r\n\r\n\r\n\r\n<p>Let&#8217;s see what happens if you try to reshape an array with unequal elements<\/p>\r\n\r\n\r\n\r\n<p><strong>Step 1: Create a numpy array of shape (5,)<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>a = np.array([<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">5<\/span>])\r\na\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Step 2: Use <code>np.reshape()<\/code> function with new shape as <code>(2,5)<\/code><\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code><span class=\"hljs-comment\">np.reshape(a, (2,5))<\/span>   \r\n\r\n#&gt; Throws ValueError\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>In this case, a <code>ValueError<\/code> exception is raised. The problem here is that the original array has 5 elements. But the new shape, defined as <code>(2,5)<\/code>, expects 2&#215;5=10 elements. There is a mismatch of the number of elements and therefore, the code failed.<\/p>\r\n\r\n\r\n\r\n<p>But what if you wish to reshape to an unknown dimension?<\/p>\r\n\r\n\r\n\r\n<p>You can use -1 for the unknown dimension.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"3-reshape-using-unknown-dimension\">3. What is the meaning of -1 in numpy.reshape?<\/h2>\r\n\r\n\r\n\r\n<p>The <code>np.reshape()<\/code> function supports powerful usage of unknown dimensions or placeholder <code>(-1)<\/code>.<\/p>\r\n\r\n\r\n\r\n<p>While defining a new shape, you can put one of the dimensions as unknown. Numpy will automatically infer the right dimension for that particular shape. This is to ensure that the input and output arrays have the same number of elements.<\/p>\r\n\r\n\r\n\r\n<p>It can be useful in cases when the exact dimensions of the input array is not known but some of the output dimensions are known. Let&#8217;s see an example where the input array dimensions are not known but 2 rows are required in the output array.<\/p>\r\n\r\n\r\n\r\n<p><strong>Step 1: Create a numpy array<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>num_array = np.array(<span class=\"hljs-string\">[[[1, 2], [3, 4]]<\/span>, <span class=\"hljs-string\">[[5, 6], [7, 8]]<\/span>])\r\nnum_array\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array(<span class=\"hljs-string\">[[[1, 2],\r\n        [3, 4]]<\/span>,\r\n\r\n       <span class=\"hljs-string\">[[5, 6],\r\n        [7, 8]]<\/span>])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Step 2: Use <code>np.reshape()<\/code> function with new shape as <code>(2,-1)<\/code><\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>np.reshape(num_array, (<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">-1<\/span>))\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([[<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>],\r\n       [<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>]])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>The given input array is a 3-D array with shape (2,2,2).<\/p>\r\n\r\n\r\n\r\n<p>So what does the -1 in (2, -1) mean?<\/p>\r\n\r\n\r\n\r\n<p>The -1 informs numpy to automatically infer the dimension of that axis. So, on applying <code>np.reshape()<\/code> function for shape <code>(2,-1)<\/code>, Numpy is able to infer the last dimension as 4 automatically.<\/p>\r\n\r\n\r\n\r\n<p>But what happens if you don&#8217;t even put the 1 in the dimension of the output array and simply use just -1 instead?<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"4-flatten-the-arrays\">4. Flatten the arrays<\/h2>\r\n\r\n\r\n\r\n<p>This is an extended use case of using unknown dimensions for reshaping numpy arrays. Unknown dimensions placeholder <code>(-1)<\/code> allows the dimensions to be inferred automatically by numpy. This trick can be used to flatten an array. If <code>(-1)<\/code> placeholder is placed in the <code>np.reshape()<\/code> function, then the function returns a flatten array,<\/p>\r\n\r\n\r\n\r\n<p>Let&#8217;s see an example below.<\/p>\r\n\r\n\r\n\r\n<p><strong>Step 1: Create a 3-D numpy array<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>a_3d_array = np.array(<span class=\"hljs-string\">[[[1, 2], [3, 4]]<\/span>, <span class=\"hljs-string\">[[5, 6], [7, 8]]<\/span>])\r\na_3d_array\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array(<span class=\"hljs-string\">[[[1, 2],\r\n        [3, 4]]<\/span>,\r\n\r\n       <span class=\"hljs-string\">[[5, 6],\r\n        [7, 8]]<\/span>])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Step 2: Use <code>np.reshape()<\/code> function with new shape as <code>(-1)<\/code><\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>np.reshape(<span class=\"hljs-built_in\">a_3d<\/span>_array, (-<span class=\"hljs-number\">1<\/span>))\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>Here, a 3-D array with shape <code>(2,2,2)<\/code> is flattened to a 1-D array.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"5-change-the-order-of-reshape\">5. How to change the ordering of numpy reshape process?<\/h2>\r\n\r\n\r\n\r\n<p>On an abstract level, the <code>np.reshape()<\/code> function works in such a way that it starts with an unrolling or flattening process. This is where all the elements of the input array are flattened into a 1-D array and then rolled back or reshaped according to the input shape provided. But how does numpy decide which dimension element would be flattened first and in which order the elements would be put back? And what if you wish to change this order?<\/p>\r\n\r\n\r\n\r\n<p>This order of unrolling can be controlled using the <code>order<\/code> parameter in the <code>np.reshape()<\/code> function. This parameter can take 3 values:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><code>C<\/code>: C-like order index<\/li>\r\n<li><code>F<\/code>: Fortran-like order index<\/li>\r\n<li><code>A<\/code>: Either in C order or in Fortran order<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>Let&#8217;s discuss each of them.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"c-like-order-index\">C-like order index<\/h3>\r\n\r\n\r\n\r\n<p>C here stands for the C-language and this order is known as the C-like order index. According to this order, the last index or dimension of the array changes the fastest, and the first index changes the slowest.<\/p>\r\n\r\n\r\n\r\n<p>In simple terms, the unrolling starts with the last dimension elements and then proceeds towards the first dimension elements. This ordering is maintained for the rolling back process too when the output array is being created. It is also the default value in <code>np.reshape()<\/code> function.<\/p>\r\n\r\n\r\n\r\n<p>Let&#8217;s see an example below.<\/p>\r\n\r\n\r\n\r\n<p><strong>Step 1: Create a 2-D numpy array<\/strong><\/p>\r\n\r\n\r\n\r\n<p>For this case, let&#8217;s create a special array. The elements of this 2-D array will correspond to the respective row and column numbers. For example, the element &#8220;R1C2&#8221; represents elements in the 1st row and 2nd column.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>a_2d_array = np.array([[<span class=\"hljs-string\">'R1C1'<\/span>, <span class=\"hljs-string\">'R1C2'<\/span>], [<span class=\"hljs-string\">'R2C1'<\/span>, <span class=\"hljs-string\">'R2C2'<\/span>], [<span class=\"hljs-string\">'R3C1'<\/span>, <span class=\"hljs-string\">'R3C2'<\/span>]])\r\na_2d_array\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([[<span class=\"hljs-string\">'R1C1'<\/span>, <span class=\"hljs-string\">'R1C2'<\/span>],\r\n       [<span class=\"hljs-string\">'R2C1'<\/span>, <span class=\"hljs-string\">'R2C2'<\/span>],\r\n       [<span class=\"hljs-string\">'R3C1'<\/span>, <span class=\"hljs-string\">'R3C2'<\/span>]], dtype=<span class=\"hljs-string\">'&lt;U4'<\/span>)\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Step 2: Use <code>np.reshape()<\/code> function with new shape as <code>(2,3)<\/code> and order <code>C<\/code><\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>np.reshape(a_2d_array, (<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>), <span class=\"hljs-keyword\">order<\/span>=<span class=\"hljs-string\">'C'<\/span>)\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([[<span class=\"hljs-string\">'R1C1'<\/span>, <span class=\"hljs-string\">'R1C2'<\/span>, <span class=\"hljs-string\">'R2C1'<\/span>],\r\n       [<span class=\"hljs-string\">'R2C2'<\/span>, <span class=\"hljs-string\">'R3C1'<\/span>, <span class=\"hljs-string\">'R3C2'<\/span>]], dtype=<span class=\"hljs-string\">'&lt;U4'<\/span>)\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>The output above clearly indicates that in the last dimension of the input 2-D array, columns were flattened first. The elements were flattened in the order &#8220;R1C1&#8221;, &#8220;R1C2&#8221;, &#8220;R2C1&#8221;, and so on. Then during the reshaping process, &#8220;R1C1&#8221; was placed in 1st row, 1st col, &#8220;R1C2&#8221; was placed in 1st row, 2nd column, and &#8220;R2C1&#8221; was placed in 1st row, 3rd column.<\/p>\r\n\r\n\r\n\r\n<p>&#8220;R2C1&#8221; was placed in such a manner so that the output array shape becomes compatible with the input array shape.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"fortran-like-order-index\">Fortran-like order index<\/h3>\r\n\r\n\r\n\r\n<p>F here stands for the language Fortran. Here, the first index or dimension changes the fastest, and the subsequent index changes the slowest. In other words, unrolling process starts with the first dimension and then it proceeds towards the last dimension. This ordering is maintained for the rolling back process too.<\/p>\r\n\r\n\r\n\r\n<p>Let&#8217;s see an example below.<\/p>\r\n\r\n\r\n\r\n<p><strong>Step 1: Create a 2-D numpy array<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code># <span class=\"hljs-keyword\">Using<\/span> the same <span class=\"hljs-keyword\">array<\/span> created <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-string\">'C'<\/span> <span class=\"hljs-keyword\">order<\/span>\r\n\r\na_2d_array\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([[<span class=\"hljs-string\">'R1C1'<\/span>, <span class=\"hljs-string\">'R1C2'<\/span>],\r\n       [<span class=\"hljs-string\">'R2C1'<\/span>, <span class=\"hljs-string\">'R2C2'<\/span>],\r\n       [<span class=\"hljs-string\">'R3C1'<\/span>, <span class=\"hljs-string\">'R3C2'<\/span>]], dtype=<span class=\"hljs-string\">'&lt;U4'<\/span>)\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Step 2: Use <code>np.reshape()<\/code> function with new shape as <code>(2,3)<\/code> and order <code>F<\/code><\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>np.reshape(a_2d_array, (<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>), <span class=\"hljs-keyword\">order<\/span>=<span class=\"hljs-string\">'F'<\/span>)\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([[<span class=\"hljs-string\">'R1C1'<\/span>, <span class=\"hljs-string\">'R3C1'<\/span>, <span class=\"hljs-string\">'R2C2'<\/span>],\r\n       [<span class=\"hljs-string\">'R2C1'<\/span>, <span class=\"hljs-string\">'R1C2'<\/span>, <span class=\"hljs-string\">'R3C2'<\/span>]], dtype=<span class=\"hljs-string\">'&lt;U4'<\/span>)\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p>The output above depicts that in the first dimension of the input 2-D array, rows were flattened first. The elements were flattened in the order &#8220;R1C1&#8221;, &#8220;R2C1&#8221;, &#8220;R3C1&#8221;, and so on. Then during the reshaping process, &#8220;R1C1&#8221; was placed in 1st row, 1st column, &#8220;R2C1&#8221; was placed in 2nd row, 1st column, and &#8220;R3C1&#8221; was placed in 1st row, 2nd column.<\/p>\r\n\r\n\r\n\r\n<p>&#8220;R3C1&#8221; was placed in such a manner so that the output array shape becomes compatible with the input array shape.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"a-order\">A order<\/h3>\r\n\r\n\r\n\r\n<p>This type of order does not have any specific rules. It depends on how the array is stored in the memory. If the array is stored in a C-like memory, then the <code>C<\/code> order is used and if the array is stored as Fortran-like memory, then the <code>F<\/code> order is used. A user is not aware of what the output result would be and that&#8217;s why this ordering is rarely used.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"6-alternate-ways-of-reshaping-arrays\">6. Alternate ways of reshaping arrays<\/h2>\r\n\r\n\r\n\r\n<p>Although, a numpy array can be reshaped using <code>np.reshape()<\/code> function but, there are some alternate methods. Two such methods are:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Numpy array object function<\/li>\r\n<li>Using <code>np.ravel()<\/code> in combination with <code>np.reshape()<\/code><\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>Let&#8217;s explore these methods.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"numpy-array-object-function-for-reshaping-arrays\">Numpy array object function for reshaping arrays<\/h3>\r\n\r\n\r\n\r\n<p>A numpy array object supports almost all the operations that can be performed using the numpy explicit functions. The numpy array can be reshaped by accessing the <code>.reshape()<\/code> function from the numpy array object. See can example below.<\/p>\r\n\r\n\r\n\r\n<p><strong>Step 1: Create a numpy array of shape (8,)<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>num_array = np.array([<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">5<\/span>,<span class=\"hljs-number\">6<\/span>,<span class=\"hljs-number\">7<\/span>,<span class=\"hljs-number\">8<\/span>])\r\nnum_array\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Step 2: Use <code>.reshape()<\/code> function from numpy array object with new shape as <code>(4,2)<\/code><\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>num_array.reshape((<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">2<\/span>))\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([[<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>],\r\n       [<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>],\r\n       [<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>],\r\n       [<span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>]])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"using-np-ravel-in-combination-with-np-reshape-for-reshaping-arrays\">Using np.ravel() in combination with np.reshape() for reshaping arrays<\/h3>\r\n\r\n\r\n\r\n<p><code>np.ravel()<\/code> function is used for flattening the numpy arrays. It returns the multidimensional array as a flattened contiguous array. This function can be used in combination with <code>np.reshape()<\/code> function. The result of the ravel function can be passed into the reshape function with a new shape defined and it will still return the correct results.<\/p>\r\n\r\n\r\n\r\n<p>Let&#8217;s see an example.<\/p>\r\n\r\n\r\n\r\n<p><strong>Step 1: Create a numpy array of shape (8,)<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>num_array = np.array([<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">5<\/span>,<span class=\"hljs-number\">6<\/span>,<span class=\"hljs-number\">7<\/span>,<span class=\"hljs-number\">8<\/span>])\r\nnum_array\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Step 2: Use <code>np.reshape()<\/code> and <code>np.ravel()<\/code> function with new shape as <code>(4,2)<\/code><\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code><span class=\"hljs-selector-tag\">np<\/span><span class=\"hljs-selector-class\">.reshape<\/span>(<span class=\"hljs-selector-tag\">np<\/span><span class=\"hljs-selector-class\">.ravel<\/span>(<span class=\"hljs-selector-tag\">num_array<\/span>), (4,2))\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([[<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>],\r\n       [<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>],\r\n       [<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>],\r\n       [<span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>]])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"7-advanced-reshaping\">7. Advanced reshaping<\/h2>\r\n\r\n\r\n\r\n<p>One of the alternative ways to reshape a numpy array, as mentioned in the above section, is to flat the array using <code>np.ravel()<\/code> function. Then use the output of ravel function as input for <code>np.reshape()<\/code> function along with the new shape for the final output array.<\/p>\r\n\r\n\r\n\r\n<p><code>np.ravel()<\/code> also supports <code>order<\/code> parameter and it works the same way as in the <code>np.reshape()<\/code> function. Therefore, one can have a different order for flattening and reshaping. Let&#8217;s discuss these cases.<\/p>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"case-1-flattening-in-c-order-reshaping-in-f-order\">Case 1: Flattening in C order, reshaping in F order<\/h3>\r\n\r\n\r\n\r\n<p>In this case, the array is flattened using the <code>np.ravel()<\/code> function along with <code>order<\/code> parameter <code>C<\/code>. For the <code>np.reshape()<\/code> function, the order parameter would be <code>F<\/code>.<\/p>\r\n\r\n\r\n\r\n<p><strong>Step 1: Create a numpy array of shape (8,)<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>num_array = np.array([<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">5<\/span>,<span class=\"hljs-number\">6<\/span>,<span class=\"hljs-number\">7<\/span>,<span class=\"hljs-number\">8<\/span>])\r\nnum_array\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Step 2: Use <code>np.reshape()<\/code> and <code>np.ravel()<\/code> function with new shape as <code>(4,2)<\/code><\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>np.reshape(np.ravel(num_array, <span class=\"hljs-keyword\">order<\/span>=<span class=\"hljs-string\">'C'<\/span>), (<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">2<\/span>), <span class=\"hljs-keyword\">order<\/span>=<span class=\"hljs-string\">'F'<\/span>)\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([[<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">5<\/span>],\r\n       [<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">6<\/span>],\r\n       [<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">7<\/span>],\r\n       [<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">8<\/span>]])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\" id=\"case-2-flattening-in-f-order-reshaping-in-c-order\">Case 2: Flattening in F order, reshaping in C order<\/h3>\r\n\r\n\r\n\r\n<p>In this case, the array is flattened using the <code>np.ravel()<\/code> function along with <code>order<\/code> parameter <code>F<\/code>. For the <code>np.reshape()<\/code> function, the order parameter would be <code>C<\/code>.<\/p>\r\n\r\n\r\n\r\n<p><strong>Step 1: Create a numpy array of shape (8,)<\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>num_array = np.array([<span class=\"hljs-number\">1<\/span>,<span class=\"hljs-number\">2<\/span>,<span class=\"hljs-number\">3<\/span>,<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">5<\/span>,<span class=\"hljs-number\">6<\/span>,<span class=\"hljs-number\">7<\/span>,<span class=\"hljs-number\">8<\/span>])\r\nnum_array\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<p><strong>Step 2: Use <code>np.reshape()<\/code> and <code>np.ravel()<\/code> function with new shape as <code>(4,2)<\/code><\/strong><\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>np.reshape(np.ravel(num_array, <span class=\"hljs-keyword\">order<\/span>=<span class=\"hljs-string\">'F'<\/span>), (<span class=\"hljs-number\">4<\/span>,<span class=\"hljs-number\">2<\/span>), <span class=\"hljs-keyword\">order<\/span>=<span class=\"hljs-string\">'C'<\/span>)\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>array([[<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>],\r\n       [<span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>],\r\n       [<span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">6<\/span>],\r\n       [<span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">8<\/span>]])\r\n<\/code><\/pre>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id=\"8-test-your-knowledge\">8. Test your knowledge<\/h2>\r\n\r\n\r\n\r\n<p><strong>Q1:<\/strong> What if a negative index is passed in the shape tuple? (Assume only one negative index)<\/p>\r\n\r\n\r\n\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Answer:<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n\r\n\r\n\r\n<p><strong>Answer:<\/strong> The numpy will automatically infer the -1 as a missing dimension and choose the correct dimension of its own<\/p>\r\n\r\n\r\n\r\n<p><\/div><\/details>\r\n\r\n\r\n\r\n<p><strong>Q2:<\/strong> What is the difference between the &#8216;C&#8217; and &#8216;F&#8217; order?<\/p>\r\n\r\n\r\n\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Answer:<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n\r\n\r\n\r\n<p><strong>Answer:<\/strong> In &#8216;C&#8217; order, the last index or dimension of the array changes the fastest, and the first index changes the slowest. But in &#8216;F&#8217; order, the first index or dimension changes the fastest, and the subsequent index changes the slowest.<\/p>\r\n\r\n\r\n\r\n<p><\/div><\/details>\r\n\r\n\r\n\r\n<p><strong>Q3:<\/strong> List two ways to flatten a numpy array.<\/p>\r\n\r\n\r\n\r\n<details class=\"blogv4-expand\"><summary class=\"blogv4-expand__toggle\">Answer:<\/summary><div class=\"blogv4-expand__body\"><\/p>\r\n\r\n\r\n\r\n<p><strong>Answer:<\/strong><\/p>\r\n\r\n\r\n\r\n<p>1) Using <code>np.ravel()<\/code> function<\/p>\r\n\r\n\r\n\r\n<p>2) Using unknown dimension placeholder <code>-1<\/code> in <code>np.shape()<\/code> function<\/p>\r\n\r\n\r\n\r\n<p><\/div><\/details>\r\n\r\n\r\n\r\n<p>The Article was contributed by Kaustubh G.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>How to reshape a numpy array? The numpy.reshape() function is used to reshape a numpy array without changing the data in the array. It is a very common practice to reshape arrays to make them compatible for further calculations.<\/p>\n","protected":false},"author":28,"featured_media":15456,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[21],"tags":[26,22],"class_list":["post-15007","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-numpy","tag-python"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Numpy Reshape - How to reshape arrays and what does -1 mean? - machinelearningplus<\/title>\n<meta name=\"description\" content=\"The numpy.reshape() is used to reshape a numpy array without changing the data in the array. Read on to know all about reshaping numpy arrays.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/localhost:8080\/python\/numpy-reshape\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Numpy Reshape - How to reshape arrays and what does -1 mean? - machinelearningplus\" \/>\n<meta property=\"og:description\" content=\"The numpy.reshape() is used to reshape a numpy array without changing the data in the array. Read on to know all about reshaping numpy arrays.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/localhost:8080\/python\/numpy-reshape\/\" \/>\n<meta property=\"og:site_name\" content=\"machinelearningplus\" \/>\n<meta property=\"article:published_time\" content=\"2021-10-20T04:25:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-08T15:36:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/localhost:8080\/wp-content\/uploads\/2021\/10\/Numpy_Feature_Image-min.png\" \/>\n\t<meta property=\"og:image:width\" content=\"560\" \/>\n\t<meta property=\"og:image:height\" content=\"315\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"MachineLearningPlus\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"MachineLearningPlus\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/numpy-reshape\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/numpy-reshape\\\/\"},\"author\":{\"name\":\"MachineLearningPlus\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/19596ae889d256d2760a8986abfa5c47\"},\"headline\":\"Numpy Reshape &#8211; How to reshape arrays and what does -1 mean?\",\"datePublished\":\"2021-10-20T04:25:49+00:00\",\"dateModified\":\"2022-03-08T15:36:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/numpy-reshape\\\/\"},\"wordCount\":1885,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/numpy-reshape\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2021\\\/10\\\/Numpy_Feature_Image-min.png\",\"keywords\":[\"Numpy\",\"Python\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/localhost:8080\\\/python\\\/numpy-reshape\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/numpy-reshape\\\/\",\"url\":\"https:\\\/\\\/localhost:8080\\\/python\\\/numpy-reshape\\\/\",\"name\":\"Numpy Reshape - How to reshape arrays and what does -1 mean? - machinelearningplus\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/numpy-reshape\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/numpy-reshape\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2021\\\/10\\\/Numpy_Feature_Image-min.png\",\"datePublished\":\"2021-10-20T04:25:49+00:00\",\"dateModified\":\"2022-03-08T15:36:01+00:00\",\"description\":\"The numpy.reshape() is used to reshape a numpy array without changing the data in the array. Read on to know all about reshaping numpy arrays.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/localhost:8080\\\/python\\\/numpy-reshape\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/python\\\/numpy-reshape\\\/#primaryimage\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2021\\\/10\\\/Numpy_Feature_Image-min.png\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2021\\\/10\\\/Numpy_Feature_Image-min.png\",\"width\":560,\"height\":315,\"caption\":\"Numpy Feature Image\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"name\":\"machinelearningplus\",\"description\":\"Learn Data Science (AI \\\/ ML) Online\",\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/machinelearningplus.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\",\"name\":\"machinelearningplus\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"width\":348,\"height\":36,\"caption\":\"machinelearningplus\"},\"image\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/19596ae889d256d2760a8986abfa5c47\",\"name\":\"MachineLearningPlus\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/b066ce75243d296fa7b373e34daf3008.jpg?ver=1776364784\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/b066ce75243d296fa7b373e34daf3008.jpg?ver=1776364784\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/b066ce75243d296fa7b373e34daf3008.jpg?ver=1776364784\",\"caption\":\"MachineLearningPlus\"},\"description\":\"Machine Learning Plus is made of a group of enthusiastic folks passionate about Data Science. They help awesome Developers, Business managers and Data Scientists become better at what they do.\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/author\\\/team-machinelearningplus\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Numpy Reshape - How to reshape arrays and what does -1 mean? - machinelearningplus","description":"The numpy.reshape() is used to reshape a numpy array without changing the data in the array. Read on to know all about reshaping numpy arrays.","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:\/\/localhost:8080\/python\/numpy-reshape\/","og_locale":"en_US","og_type":"article","og_title":"Numpy Reshape - How to reshape arrays and what does -1 mean? - machinelearningplus","og_description":"The numpy.reshape() is used to reshape a numpy array without changing the data in the array. Read on to know all about reshaping numpy arrays.","og_url":"https:\/\/localhost:8080\/python\/numpy-reshape\/","og_site_name":"machinelearningplus","article_published_time":"2021-10-20T04:25:49+00:00","article_modified_time":"2022-03-08T15:36:01+00:00","og_image":[{"width":560,"height":315,"url":"https:\/\/localhost:8080\/wp-content\/uploads\/2021\/10\/Numpy_Feature_Image-min.png","type":"image\/png"}],"author":"MachineLearningPlus","twitter_card":"summary_large_image","twitter_misc":{"Written by":"MachineLearningPlus","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/localhost:8080\/python\/numpy-reshape\/#article","isPartOf":{"@id":"https:\/\/localhost:8080\/python\/numpy-reshape\/"},"author":{"name":"MachineLearningPlus","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/19596ae889d256d2760a8986abfa5c47"},"headline":"Numpy Reshape &#8211; How to reshape arrays and what does -1 mean?","datePublished":"2021-10-20T04:25:49+00:00","dateModified":"2022-03-08T15:36:01+00:00","mainEntityOfPage":{"@id":"https:\/\/localhost:8080\/python\/numpy-reshape\/"},"wordCount":1885,"commentCount":0,"publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"image":{"@id":"https:\/\/localhost:8080\/python\/numpy-reshape\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2021\/10\/Numpy_Feature_Image-min.png","keywords":["Numpy","Python"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/localhost:8080\/python\/numpy-reshape\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/localhost:8080\/python\/numpy-reshape\/","url":"https:\/\/localhost:8080\/python\/numpy-reshape\/","name":"Numpy Reshape - How to reshape arrays and what does -1 mean? - machinelearningplus","isPartOf":{"@id":"https:\/\/machinelearningplus.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/localhost:8080\/python\/numpy-reshape\/#primaryimage"},"image":{"@id":"https:\/\/localhost:8080\/python\/numpy-reshape\/#primaryimage"},"thumbnailUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2021\/10\/Numpy_Feature_Image-min.png","datePublished":"2021-10-20T04:25:49+00:00","dateModified":"2022-03-08T15:36:01+00:00","description":"The numpy.reshape() is used to reshape a numpy array without changing the data in the array. Read on to know all about reshaping numpy arrays.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/localhost:8080\/python\/numpy-reshape\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/localhost:8080\/python\/numpy-reshape\/#primaryimage","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2021\/10\/Numpy_Feature_Image-min.png","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2021\/10\/Numpy_Feature_Image-min.png","width":560,"height":315,"caption":"Numpy Feature Image"},{"@type":"WebSite","@id":"https:\/\/machinelearningplus.com\/#website","url":"https:\/\/machinelearningplus.com\/","name":"machinelearningplus","description":"Learn Data Science (AI \/ ML) Online","publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/machinelearningplus.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/machinelearningplus.com\/#organization","name":"machinelearningplus","url":"https:\/\/machinelearningplus.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","width":348,"height":36,"caption":"machinelearningplus"},"image":{"@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/19596ae889d256d2760a8986abfa5c47","name":"MachineLearningPlus","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/b066ce75243d296fa7b373e34daf3008.jpg?ver=1776364784","url":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/b066ce75243d296fa7b373e34daf3008.jpg?ver=1776364784","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/b066ce75243d296fa7b373e34daf3008.jpg?ver=1776364784","caption":"MachineLearningPlus"},"description":"Machine Learning Plus is made of a group of enthusiastic folks passionate about Data Science. They help awesome Developers, Business managers and Data Scientists become better at what they do.","url":"https:\/\/machinelearningplus.com\/author\/team-machinelearningplus\/"}]}},"_links":{"self":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/15007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/users\/28"}],"replies":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/comments?post=15007"}],"version-history":[{"count":0,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/15007\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media\/15456"}],"wp:attachment":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media?parent=15007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/categories?post=15007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/tags?post=15007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}