Saved Bookmarks
| 1. |
How do you concatenate 2 NumPy arrays? |
|
Answer» Concatenating 2 arrays by adding elements to the end can be achieved by making use of the concatenate() method of the NumPy package. Syntax: np.concatenate((a1, a2, ...), axis=0, out=None)where,
For example: import numpy as npa = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6]]) # Concatenate with axis 0 c = np.concatenate((a,b), axis=0) print("With axis 0: \n",c ) # Concatenate with axis 1 (b.T represents transpose matrix) d = np.concatenate((a,b.T), axis=1) print("With axis 1: \n",d ) The output would be: With axis 0:[[1 2] [3 4] [5 6]] With axis 1: [[1 2 5] [3 4 6]] Notice how the arrays are concatenated with different values of the axis. |
|