Let’s say we have a 2-dimensional array. The array has three rows and two columns. So, the array has 2×3 = 6 elements. Now, we want to create a new 2-dimensional array with the same elements. That new array should have two rows and three columns. We can do so using the array.reshape() function in Python. We can also use the same array to create a three-dimensional array that has 1, 2, and 3 elements across the three dimensions, respectively.
Let’s look at an example to understand this in a better way.
import numpy list1 = [[1, 2], [3, 4], [5, 6]] array1 = numpy.array(list1) print("array1: \n", array1) array2 = array1.reshape(2, 3) print("array2: \n", array2) array3 = array1.reshape(1, 2, 3) print("array3: \n", array3)
The output of the above program will be:
array1: [[1 2] [3 4] [5 6]] array2: [[1 2 3] [4 5 6]] array3: [[[1 2 3] [4 5 6]]]
Here, we are first creating an array array1 from …
0 Comments