In NumPy, there isn't a specific "join" operation like you might find in Python strings. However, you can achieve similar functionality using concatenation or stacking operations depending on your requirements.
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
# Concatenate along the first axis (rows)
result = np.concatenate((arr1, arr2), axis=0)
print(result)
[[1 2] [3 4] [5 6]]
Stacking: NumPy provides functions like numpy.vstack() and numpy.hstack() for vertical and horizontal stacking, respectively. These functions are shortcuts for concatenating arrays along the first axis (vertical stacking) or the second axis (horizontal stacking).
import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
# Vertical stack
vstacked = np.vstack((arr1, arr2))
# Horizontal stack
hstacked = np.hstack((arr1, arr2.T)) # Transpose arr2 to match dimensions
print("Vertical stack:")
print(vstacked)
print("\nHorizontal stack:")
print(hstacked)
Vertical stack:
[[1 2]
[3 4]
[5 6]]
Horizontal stack:
[[1 2 5]
[3 4 6]]
These methods provide flexibility in joining NumPy arrays along different axes and dimensions. Depending on your specific use case, you can choose the appropriate method for joining arrays.