Merge two numpy array's of different shape into a single array

后端 未结 2 1955
Happy的楠姐
Happy的楠姐 2021-01-14 00:34

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 th

相关标签:
2条回答
  • 2021-01-14 00:44

    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, )
    
    0 讨论(0)
  • 2021-01-14 00:48

    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]
    
    0 讨论(0)
提交回复
热议问题