Let’s learn how to stack arrays in Numpy Python library.
Arrays are stackable. I’ll show how to stack array vertically and horizontally in Python Numpy.
Using vstack
To stack arrays vertically use Numpy vstack function.
import numpy as np my_array = np.array([[0, 1], [2, 4], [5, 6]]) my_second_array = np.array([[1, 4], [3, 7], [5, 8]]) stacked_array = np.vstack((my_array, my_second_array)) print(f"Arrays stacked vertically: \n {stacked_array}")
Using hstack
Similarlly you can stack array horizontally with Numpy hstack function.
import numpy as np my_array = np.array([[0, 1], [2, 4], [5, 6]]) my_second_array = np.array([[1, 4], [3, 7], [5, 8]]) stacked_array = np.hstack((my_array, my_second_array)) print(f"Arrays stacked horizontally: \n {stacked_array}")
Stacking based on dimensions
There is also a possible to stack array based on dimensions configured by Numpy stack axis parameter.
Let’s see above example with two different axis parameters based on -1 and 1 dimension of stacking.
import numpy as np my_array = np.array([[0, 1], [2, 4], [5, 6]]) my_second_array = np.array([[1, 4], [3, 7], [5, 8]]) stacked_array = np.stack((my_array, my_second_array), axis=-1) print(f"Arrays stacked using axis=-1: \n {stacked_array}") stacked_array = np.stack((my_array, my_second_array), axis=1) print(f"Arrays stacked using axis=1: \n {stacked_array}")
As you can see there are difference in outputs depends on axis parameter chosen.
For 3-dimension axes you can set axis parameter between -3 and 3 (including 0). For every other value you will get different outputs.
Using dstack
The other stacking possibility is also third axis stack. There is Numpy dstack function for that.
import numpy as np my_array = np.array([[0, 1], [2, 4], [5, 6]]) my_second_array = np.array([[1, 4], [3, 7], [5, 8]]) stacked_array = np.dstack((my_array, my_second_array)) print(f"Third axis stack: \n {stacked_array}")
Now you know so many ways to stack arrays in Python Numpy.
How to stack arrays in NumPy using the dstack() method?
If you want to stack arrays along the third dimension, you can use the dstack() method. The dstack() method takes a sequence of arrays as input and returns a new array with the arrays stacked along the third dimension.
The following code shows how to stack arrays along the third dimension using the dstack() method:
import numpy as np my_array = np.array([[0, 1], [2, 4], [5, 6]]) my_second_array = np.array([[1, 4], [3, 7], [5, 8]]) stacked_array = np.dstack((my_array, my_second_array)) print(f"Third axis stack: \n {stacked_array}")
As you can see, the dstack() method has stacked the arrays along the third dimension, creating a new array with three dimensions.
Key Takeaways
You can stack arrays in NumPy using the vstack(), hstack(), dstack(), and stack() methods.
The vstack() method stacks arrays vertically.
The hstack() method stacks arrays horizontally.
The dstack() method stacks arrays along the third dimension.
The stack() method stacks arrays along a specified dimension.
FAQs
What is stacking arrays?
Stacking arrays is the process of combining multiple arrays into a single array.
Why would I want to stack arrays?
There are a few reasons why you might want to stack arrays. First, it can make it easier to work with multiple arrays at the same time. Second, it can be used to create new arrays with different dimensions.
What are the different ways to stack arrays in NumPy?
There are four different ways to stack arrays in NumPy:
* vstack()
* hstack()
* dstack()
* stack()
Each of these methods has its own advantages and disadvantages, so you should choose the method that is most appropriate for your needs.