29 Mar Joining Numpy Arrays
We can join two or more arrays in a single new array using the following:
- concatenate() method: Joining along with axis, unlike keys in SQL.
- stack method: Joining along a new axis, unlike keys in SQL.
Joining Numpy Arrays with concatenate()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import numpy as np # Create two arrays n1 = np.array([5, 10, 15, 20]) n2 = np.array([25, 30, 35, 40]) print("Iterating array1...") for a in n1: print(a) print("\nIterating array2...") for a in n2: print(a) # Joining arrays resarr = np.concatenate((n1, n2)) print("\nAfter Joining = ", resarr) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Iterating array1... 5 10 15 20 Iterating array2... 25 30 35 40 After Joining = [ 5 10 15 20 25 30 35 40] |
Joining Numpy Arrays with Stack Methods
Join arrays in Numpy using the stack methods, such as
- stack()
- hstack()
- vstack()
- dstack()
- column_stack()
Let us see the examples one by one:
numpy.stack() in Python
Join Numpy arrays using the stack() method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import numpy as np # Create numpy arrays n1 = np.array([5, 10, 15, 20]) n2 = np.array([25, 30, 35, 40]) print("Iterating array1...") for a in n1: print(a) print("\nIterating array2...") for a in n2: print(a) # Join arrays using numpy.stack() resarr = np.stack((n1, n2)) print("\nAfter Joining = \n", resarr) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Iterating array1... 5 10 15 20 Iterating array2... 25 30 35 40 After Joining = [[ 5 10 15 20] [25 30 35 40]] |
numpy.hstack() in Python
Stack along rows using the hstack() method in Numpy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import numpy as np # Create two arrays n1 = np.array([5, 10, 15, 20]) n2 = np.array([25, 30, 35, 40]) print("Iterating array1...") for a in n1: print(a) print("\nIterating array2...") for a in n2: print(a) # Join arrays using the hstack() resarr = np.hstack((n1, n2)) print("\nAfter Joining along rows = \n", resarr) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Iterating array1... 5 10 15 20 Iterating array2... 25 30 35 40 After Joining along rows = [ 5 10 15 20 25 30 35 40] |
numpy.vstack() in Python
Stack along columns using the vstack() method in Numpy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import numpy as np # Create two arrays n1 = np.array([5, 10, 15, 20]) n2 = np.array([25, 30, 35, 40]) print("Iterating array1...") for a in n1: print(a) print("\nIterating array2...") for a in n2: print(a) # Join arrays resarr = np.vstack((n1, n2)) print("\nAfter Joining along columns = \n", resarr) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Iterating array1... 5 10 15 20 Iterating array2... 25 30 35 40 After Joining along columns = [[ 5 10 15 20] [25 30 35 40]] |
numpy.dstack() in Python
Stack along depth i.e. height using the dstack() method in Numpy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import numpy as np # Create two arrays n1 = np.array([5, 10, 15, 20]) n2 = np.array([25, 30, 35, 40]) print("Iterating array1...") for a in n1: print(a) print("\nIterating array2...") for a in n2: print(a) # Join arrays resarr = np.dstack((n1, n2)) print("\nAfter Joining along height = \n", resarr) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Iterating array1... 5 10 15 20 Iterating array2... 25 30 35 40 After Joining along height = [[[ 5 25] [10 30] [15 35] [20 40]]] |
numpy.column_stack() in Python
Stack according to columns using the column_stack() method in Numpy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Create two arrays n1 = np.array([5, 10, 15, 20]) n2 = np.array([25, 30, 35, 40]) print("Iterating array1...") for a in n1: print(a) print("\nIterating array2...") for a in n2: print(a) # Join arrays resarr = np.column_stack((n1, n2)) print("\nAfter Joining = \n", resarr) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Iterating array1... 5 10 15 20 Iterating array2... 25 30 35 40 After Joining = [[ 5 25] [10 30] [15 35] [20 40]] |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments