NumPy Tutorial: NumPy Array Shaping
Hi everyone, welcome back. NumPy is a library for the Python programming language. NumPy is short for “Numerical Python” and is a popular library that is used in data science. NumPy is used specifically to work with arrays as it provides various functions and support. In this tutorial, we will go over how we can shape our array, so our arrays will know how many dimensions and how many elements it will have. With this introduction out of the way, let’s get into it.

Shape Property
First let’s take a look at the shape property. The shape property returns a tuple which will show how many elements are in each dimension. Let’s see an example:
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
print(array.shape)Output:
(2, 3)
We can see that our tuple shows that we have 2 elements in the first dimension which would be the array containing 1, 2, and 3, and the array containing 4, 5, and 6. Then our second dimension has 3 elements which would be the individual values in the arrays, as each array has 3 elements.
ndmin Argument
The ndmin argument is an optional parameter that can be used when creating arrays to define the number of dimensions. Let’s see an example:
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]], ndmin=7)
print(array.shape)Output:
(1, 1, 1, 1, 1, 2, 3)
As we can see in the output above, we have 7 different values in our output tuple, verifying that we do indeed have 7 dimensions.
Reshape Function
The reshape function can be used to change the number of dimensions or the number of elements in each dimension of an already existing array. Let’s see an example:
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
reshapedArray = array.reshape(6)
print(reshapedArray)Output:
[1 2 3 4 5 6]
In this example, we went from a 2-D array and reshaped it into a 1-D array. Let’s see how we can change this from a 2-D array and reshape it into a 3-D array:
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
reshapedArray = array.reshape(1, 3, 2)
print(reshapedArray)Output:
[[[1 2]
[3 4]
[5 6]]]
As we can see from our output, we have successfully created a 3-D array.
Example: Modifying Original Array After Reshaping
Let’s look at an example of us modifying an original array after reshaping:
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
reshapedArray = array.reshape(6)
array[0][0] = 7
print(reshapedArray)Output:
[7 2 3 4 5 6]
We reused our example of reshaping our original 2-D array into a 1-D array, but then modified our original array after. As we can see in the output, the modification was also applied to our reshaped array. The reshape function creates a view which means any changes to the original array will reflect onto the reshaped array and any changes to the reshaped array will reflect to the original array. If modifications are wanting to be made on only one array, we can use the copy function to create a new array that won’t be affected by changes. More about the view and copy functions can be read below.
Example: Reshaping Error
Let’s see what happens if we attempt to reshape a into a shape that our current elements will not fit in:
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
reshapedArray = array.reshape(3, 2, 3)
print(reshapedArray)Output:
ValueError: cannot reshape array of size 6 into shape (3,2,3)
We get an error! When reshaping, it is always good to know how many elements are in the current array and how many elements are needed for the new shape.
Trick to Reshape Into a 1-D Array
We can use ‘-1’ to reshape any array into a 1-D Array. Let’s see an example:
import numpy as np
array = np.array([[1, 2, 3], [4, 5, 6]])
reshapedArray = array.reshape(-1)
print(reshapedArray)Output:
[1 2 3 4 5 6]
Conclusion
This is the end of the shaping NumPy arrays tutorial. For additional help with NumPy, check out the official NumPy documentation here. I hope this helps. Thanks for reading.