DEV Community

TechPlygrnd
TechPlygrnd

Posted on

NumPy Tutorial #7: Array Reshape

If you have read the previous blog in the series, you might already familiar to array shape. Now how can we reshape the existing array, perhaps if we want to flatten a multidimensional array.


Reshape

NumPy has provided a method called reshape to reshape an array. This method requires shape of the target array. To understand more, let us see it through an example.

Let us create an array

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
Enter fullscreen mode Exit fullscreen mode

I want the arr to be flat, meaning that it will only have one dimension and that dimension has 6 items in it. I can do that by

flatten_arr = arr.reshape(6, )
Enter fullscreen mode Exit fullscreen mode

Let us print the flatten_arr to see the output

print(flatten_arr)
Enter fullscreen mode Exit fullscreen mode

You will see the following output

[1 2 3 4 5 6]
Enter fullscreen mode Exit fullscreen mode

You can see the output shows the original array have been flatted.


There you go, that is how you can reshape an array. Thank you for reading and see you next time!

Top comments (0)