Merge Two Numpy Array's Of Different Shape Into A Single Array
I have two numpy array's a and b of length 53 and 82 respectively. I would like to merge them into a single array because I want to use the 53+82=135 length array say call it c for
Solution 1:
You need to use numpy.concatenate instead of array addition
c = numpy.concatenate((a, b))
Implementation
import numpy as np
a = np.arange(53)
b = np.arange(82)
c = np.concatenate((a, b))
Output
c.shape
(135, )
Solution 2:
Use numpy.concatenate
:
In [5]: import numpy as np
In [6]: a = np.arange(5)
In [7]: b = np.arange(11)
In [8]: np.concatenate((a, b))
Out[8]: array([ 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
For 1-D arrays you can also use numpy.hstack
:
In [9]: np.hstack((a, b))
Out[9]: array([ 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Post a Comment for "Merge Two Numpy Array's Of Different Shape Into A Single Array"