Array Shape & Reshape In NUMPY
In this post, we’ll dive into the details of NumPy array shape and reshape, covering the basics of reshaping Numpy arrays and the use of reshape() function to change the shape of an array.
NumPy Array Shape
The shape of an array refers to the number of elements along each dimension of the array.
In this section of article, we’ll explore the concept of array shape and how to access and modify Numpy array shape.
Get NumPY Array Shape
In Numpy Array shape, Numpy arrays have an attribute known as shape that provides a tuple with the number of items for each index.
The shape of a 2-D array can be displayed as follows:
NumPy Array Shape Example: 1
According to the example above, we return (2, 6), which indicates that the array has two dimensions, and every dimension has six items.
A 3-D array shape can be represented as follows:
NumPy Array Shape Example: 2
In the example above, it indicates that the array has three dimensions, and each dimension has three items.
With ndmin, generate a 9-dimensional array with elements 2, 3, 5, 7, 11, 13, 17, 19, and make sure that the final dimension has value 8:
NumPy Array Shape Example: 3
Utilizing ndmin, build a 6-dimensional array with components B, C, D, F, G, H, and ensure that the last dimension is 6:
NumPy Array Shape Example: 4
How does the shape tuple work?
The shape tuple in Numpy represents the dimensions of an array, with each integer in the tuple indicating the size of the corresponding dimension.
In other words, integers at every index tell us how many items there are in the respective dimension.
According to the example above, index-6 has value 6, so 6th ( 5 + 1) dimension has 6 components.
NumPy Array Reshape
In this section of article, we explore Numpy array reshape with examples in the expectation of fulfilling the learning aims.
The reshape() function in Numpy is used to change the shape of an array, allowing for a flexible representation of data.
This function takes two arguments: the first argument is the array that you want to reshape, and the second argument is the desired shape, which is specified as a tuple.
Reshape An Array
Numpy array reshape is the process of altering the shape of an array.
It is the number of items in each dimension that defines the shape of an array.
The user can insert or delete dimensions or modify the number of items in each dimension by reshaping.
Reshape From 1-D to 2-D
The following 1-D arrays has 7 components. Transform it into a 2-D array.
In above example, we first import the Numpy library as npy for convenience. Then, we create a one-dimensional Numpy array named factorial_arr which contains the values of factorials from 1 to 8.
Next, we use the reshape() function on the factorial_arr array to change its shape from (8,) to (2, 4). This function returns a new array with the desired shape, which is stored in a new variable named mrx_arr.
Finally, we print the mrx_arr array using the print() function. The output of this code would be:
array([[ 1, 2, 6, 24], [ 120, 720, 5040, 40320]])
As we can see, the mrx_arr array has been reshaped into a two-dimensional array with 2 rows and 4 columns.
The elements of the original array factorial_arr are rearranged in the new array to match the desired shape (2, 4).
Reshape From 1-D to 3-D
The below code would output the following 3-dimensional reshaped array of the original 1-dimensional array:
In this example, we create a one-dimensional Numpy array named palindrome_arr which contains a list of palindrome words.
Next, we use the reshape() function on the palindrome_arr array to change its shape from (8,) to (2, 2, 2).
This function returns a new array with the desired shape, which is stored in a new variable named mrx_arr.
Finally, we print the mrx_arr array using the print() function.
What shapes can we reshape?
In our exploration of Numpy array reshape, it works, as long as the items required in both shapes are identical.
In a 1D array with 8 items, we can reshape it into a 2D array with 4 items in 2 rows, but not into a 3D array with 3 items in 3 rows.
You can transform a 1D array with 8 components into a 2D array with 3 items in each dimension by following these steps:
Find out whether the resulting array is a copy or a view:
The code would output the base object of the reshaped array, which is the original 1-dimensional factorial_arr:
In the above code, the original 1-dimensional palindrome_arr would be output as the base object of the reshaped array.
One “unknown” dimension is permitted.
As we discussed with Numpy array reshape, you do not have to enter an accurate number for one of the dimensions.
NumPy will compute this number if you enter -1 as the value.
To transform an 8-element 1D array to a 2×2 3D array, follow the below examples:
Array flattening is the process of transforming a multidimensional array to a one-dimensional array utilizing Numpy.
To accomplish this, we can use reshape(-1).
Make the array one dimension by transforming it as follows: